This file contains hidden or 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
| /** | |
| 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); |