Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have some code I call EventedModel:</p> <pre><code>EventedModel = Backbone.Model.extend({ save: function(attrs, options) { var complete, self, success, value; self = this; options || (options = {}); success = options.success; options.success = function(resp) { self.trigger("save:success", self); if (success) { return success(self, resp); } }; complete = options.complete; options.complete = function(resp) { self.trigger("save:complete", self); if (complete) { return complete(self, resp); } }; this.trigger("save", this); value = Backbone.Model.prototype.save.call(this, attrs, options); return value; } }); </code></pre> <p>You can use it as a backbone model. But it will trigger save and save:complete. You can boost this a little:</p> <pre><code>EventedSynchroneModel = Backbone.Model.extend({ save: function(attrs, options) { var complete, self, success, value; if(this.saving){ if(this.needsUpdate){ this.needsUpdate = { attrs: _.extend(this.needsUpdate, attrs), options: _.extend(this.needsUpdate, options)}; }else { this.needsUpdate = { attrs: attrs, options: options }; } return; } self = this; options || (options = {}); success = options.success; options.success = function(resp) { self.trigger("save:success", self); if (success) { return success(self, resp); } }; complete = options.complete; options.complete = function(resp) { self.trigger("save:complete", self); //call previous callback if any if (complete) { complete(self, resp); } this.saving = false; if(self.needsUpdate){ self.save(self.needsUpdate.attrs, self.needsUpdate.options); self.needsUpdate = null; } }; this.trigger("save", this); // we are saving this.saving = true; value = Backbone.Model.prototype.save.call(this, attrs, options); return value; } }); </code></pre> <p>(untested code)</p> <p>Upon the first save call it will save the record normally. If you quickly do a new save it will buffer that call (merging the different attributes and options into a single call). Once the first save succeed, you go forward with the second save.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload