-
Notifications
You must be signed in to change notification settings - Fork 0
/
MVCObject-0.21.min.js
23 lines (18 loc) · 4.31 KB
/
MVCObject-0.21.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
Available on [GitHub](https://github.com/dcporter/mvcobject).
### License
Copyright 2011 urbanplum.eu and Dave Porter. Modified from the original by
Dave Porter.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
@author John Smith - [email protected]
@author Dave Porter - [email protected]
*/
function MVCObject(){}MVCObject.prototype.bindTo=function(e,t,n,r){n=n||e;var i;if(!(n in t)){throw'Undefined: property "'+n+'" on target object.'}if(!t._isPropertyBound(n)){t[n]={__my_index:0,__observer_indexes:[],__shared_object:{__value:t[n],__observers:[{obj:t,key:n}]}}}var s=t[n].__shared_object.__observers.length;t[n].__observer_indexes.push(s);if(this._isPropertyBound(e)){if(this[e]["__my_index"]!==0)return;this._updateObserverIndexes(e,s);t[n].__shared_object.__observers=t[n].__shared_object.__observers.concat(this[e].__shared_object.__observers);i=this[e].__shared_object.__value}else{i=this[e];this[e]={__my_index:s,__observer_indexes:[],__shared_object:null};t[n].__shared_object.__observers.push({obj:this,key:e})}for(var o in t[n].__shared_object.__observers){if(o<s||t[n].__shared_object.__observers[o]===null)continue;t[n].__shared_object.__observers[o].obj[t[n].__shared_object.__observers[o].key].__shared_object=t[n].__shared_object;if(t[n].__shared_object.__value===i||o==s&&r)continue;if(t[n].__shared_object.__observers[o].key+"_changed"in t[n].__shared_object.__observers[o].obj)t[n].__shared_object.__observers[o].obj[t[n].__shared_object.__observers[o].key+"_changed"]()}};MVCObject.prototype._updateObserverIndexes=function(e,t){this[e]["__my_index"]+=t;for(var n in this[e].__observer_indexes){if(this[e].__shared_object.__observers[this[e].__observer_indexes[n]]===null)continue;this[e].__shared_object.__observers[this[e].__observer_indexes[n]].obj._updateObserverIndexes(this[e].__shared_object.__observers[this[e].__observer_indexes[n]].key,t);this[e].__observer_indexes[n]+=t}};MVCObject.prototype._isPropertyBound=function(e){return this[e]!==null&&typeof this[e]==="object"&&"__my_index"in this[e]};MVCObject.prototype.get=function(e){return this._isPropertyBound(e)?this[e].__shared_object.__value:this[e]};MVCObject.prototype.set=function(e,t){if(!e in this)throw'Cannot set value for undefined property "'+e+'".';if(this._isPropertyBound(e)){if(this[e].__shared_object.__value===t)return;this[e].__shared_object.__value=t;for(var n in this[e].__shared_object.__observers){if(this[e].__shared_object.__observers[n]===null)continue;if(this[e].__shared_object.__observers[n].key+"_changed"in this[e].__shared_object.__observers[n].obj){this[e].__shared_object.__observers[n].obj[this[e].__shared_object.__observers[n].key+"_changed"]()}}return}if(this[e]===t)return;this[e]=t;if(e+"_changed"in this)this[e+"_changed"]()};MVCObject.prototype.setValues=function(e){for(var t in e){this.set(t,e[t])}};MVCObject.prototype._rebindObservers=function(e,t){for(var n in this[t].__observer_indexes){var r;if(this[t].__shared_object.__observers[this[t].__observer_indexes[n]]!==null){r=this[t].__shared_object.__observers.splice(this[t].__observer_indexes[n],1,null)[0];r.obj._rebindObservers(e,r.key);r.obj[r.key].__shared_object=e;this[t].__observer_indexes.splice(n,1,e.__observers.length);r.obj[r.key]["__my_index"]=e.__observers.length}else{r=null}e.__observers.push(r);this[t]["__my_index"]=e.__observers.length}};MVCObject.prototype.unbind=function(e){if(!this[e]||typeof this[e]!=="object"||!("__my_index"in this[e])||this[e]["__my_index"]===0)return;this[e].__shared_object.__observers[this[e]["__my_index"]]=null;if(this[e].__observer_indexes.length===0){this[e]=this[e].__shared_object.__value;return}var t={__observers:[{obj:this,key:e}],__value:this[e].__shared_object.__value};this._rebindObservers(t,e,0);this[e]["__my_index"]=0;this[e].__shared_object=t};MVCObject.prototype.unbindAll=function(){for(var e in this){if(!this._isPropertyBound(e))continue;this.unbind(e)}}