forked from ianstormtaylor/backbone-state
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-state.min.js
5 lines (5 loc) · 1.47 KB
/
backbone-state.min.js
1
2
3
4
5
// Backbone State 0.0.1
//
// by Ian Storm Taylor
// https://github.com/ianstormtaylor/backbone-state
(function(e,t){if(e===undefined)throw new Error("Couldn't find Underscore");if(t===undefined)throw new Error("Couldn't find Backbone");t.mixin||(t.mixin={}),t.mixin.state=function(t){var n=this.prototype._configure;this.prototype._previousAttributes||this.prototype._prepareModel?this.prototype._configure=function(e,t){n.apply(this,arguments),this._configureStates(this.states||[],t||{})}:this.prototype._configure=function(e){n.apply(this,arguments),this._configureStates(this.states||[],this.options||{})},this.prototype._configureStates=function(t,n){e.isFunction(n)&&(n=n()),this._state={};for(var r=0,i;i=t[r];r++)this._state[i]=n[i]===!0},this.prototype.state=function(e,t){if(!(arguments.length>1))return this._state[e];if(this._state[e]===t)return this;this._state[e]=t,this.$el&&this.$el.state&&this.$el.state(e,t),this.trigger("change:state:"+e,this,t),this.trigger("change:state",this,e,t)},this.prototype.toggleState=function(e,t){return this.state(e,!this.state(e))},this.prototype.onState=function(e,t,n,r){var i=this.state(e);return i===t&&n.call(r,this,i),this.on("change:state:"+e,function(e,i){i===t&&n.apply(r,arguments)}),this},this.prototype.onceState=function(e,t,n,r){var i=this.state(e);if(i===t)return n.call(r,this,i),this;var s=this,o=function(i,s){i.off("change:state:"+e,o,r),s===t&&n.apply(r,arguments)};return this.on("change:state:"+e,o,r),this}}})(_,Backbone);