-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.statefulEvents.min.js
4 lines (4 loc) · 1.44 KB
/
backbone.statefulEvents.min.js
1
2
3
4
// Backbone.StatefulEventView.js, v0.1.1
// (c) 2012 Ankit Solanki, nth loop.
// May be freely distributed under an MIT license.
(function(a,b){a.StatefulEventView=a.View.extend({delegateEvents:function(b){this.undelegateEvents(),this.ignoreUndelegate=!0,this.delegateStatefulEvents(),a.View.prototype.delegateEvents.call(this,b),delete this.ignoreUndelegate},undelegateEvents:function(){if(this.ignoreUndelegate)return;a.View.prototype.undelegateEvents.call(this)},delegateStatefulEvents:function(c){var d,e,f,g,h,i,j,k;if(!b.isFunction(this.getState))if(!this.model||!b.isFunction(this.model.getState))throw new Error("A Backbone.StatefulEventView or its model must define a getState() method");c=this.statefulEvents;if(!c)return;d=b.keys(c);for(e=0;e<d.length;e++){j=d[e],k=c[j],b.isFunction(k)||(k=this[c[j]]);if(!b.isFunction(k))throw new Error('Event "'+c[j]+'" does not exist');f=j.match(a.StatefulEventView._splitter);if(!f||f.length<=2)throw new Error('The key "'+j+'" passed to statefulEvents is invalid.\n'+"It needs both a state and an event name.");g=f[1],h=f[2],i=f[3],k=this._invokeMethodOnlyInGivenState(k,g),h+=".delegateEvents"+this.cid,i===""?this.$el.bind(h,k):this.$el.delegate(i,h,k)}},_invokeMethodOnlyInGivenState:function(a,c){return b.bind(function(d){var e=b.isFunction(this.getState)?this.getState():this.model.getState();return e===c?a.call(this,d):!0},this)}}),a.StatefulEventView._splitter=/^(\S+)\s+(\S+)\s*(.*)$/})(this.Backbone,this._);