forked from kmalakoff/knockback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknockback.min.js
10 lines (10 loc) · 28 KB
/
knockback.min.js
1
2
3
4
5
6
7
8
9
10
/*
knockback.js 0.13.0
(c) 2011 Kevin Malakoff.
Knockback.js is freely distributable under the MIT license.
See the following for full license details:
https://github.com/kmalakoff/knockback/blob/master/LICENSE
Dependencies: Knockout.js, Backbone.js, and Underscore.js.
Optional dependency: Backbone.ModelRef.js.
*/
if(!this.ko){throw new Error("Knockback: Dependency alert! Knockout.js must be included before this file")}if(!this.Backbone){throw new Error("Knockback: Dependency alert! Backbone.js must be included before this file")}if(!this._||!this._.VERSION){throw new Error("Knockback: Dependency alert! Underscore.js must be included before this file")}this.Knockback||(this.Knockback={});this.kb||(this.kb=this.Knockback);Knockback.VERSION="0.13.0";Knockback.locale_manager;Knockback.wrappedObservable=function(a){if(!a._kb_observable){throw new Error("Knockback: _kb_observable missing from your instance")}return a._kb_observable};Knockback.setToDefault=function(a){if(a&&a.setToDefault){return a.setToDefault()}};Knockback.vmSetToDefault=function(b){var c,d,a;a=[];for(c in b){d=b[c];a.push(kb.setToDefault(d))}return a};Knockback.vmRelease=function(a){if(a instanceof kb.ViewModel_RCBase){a.release();return}return Knockback.vmReleaseObservables(a)};Knockback.vmReleaseObservables=function(b,d){var c,e,a;a=[];for(c in b){e=b[c];if(!e){continue}if(!(ko.isObservable(e)||(e instanceof kb.Observables)||(e instanceof kb.ViewModel_RCBase))){continue}if(d&&!_.contains(d,c)){continue}b[c]=null;a.push(kb.vmReleaseObservable(e))}return a};Knockback.vmReleaseObservable=function(a){if(!(ko.isObservable(a)||(a instanceof kb.Observables)||(a instanceof kb.ViewModel_RCBase))){return}if(a.destroy){return a.destroy()}else{if(a.dispose){return a.dispose()}else{if(a.release){return a.release()}}}};var __bind=function(a,b){return function(){return a.apply(b,arguments)}},__hasProp=Object.prototype.hasOwnProperty,__extends=function(d,b){for(var a in b){if(__hasProp.call(b,a)){d[a]=b[a]}}function c(){this.constructor=d}c.prototype=b.prototype;d.prototype=new c;d.__super__=b.prototype;return d};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.CollectionObservable=(function(){function a(j,i,l){var b,d,g,f,k,c,h,e;this.vm_observable_array=i;if(l==null){l={}}if(!j){throw new Error("CollectionObservable: collection is missing")}if(l.hasOwnProperty("view_model")){l.view_model_constructor=l.view_model;delete l.view_model}if(this.vm_observable_array||l.view_model_create||l.view_model_constructor){if(!this.vm_observable_array){throw new Error("CollectionObservable: vm_observable_array is missing")}}if(l.hasOwnProperty("defer")){b=l.defer;delete l.defer}this.options=_.clone(l);_.bindAll(this,"destroy","collection","sortedIndex","sortAttribute","viewModelByModel","eachViewModel","bind","unbind","trigger");_.bindAll(this,"_onGetValue","_onCollectionReset","_onCollectionResort","_onModelAdd","_onModelRemove","_onModelChanged");this._kb_collection=j;if(this._kb_collection.retain){this._kb_collection.retain()}this._kb_collection.bind("reset",this._onCollectionReset);if(!this.options.sorted_index){this._kb_collection.bind("resort",this._onCollectionResort)}h=["new","add"];for(g=0,k=h.length;g<k;g++){d=h[g];this._kb_collection.bind(d,this._onModelAdd)}e=["remove","destroy"];for(f=0,c=e.length;f<c;f++){d=e[f];this._kb_collection.bind(d,this._onModelRemove)}this._kb_collection.bind("change",this._onModelChanged);this._kb_value_observable=ko.observableArray([]);this._kb_observable=ko.dependentObservable(this._onGetValue);this._kb_observable.destroy=this.destroy;this._kb_observable.collection=this.collection;this._kb_observable.viewModelByModel=this.viewModelByModel;this._kb_observable.eachViewModel=this.eachViewModel;this._kb_observable.sortedIndex=this.sortedIndex;this._kb_observable.sortAttribute=this.sortAttribute;this._kb_observable.bind=this.bind;this._kb_observable.unbind=this.unbind;this._kb_observable.trigger=this.trigger;this.sortedIndex(this.options.sorted_index,this.options.sort_attribute,{silent:true,defer:b});return kb.wrappedObservable(this)}a.prototype.destroy=function(){var e,g,d,c,b,f,h;this._clearViewModels();this._kb_collection.unbind("reset",this._onCollectionReset);if(!this.options.sorted_index){this._kb_collection.unbind("resort",this._onCollectionResort)}f=["new","add"];for(g=0,c=f.length;g<c;g++){e=f[g];this._kb_collection.unbind(e,this._onModelAdd)}h=["remove","destroy"];for(d=0,b=h.length;d<b;d++){e=h[d];this._kb_collection.unbind(e,this._onModelRemove)}this._kb_collection.unbind("change",this._onModelChanged);if(this._kb_collection.release){this._kb_collection.release()}this._kb_collection=null;this._kb_value_observable=null;this._kb_observable.dispose();this._kb_observable=null;return this.options=null};a.prototype.collection=function(){this._kb_value_observable();return this._kb_collection};a.prototype.sortedIndex=function(e,b,c){var d;if(c==null){c={}}if(e){this.options.sorted_index=e;this.options.sort_attribute=b}else{if(b){this.options.sort_attribute=b;this.options.sorted_index=this._sortAttributeFn(b)}else{this.options.sort_attribute=null;this.options.sorted_index=null}}d=__bind(function(){if((this._kb_collection.models.length===0)&&(this._kb_value_observable().length===0)){return}this._collectionResync(true);if(!c.silent){return this.trigger("resort",this.vm_observable_array())}},this);if(c.defer){_.defer(d)}else{d()}return this};a.prototype.sortAttribute=function(c,d,b){return this.sortedIndex(d,c,b)};a.prototype._sortAttributeFn=function(b){return function(d,c){return _.sortedIndex(d,c,function(e){return e.get(b)})}};a.prototype.viewModelByModel=function(c){var b;if(!this.vm_observable_array){throw new Error("CollectionObservable: cannot get a view model if vm_observable_array was not supplied")}b=c.hasOwnProperty(c.idAttribute)?c.idAttribute:"cid";return _.find(this.vm_observable_array(),function(d){return d.__kb_model[b]===c[b]})};a.prototype.eachViewModel=function(e){var d,g,c,f,b;f=this.vm_observable_array();b=[];for(g=0,c=f.length;g<c;g++){d=f[g];b.push(e(d))}return b};a.prototype._onGetValue=function(){return this._kb_value_observable()};a.prototype._onCollectionReset=function(){return this._collectionResync()};a.prototype._onCollectionResort=function(b){if(this.options.sorted_index){throw new Error("CollectionObservable: collection sorted_index unexpected")}if(_.isArray(b)){this._collectionResync(true);return this.trigger("resort",this.vm_observable_array())}else{return this._onModelResort(b)}};a.prototype._onModelAdd=function(d){var b,e,c;if(this.options.sorted_index){e=_.pluck(this.vm_observable_array(),"__kb_model");b=this.options.sorted_index(e,d)}else{b=this._kb_collection.indexOf(d)}if(this.vm_observable_array){c=this._viewModelCreate(d);this.vm_observable_array.splice(b,0,c)}this._kb_value_observable.splice(b,0,d);if(this.vm_observable_array){return this.trigger("add",c,this.vm_observable_array())}};a.prototype._onModelRemove=function(c){var b;this._kb_value_observable.remove(c);if(this.vm_observable_array){b=this.viewModelByModel(c);if(!b){return}this.vm_observable_array.remove(b);this.trigger("remove",b,this.vm_observable_array());kb.vmRelease(b);return b.__kb_model=null}};a.prototype._onModelChanged=function(b){if(this.options.sorted_index&&(!this.options.sort_attribute||b.hasChanged(this.options.sort_attribute))){this._onModelResort(b)}return this._kb_value_observable.valueHasMutated()};a.prototype._onModelResort=function(d){var b,f,e,c;f=this._kb_value_observable.indexOf(d);if(this.options.sorted_index){e=_.clone(this._kb_value_observable());e.splice(f,1);b=this.options.sorted_index(e,d)}else{b=this._kb_collection.indexOf(d)}if(f===b){return}if(this.vm_observable_array){c=this.viewModelByModel(d);this.vm_observable_array.splice(f,1);this.vm_observable_array.splice(b,0,c)}this._kb_value_observable.splice(f,1);this._kb_value_observable.splice(b,0,d);if(this.vm_observable_array){return this.trigger("resort",c,this.vm_observable_array(),b)}};a.prototype._clearViewModels=function(e){var d,f,g,c,b;if(this.vm_observable_array){if(!e){this.trigger("remove",this.vm_observable_array())}f=this.vm_observable_array.removeAll();b=[];for(g=0,c=f.length;g<c;g++){d=f[g];b.push(kb.vmRelease(d))}return b}};a.prototype._collectionResync=function(j){var b,i,d,e,g,f,k,c,h;this._clearViewModels(j);this._kb_value_observable.removeAll();if(this.options.sorted_index){d=[];h=this._kb_collection.models;for(g=0,k=h.length;g<k;g++){i=h[g];b=this.options.sorted_index(d,i);d.splice(b,0,i)}}else{d=_.clone(this._kb_collection.models)}if(this.vm_observable_array){e=[];for(f=0,c=d.length;f<c;f++){i=d[f];e.push(this._viewModelCreate(i))}this.vm_observable_array(e)}this._kb_value_observable(d);if(this.vm_observable_array){if(!j){return this.trigger("add",this.vm_observable_array())}}};a.prototype._viewModelCreate=function(c){var b;if(this.options.view_model_create){b=this.options.view_model_create(c)}else{if(this.options.view_model_constructor){b=new this.options.view_model_constructor(c)}else{b=kb.viewModel(c)}}b.__kb_model=c;return b};return a})();__extends(Knockback.CollectionObservable.prototype,Backbone.Events);Knockback.collectionObservable=function(b,c,a){return new Knockback.CollectionObservable(b,c,a)};Knockback.viewModelGetModel=Knockback.vmModel=function(a){return a.__kb_model};Knockback.sortedIndexWrapAttr=Knockback.siwa=function(a,b){return function(d,c){return _.sortedIndex(d,c,function(e){return new b(e.get(a))})}};var __bind=function(a,b){return function(){return a.apply(b,arguments)}};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.DefaultWrapper=(function(){function a(c,b){this.default_value=b;_.bindAll(this,"destroy","setToDefault");this._kb_observable=ko.dependentObservable({read:__bind(function(){var d;d=ko.utils.unwrapObservable(c());if(!d){return ko.utils.unwrapObservable(this.default_value)}else{return d}},this),write:function(d){return c(d)},owner:{}});this._kb_observable.destroy=this.destroy;this._kb_observable.setToDefault=this.setToDefault;return kb.wrappedObservable(this)}a.prototype.destroy=function(){this._kb_observable=null;return this.default_value=null};a.prototype.setToDefault=function(){return this._kb_observable(this.default_value)};return a})();Knockback.defaultWrapper=function(b,a){return new Knockback.DefaultWrapper(b,a)};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.toFormattedString=function(g){var b,e,d,c,a,f;a=g.slice();e=Array.prototype.slice.call(arguments,1);for(d in e){b=e[d];f=ko.utils.unwrapObservable(b);if(!f){f=""}c=g.indexOf("{"+d+"}");while(c>=0){a=a.replace("{"+d+"}",f);c=g.indexOf("{"+d+"}",c+1)}}return a};Knockback.parseFormattedString=function(h,m){var i,a,j,k,g,p,o,f,l,b,e,n,d,c;b=m.slice();j=0;p=0;f={};while(b.search("\\{"+j+"\\}")>=0){o=m.indexOf("{"+j+"}");while(o>=0){b=b.replace("{"+j+"}","(.*)");f[o]=j;p++;o=m.indexOf("{"+j+"}",o+1)}j++}i=j;l=new RegExp(b);g=l.exec(h);if(g){g.shift()}if(!g||(g.length!==p)){return _.map((function(){c=[];for(var q=1;1<=i?q<=i:q>=i;1<=i?q++:q--){c.push(q)}return c}).apply(this),function(){return""})}n=_.sortBy(_.keys(f),function(q,r){return parseInt(q,10)});a={};for(k in n){o=n[k];j=f[o];if(a.hasOwnProperty(j)){continue}a[j]=k}e=[];j=0;while(j<i){e.push(g[a[j]]);j++}return e};Knockback.formattedObservable=function(d,c){var b,a;b=Array.prototype.slice.call(arguments,1);a=ko.dependentObservable({read:function(){var e,g,f;c=[ko.utils.unwrapObservable(d)];for(g=0,f=b.length;g<f;g++){e=b[g];c.push(ko.utils.unwrapObservable(e))}return kb.toFormattedString.apply(null,c)},write:function(h){var f,g,i,e;g=kb.parseFormattedString(h,ko.utils.unwrapObservable(d));i=Math.min(b.length,g.length);f=0;e=[];while(f<i){b[f](g[f]);e.push(f++)}return e},owner:{}});return a};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.LocalizedObservable=(function(){function a(d,c,b){this.value=d;this.options=c!=null?c:{};this.view_model=b;if(!(this.options.read||this.read)){throw new Error("LocalizedObservable: options.read is missing")}if(this.options.read&&this.read){throw new Error("LocalizedObservable: options.read and read class function exist. You need to choose one.")}if(this.options.write&&this.write){throw new Error("LocalizedObservable: options.write and write class function exist. You need to choose one.")}if(!kb.locale_manager){throw new Error("LocalizedObservable: Knockback.locale_manager is not defined")}_.bindAll(this,"destroy","setToDefault","resetToCurrent","observedValue","_onGetValue","_onSetValue","_onLocaleChange");this._kb_read=this.options.read?this.options.read:this.read;this._kb_write=this.options.write?this.options.write:this.write;this._kb_default=this.options["default"]?this.options["default"]:this["default"];if(this.value){d=ko.utils.unwrapObservable(this.value)}this._kb_value_observable=ko.observable(!d?this._getDefaultValue():this._kb_read.call(this,d,null));if(this._kb_write){if(!this.view_model){this.view_model={}}if(!_.isFunction(this._kb_write)){throw new Error("LocalizedObservable: options.write is not a function for read_write model attribute")}this._kb_observable=ko.dependentObservable({read:this._onGetValue,write:this._onSetValue,owner:this.view_model})}else{this._kb_observable=ko.dependentObservable(this._onGetValue)}this._kb_observable.destroy=this.destroy;this._kb_observable.observedValue=this.observedValue;this._kb_observable.setToDefault=this.setToDefault;this._kb_observable.resetToCurrent=this.resetToCurrent;kb.locale_manager.bind("change",this._onLocaleChange);return kb.wrappedObservable(this)}a.prototype.destroy=function(){kb.locale_manager.unbind("change",this._onLocaleChange);this._kb_value_observable=null;this._kb_observable.dispose();this._kb_observable=null;this.options={};return this.view_model=null};a.prototype.setToDefault=function(){var c,b;if(!this._kb_default){return}b=this._getDefaultValue();c=this._kb_value_observable();if(c!==b){return this._onSetValue(b)}else{return this._kb_value_observable.valueHasMutated()}};a.prototype.resetToCurrent=function(){this._kb_value_observable(null);return this._onSetValue(this._getCurrentValue())};a.prototype.observedValue=function(b){if(arguments.length===0){return this.value}this.value=b;this._onLocaleChange();return this};a.prototype._getDefaultValue=function(){if(!this._kb_default){return""}if(_.isFunction(this._kb_default)){return this._kb_default()}else{return this._kb_default}};a.prototype._getCurrentValue=function(){if(!(this.value&&this._kb_observable)){return this._getDefaultValue()}return this._kb_read.call(this,ko.utils.unwrapObservable(this.value),this._kb_observable)};a.prototype._onGetValue=function(){if(this.value){ko.utils.unwrapObservable(this.value)}return this._kb_value_observable()};a.prototype._onSetValue=function(b){this._kb_write.call(this,b,ko.utils.unwrapObservable(this.value),this._kb_observable);b=this._kb_read.call(this,ko.utils.unwrapObservable(this.value),this._kb_observable);this._kb_value_observable(b);if(this.options.onChange){return this.options.onChange(b)}};a.prototype._onLocaleChange=function(){var b;b=this._kb_read.call(this,ko.utils.unwrapObservable(this.value),this._kb_observable);this._kb_value_observable(b);if(this.options.onChange){return this.options.onChange(b)}};return a})();Knockback.localizedObservable=function(c,b,a){return new Knockback.LocalizedObservable(c,b,a)};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.Observable=(function(){function a(d,c,b){this.model=d;this.options=c;this.view_model=b;if(!this.model){throw new Error("Observable: model is missing")}if(!this.options){throw new Error("Observable: options is missing")}if(_.isString(this.options)||ko.isObservable(this.options)){this.options={key:this.options}}if(!this.options.key){throw new Error("Observable: options.key is missing")}_.bindAll(this,"destroy","setToDefault","_onGetValue","_onSetValue","_onModelChange","_onModelLoaded","_onModelUnloaded");if(Backbone.ModelRef&&(this.model instanceof Backbone.ModelRef)){this.model_ref=this.model;this.model_ref.retain();this.model_ref.bind("loaded",this._onModelLoaded);this.model_ref.bind("unloaded",this._onModelUnloaded);this.model=this.model_ref.getModel()}this._kb_value_observable=ko.observable();if(this.options.localizer){this._kb_localizer=new this.options.localizer(this._getCurrentValue())}if(this.options.write){if(!this.view_model){throw new Error("Observable: view_model is missing for read_write model attribute")}this._kb_observable=ko.dependentObservable({read:this._onGetValue,write:this._onSetValue,owner:this.view_model})}else{this._kb_observable=ko.dependentObservable(this._onGetValue)}this._kb_observable.destroy=this.destroy;this._kb_observable.setToDefault=this.setToDefault;if(!this.model_ref||this.model_ref.isLoaded()){this.model.bind("change",this._onModelChange)}return kb.wrappedObservable(this)}a.prototype.destroy=function(){this._kb_value_observable=null;this._kb_observable.dispose();this._kb_observable=null;if(this.model){this._onModelUnloaded(this.model)}if(this.model_ref){this.model_ref.unbind("loaded",this._onModelLoaded);this.model_ref.unbind("unloaded",this._onModelUnloaded);this.model_ref.release();this.model_ref=null}this.options=null;return this.view_model=null};a.prototype.setToDefault=function(){var b;b=this._getDefaultValue();if(this._kb_localizer){this._kb_localizer.observedValue(b);b=this._kb_localizer()}return this._kb_value_observable(b)};a.prototype._getDefaultValue=function(){if(!this.options.hasOwnProperty("default")){return""}if(_.isFunction(this.options["default"])){return this.options["default"]()}else{return this.options["default"]}};a.prototype._getCurrentValue=function(){var b,d,e,g,c,f;if(!this.model){return this._getDefaultValue()}e=ko.utils.unwrapObservable(this.options.key);d=[e];if(!_.isUndefined(this.options.args)){if(_.isArray(this.options.args)){f=this.options.args;for(g=0,c=f.length;g<c;g++){b=f[g];d.push(ko.utils.unwrapObservable(b))}}else{d.push(ko.utils.unwrapObservable(this.options.args))}}if(this.options.read){return this.options.read.apply(this.view_model,d)}else{return this.model.get.apply(this.model,d)}};a.prototype._onGetValue=function(){var b,f,e,c,d;this._kb_value_observable();ko.utils.unwrapObservable(this.options.key);if(!_.isUndefined(this.options.args)){if(_.isArray(this.options.args)){d=this.options.args;for(e=0,c=d.length;e<c;e++){b=d[e];ko.utils.unwrapObservable(b)}}else{ko.utils.unwrapObservable(this.options.args)}}f=this._getCurrentValue();if(this._kb_localizer){this._kb_localizer.observedValue(f);f=this._kb_localizer()}return f};a.prototype._onSetValue=function(h){var b,e,d,g,c,f;if(this._kb_localizer){this._kb_localizer(h);h=this._kb_localizer.observedValue()}if(this.model){d={};d[ko.utils.unwrapObservable(this.options.key)]=h;e=_.isFunction(this.options.write)?[h]:[d];if(!_.isUndefined(this.options.args)){if(_.isArray(this.options.args)){f=this.options.args;for(g=0,c=f.length;g<c;g++){b=f[g];e.push(ko.utils.unwrapObservable(b))}}else{e.push(ko.utils.unwrapObservable(this.options.args))}}if(_.isFunction(this.options.write)){this.options.write.apply(this.view_model,e)}else{this.model.set.apply(this.model,e)}}if(this._kb_localizer){return this._kb_value_observable(this._kb_localizer())}else{return this._kb_value_observable(h)}};a.prototype._onModelLoaded=function(b){this.model=b;this.model.bind("change",this._onModelChange);return this._updateValue()};a.prototype._onModelUnloaded=function(b){if(this._kb_localizer&&this._kb_localizer.destroy){this._kb_localizer.destroy();this._kb_localizer=null}this.model.unbind("change",this._onModelChange);return this.model=null};a.prototype._onModelChange=function(){if((this.model&&this.model.hasChanged)&&!this.model.hasChanged(ko.utils.unwrapObservable(this.options.key))){return}return this._updateValue()};a.prototype._updateValue=function(){var b;b=this._getCurrentValue();if(this._kb_localizer){this._kb_localizer.observedValue(b);b=this._kb_localizer()}return this._kb_value_observable(b)};return a})();Knockback.observable=function(c,b,a){return new Knockback.Observable(c,b,a)};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.Observables=(function(){function a(e,f,k,d){var g,h,i,j,c,b;this.model=e;this.mappings_info=f;this.view_model=k;if(!this.model){throw new Error("Observables: model is missing")}if(!this.mappings_info){throw new Error("Observables: mappings_info is missing")}if(!!d&&((_.isBoolean(d)&&d)||!!d.write)){j=_.isBoolean(d)?d:!!d.write;c=this.mappings_info;for(i in c){h=c[i];g=_.isString(h);if(g||!h.hasOwnProperty(j)){h=g?{key:h,write:j}:_.extend({write:j},h)}this.view_model[i]=kb.observable(this.model,h,this.view_model)}}else{b=this.mappings_info;for(i in b){h=b[i];this.view_model[i]=kb.observable(this.model,h,this.view_model)}}}a.prototype.destroy=function(){var b,d,c;c=this.mappings_info;for(d in c){b=c[d];if(this.view_model[d]){this.view_model[d].destroy()}this.view_model[d]=null}this.view_model=null;this.mappings_info=null;return this.model=null};a.prototype.setToDefault=function(){var c,e,d,b;d=this.mappings_info;b=[];for(e in d){c=d[e];b.push(this.view_model[e].setToDefault())}return b};return a})();Knockback.observables=function(c,d,a,b){return new Knockback.Observables(c,d,a,b)};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}Knockback.TriggeredObservable=(function(){function a(b,c){this.model=b;this.event_name=c;if(!this.model){throw new Error("Observable: model is missing")}if(!this.event_name){throw new Error("Observable: event_name is missing")}_.bindAll(this,"destroy","_onGetValue","_onValueChange","_onModelLoaded","_onModelUnloaded");if(Backbone.ModelRef&&(this.model instanceof Backbone.ModelRef)){this.model_ref=this.model;this.model_ref.retain();this.model_ref.bind("loaded",this._onModelLoaded);this.model_ref.bind("unloaded",this._onModelUnloaded);this.model=this.model_ref.getModel()}this._kb_value_observable=ko.observable();this._kb_observable=ko.dependentObservable(this._onGetValue);this._kb_observable.destroy=this.destroy;if(!this.model_ref||this.model_ref.isLoaded()){this._onModelLoaded(this.model)}return kb.wrappedObservable(this)}a.prototype.destroy=function(){this._kb_observable.dispose();this._kb_observable=null;this._kb_value_observable=null;if(this.model){this._onModelUnloaded(this.model)}if(this.model_ref){this.model_ref.unbind("loaded",this._onModelLoaded);this.model_ref.unbind("unloaded",this._onModelUnloaded);this.model_ref.release();this.model_ref=null}this.options=null;return this.view_model=null};a.prototype._onGetValue=function(){return this._kb_value_observable()};a.prototype._onModelLoaded=function(b){this.model=b;this.model.bind(this.event_name,this._onValueChange);return this._onValueChange()};a.prototype._onModelUnloaded=function(){if(this._kb_localizer&&this._kb_localizer.destroy){this._kb_localizer.destroy();this._kb_localizer=null}this.model.unbind(this.event_name,this._onValueChange);return this.model=null};a.prototype._onValueChange=function(){var b;b=this._kb_value_observable();if(b!==this.model){return this._kb_value_observable(this.model)}else{return this._kb_value_observable.valueHasMutated()}};return a})();Knockback.triggeredObservable=function(a,b){return new Knockback.TriggeredObservable(a,b)};var AttributeConnector;var __bind=function(a,b){return function(){return a.apply(b,arguments)}},__hasProp=Object.prototype.hasOwnProperty,__extends=function(d,b){for(var a in b){if(__hasProp.call(b,a)){d[a]=b[a]}}function c(){this.constructor=d}c.prototype=b.prototype;d.prototype=new c;d.__super__=b.prototype;return d};if(!this.Knockback){throw new Error("Knockback: Dependency alert! knockback_core.js must be included before this file")}AttributeConnector=(function(){function a(b,c,d){this.key=c;this.read_only=d;_.bindAll(this,"destroy","setModel");this._kb_observable=ko.observable();this._kb_observable.subscription=this._kb_observable.subscribe(__bind(function(f){var e;if(this.read_only){if(this.model){f=this.model.get(this.key);if(this._kb_observable()===f){return}this._kb_observable(f)}throw"Cannot write a value to a dependentObservable unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters."}else{if(this.model){e={};e[this.key]=f;return this.model.set(e)}}},this));this._kb_observable.destroy=this.destroy;this._kb_observable.setModel=this.setModel;if(b){this.setModel(b)}return kb.wrappedObservable(this)}a.prototype.destroy=function(){this.model=null;return this._kb_observable=null};a.prototype.setModel=function(b){if(b){this.model=b;return this._kb_observable(this.model.get(this.key))}else{return this.model=null}};return a})();Knockback.ViewModel_RCBase=(function(){function a(){this._kb_vm={};this._kb_vm.ref_count=1}a.prototype.__destroy=function(){return kb.vmReleaseObservables(this)};a.prototype.retain=function(){if(this._kb_vm.ref_count<=0){throw new Error("ViewModel: ref_count is corrupt: "+this._kb_vm.ref_count)}this._kb_vm.ref_count++;return this};a.prototype.release=function(){if(this._kb_vm.ref_count<=0){throw new Error("ViewModel: ref_count is corrupt: "+this._kb_vm.ref_count)}this._kb_vm.ref_count--;if(!this._kb_vm.ref_count){this.__destroy()}return this};a.prototype.refCount=function(){return this._kb_vm.ref_count};return a})();Knockback.ViewModel=(function(){__extends(a,kb.ViewModel_RCBase);function a(e,d,c){var g,f,h,b;if(d==null){d={}}a.__super__.constructor.apply(this,arguments);this._kb_vm.model=e;this._kb_vm.options=d;this._kb_vm.view_model=c;if(!this._kb_vm.model){throw new Error("ViewModel: model is missing")}_.bindAll(this,"_kb_vm_onModelChange","_kb_vm_onModelLoaded","_kb_vm_onModelUnloaded");if(!this._kb_vm.view_model){this._kb_vm.view_model=this}else{this._kb_vm.observables=[]}if(Backbone.ModelRef&&(this._kb_vm.model instanceof Backbone.ModelRef)){this._kb_vm.model_ref=this._kb_vm.model;this._kb_vm.model_ref.retain();this._kb_vm.model_ref.bind("loaded",this._kb_vm_onModelLoaded);this._kb_vm.model_ref.bind("unloaded",this._kb_vm_onModelUnloaded);this._kb_vm.model=this._kb_vm.model_ref.getModel()}if(!this._kb_vm.model_ref||this._kb_vm.model_ref.isLoaded()){this._kb_vm_onModelLoaded(this._kb_vm.model)}if(!this._kb_vm.options.internals&&!this._kb_vm.options.requires){return this}f=_.union((this._kb_vm.options.internals?this._kb_vm.options.internals:[]),(this._kb_vm.options.requires?this._kb_vm.options.requires:[]));if(!this._kb_vm.model_ref||this._kb_vm.model_ref.isLoaded()){f=_.difference(f,_.keys(this._kb_vm.model.attributes))}for(h=0,b=f.length;h<b;h++){g=f[h];this._updateAttributeObservor(this._kb_vm.model,g)}}a.prototype.__destroy=function(){var b;if(this._kb_vm.model){this._kb_vm.model.unbind("change",this._kb_vm_onModelChange);this._kb_vm.model=null}b=this._kb_vm.view_model;this._kb_vm.view_model=null;kb.vmReleaseObservables(b,this._kb_vm.observables);if(this._kb_vm.observables){return this._kb_vm.observables=null}};a.prototype._kb_vm_onModelLoaded=function(c){var d,b;this._kb_vm.model=c;this._kb_vm.model.bind("change",this._kb_vm_onModelChange);b=[];for(d in this._kb_vm.model.attributes){b.push(this._updateAttributeObservor(this._kb_vm.model,d))}return b};a.prototype._kb_vm_onModelUnloaded=function(c){var d,b;this._kb_vm.model.unbind("change",this._kb_vm_onModelChange);c=this._kb_vm.model;this._kb_vm.model=null;b=[];for(d in c.attributes){b.push(this._updateAttributeObservor(this._kb_vm.model,d))}return b};a.prototype._kb_vm_onModelChange=function(){var c,b;if(!this._kb_vm.model._changed){return}b=[];for(c in this._kb_vm.model.attributes){b.push((this._kb_vm.model.hasChanged(c)?this._updateAttributeObservor(this._kb_vm.model,c):void 0))}return b};a.prototype._updateAttributeObservor=function(b,d){var c;c=this._kb_vm.options.internals&&_.contains(this._kb_vm.options.internals,d)?"_"+d:d;if(this._kb_vm.view_model.hasOwnProperty(c)){if(this._kb_vm.view_model[c]){return this._kb_vm.view_model[c].setModel(b)}}else{if(this._kb_vm.observables){this._kb_vm.observables.push(c)}return this._kb_vm.view_model[c]=new AttributeConnector(b,d,this._kb_vm.options.read_only)}};return a})();Knockback.viewModel=function(c,b,a){return new Knockback.ViewModel(c,b,a)};