Skip to content

Instantly share code, notes, and snippets.

@lifeinafolder
lifeinafolder / promises-multicallbacks.js
Created January 15, 2012 21:36
promises-multiplecallbacks
var p = async(data);
p.done(function(response){
// do task 1
});
p.done(function(response){
// do task 2
});
@lifeinafolder
lifeinafolder / multicallbacks.js
Created January 15, 2012 21:34
promises-multiplecallbacks
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);
});
@lifeinafolder
lifeinafolder / jsdoc-promise.js
Created January 15, 2012 20:11
promise jsdoc
/**
* asynchronousCall function
* @param data - input data
* @return promise object- It will resolve with `response` data
* or fail with `error` object
*/
@lifeinafolder
lifeinafolder / jsdoc-async.js
Created January 15, 2012 20:10
Async Call JsDoc
/**
* asynchronousCall function
* @param data - input data
* @param success callback. Should accept `response` parameter
* @param failure callback. Should accept `error` parameter
* @return void
*/
@lifeinafolder
lifeinafolder / promises.js
Created January 15, 2012 20:07
Promise Example
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);
@lifeinafolder
lifeinafolder / new-observer-pattern.js
Created December 6, 2011 07:54
Fun way of doing Observer pattern without maintaining an array of listeners and closely mimicing browser eventing scheme.
// @author: Rajat Mittal
/**
* Sample Model Constructor
* @param {Object} fields
*/
var Model = function(fields){
this._fields = fields;
/**
* NOTICE how we dropped the internal array _listeners
@lifeinafolder
lifeinafolder / vanilla-observer-pattern.js
Created December 6, 2011 07:50
Textbook Observer Pattern Example
/**
* 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
//his own object literal
var $externalLib_Proto = {};
// aliasing your library to his object
myFunkyLibrary.call($externalLib_Proto);
//calling your library
$externalLib_Proto.fn();
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
};
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
};
})();