patternjavascriptMinor
Getting object when is ready
Viewed 0 times
objectreadygettingwhen
Problem
I have a module, that fetches some object. Then other modules can use this object. Here is a little example of this task:
Am I right on doing this? How can this be made better?
var Structure = (function(){
var tree = null,
df = $.ajax({
'url' : '/some/path',
'type' : 'POST',
'data' : { some : 'data' },
'dataType' : 'json'
}),
ready = function(cb){
if (tree) {
cb(tree);
return
}
$.when(df).then(function(data){
tree = TreeHandler.create(data);
cb(tree);
});
};
return {
df : df,
ready : ready
};
})();
// Usage:
Structure.ready(function(data){
console.log (data);
// some actions with data
});Am I right on doing this? How can this be made better?
Solution
I'm repeating @RoToRa for the sake putting it in answer form for beta-stats:
Your syntax is fine. Without knowing more of the problem domain, it does seem a little unnecessary. Why not use callbacks on your
Your syntax is fine. Without knowing more of the problem domain, it does seem a little unnecessary. Why not use callbacks on your
$.ajax() call?Context
StackExchange Code Review Q#7198, answer score: 3
Revisions (0)
No revisions yet.