How can i handle multiple callbacks? Right now only the first one i register gets fired..
Here is my current code for handling the callbacks (this code is being loaded async)
var widget = function () {
var onNameCallback,
onTriggedCallback;
function getName(callback) {
onNameCallback = callback;
}
function onTrigg(callback) {
onTriggedCallback = callback;
}
/** lots of code ... **/
if(typeof onNameCallback == 'function') {
// Fire the callback
onNameCallback('1');
}
if(typeof onTriggedCallback == 'function') {
// Fire the callback
onTriggedCallback('hello');
}
/** lots of code ... **/
return {
getName:getName,
onTrigg:onTrigg,
};
}();
if (window._test) {
for (var i = 0; i < _test.length; i++) {
var method = _test.shift();
try {
widget[method].apply(widget, _test);
} catch (err) {
console.log(err);
}
var method = _test.shift();
}
};
And here is how i register them:
_test.push('getName', function(data) {
console.log(data);
});
_test.push('onTrigg', function(data) {
console.log(data);
});
But for some reason only getName is fired..
Any ideas?
_testwill contain the elements['name', function, 'name', function]. The code I wrote to one of your previous questions (which usesshift) was under the assumption that every element of_testis an array itself. To make it work with in your case, you would have to find the next method name in the array, remove all elements up to then and pass them as second argument to.apply, e.g._test.splice(0, index_of_next_method_name). If you only pass one argument to the method, that's not a problem, otherwise using an array of arrays is easier._.testtwice in for loop..['getName', function(data) {...}]. A function is just like any other value. I think overall it would be easier for you if you read some JavaScript tutorials (e.g. eloquentjavascript.net), so that you are familiar with these basics. It would be more fruitful than asking a bunch of SO questions (and wait for answers).