This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p = async(data); | |
p.done(function(response){ | |
// do task 1 | |
}); | |
p.done(function(response){ | |
// do task 2 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var t1 = Event.subscribe('asyncOp',function(data){ | |
//Listener for the data that comes in | |
// when the event happens. | |
}); | |
async(data, function(response){ | |
Event.dispatch('asyncOp',response); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* asynchronousCall function | |
* @param data - input data | |
* @return promise object- It will resolve with `response` data | |
* or fail with `error` object | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* asynchronousCall function | |
* @param data - input data | |
* @param success callback. Should accept `response` parameter | |
* @param failure callback. Should accept `error` parameter | |
* @return void | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
asyncCall(data,function(response){ | |
// You have response from your asynchronous call here. | |
}, function(err){ | |
// if the async call fails, the error callback is invoked | |
}); | |
// And now with Promises pattern, you would write it like this: | |
var promise = asyncCall(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @author: Rajat Mittal | |
/** | |
* Sample Model Constructor | |
* @param {Object} fields | |
*/ | |
var Model = function(fields){ | |
this._fields = fields; | |
/** | |
* NOTICE how we dropped the internal array _listeners |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extremely naiive implementation of Observer pattern | |
* Its used to illustrate the concept and to basically show | |
* How its done. This is not production level code. | |
* @author: Rajat Mittal | |
*/ | |
/** | |
* Sample Model Constructor | |
* @param {Object} fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//his own object literal | |
var $externalLib_Proto = {}; | |
// aliasing your library to his object | |
myFunkyLibrary.call($externalLib_Proto); | |
//calling your library | |
$externalLib_Proto.fn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myFunkyLibrary = function(){ | |
var _prop1 = ''; // this property turned private. woozahh! | |
//function meant for internal usage. This is truly private now. | |
var _fn2 = function(){}; | |
this.prop2 = 'some value here'; // a public property. | |
this.fn = function(){}; //public function of the library | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myFunkyLibrary = (function(){ | |
var _prop1 = ''; // this property turned private. woozahh! | |
//function meant for internal usage. This is truly private now. | |
var _fn2 = function(){}; | |
return { | |
prop2 : 'some value here', //public property | |
fn = function(){} //public function of the library | |
}; | |
})(); |