Skip to content

Instantly share code, notes, and snippets.

@greim
Created April 3, 2012 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greim/2294794 to your computer and use it in GitHub Desktop.
Save greim/2294794 to your computer and use it in GitHub Desktop.
Backbone.SoftModel
/**
Extend Backbone.SoftModel and then bind to the 'soft:change' event.
It will only fire once given a sequence of rapid-fire changes.
This avoids for example having multiple redundant renders,
which can be a performance killer. That way you don't have to
splatter your code with speculative {silent:true}s.
Warning!
This code is untested. This is just exploring an idea.
*/
Backbone.SoftModel = (function(window){
var arraySlice = [].slice;
var superTrigger = Backbone.Model.prototype.trigger;
return Backbone.Model.extend({
trigger: function(eventName){
var args = arraySlice.call(arguments);
var self = this;
superTrigger.apply(self, args);
args[0] = 'soft:'+args[0];
if (!self.timers) { self.timers = {}; }
window.clearTimeout(self.timers[eventName]);
self.timers[eventName] = window.setTimeout(function(){
superTrigger.apply(self, args);
}, 10);
}
});
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment