javascript - Custom JQuery deferred handler -


not sure if possible i'll give go. i've got deferred function makes call of server either create or update , object. example:

var createorupdate = function(item) {     var self = this;     var deferred;      if (item.id && item.id > 0) {         updated = true;         deferred = update.call(self, params);     } else {         deferred = create.call(self);     }      return deferred; }; 

where create , call methods return deferred of themselves. allow me call

foo.createorupdate(bar).then(function(response) {     // happened }); 

however i'd able add in custom deferred handlers this

foo.createorupdate(bar).created(function(response) {     // item created }).updated(function(response) {     //an item updated }); 

firstly, possible. , secondly, if so, how go it.

basically want create custom deferred handler.

many thanks.

not tested:

var createorupdate = function(item) {   return {     isupdated : item.id && item.id > 0,     created   : function(cb) {       if (! this.isupdated)         create.call(self).then(cb);       return this;     },       updated   : function(cb) {       if (this.isupdated)         update.call(self, params).then(cb);       return this;     }      };   };    foo.createorupdate(bar)   .created(function(response) {     // item created   })   .updated(function(response) {     //an item updated   }); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -