我正在接受别人的代码,但不确定我是否会以正确的方式做某事。
有一个跟踪器“类”(基本上只是一个函数对象,包括一个“初始化器”)。
该类将调用第三方库。
返回结果后应该发生回调。
这是我正在尝试做的事情:
var tracker = Tracking.initialize({
prod: 'OurProduct',
requiredLayoutKeys: ["scr"],
requiredInteractiveKeys: ["name", "action"],
maxQueueSize: 5,
timeToSend: 5000,
});
// this code should happen in the callback once tracker is initialized:
InitSessionInfo(
{sid: tracker.__sid, product: tracker.prod},
function (returnObj) {
setSessionIP(tracker.getIP(),function(returnObj){
OurApp.auth(function (user) {
OurApp.initialize(user);
});
});
});
以下是 Tracking 对象在单独文件中的样子:
var Tracking = {
__required_layout_keys: null,
__required_interactive_keys: null,
__maxQueueSize: null,
__timeToSend: null,
__initialized: false,
initialize: function(config){
Tracking.__initialized = true;
Tracking.__required_interactive_keys = config.requiredInteractiveKeys;
Tracking.__required_layout_keys = config.requiredLayoutKeys;
Tracking.__maxQueueSize = config.maxQueueSize;
Tracking.__timeToSend = config.timeToSend;
Tracking.__bid = '';
Tracking.__sid = '';
Tracking.__ua = '';
// get user data from FingerPrintJs2.
// The callback needs to happen after these results are returned:
new Fingerprint2().get(function(result, components){
var ua = components.find(function(item){
return item.key === "user_agent";
});
Tracking.__bid = result;
Tracking.__sid = result + "." + Date.now();
Tracking.__ua = ua.value;
});
},
// there are other functions here that return ip, etc.
}
不幸的是,“跟踪器”总是以未定义的形式返回。
更新:
在此处粘贴调试器时:
var tracker = Tracking.initialize({
prod: 'OurProduct',
requiredLayoutKeys: ["scr"],
requiredInteractiveKeys: ["name", "action"],
maxQueueSize: 5,
timeToSend: 5000,
});
debugger;
tracker 的值为“未定义”,Tracking 的值尚未设置其“sid”值。
跟踪更新:
var Tracking = {
...
initialize: function(){
...
return new Fingerprint2().get(function(result, components){
var ua = components.find(function(item){
return item.key === "user_agent";
});
Tracking.__bid = result;
Tracking.__sid = result + "." + Date.now();
Tracking.__ua = ua.value;
return Tracking;
});
}