// Generated by CoffeeScript 1.3.3 /* knockback.js 0.15.3 (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. */(function(){var Backbone,Knockback,kb,ko,_,__hasProp={}.hasOwnProperty,__extends=function(child,parent){function ctor(){this.constructor=child}for(var key in parent)__hasProp.call(parent,key)&&(child[key]=parent[key]);return ctor.prototype=parent.prototype,child.prototype=new ctor,child.__super__=parent.prototype,child};_=!this._&&typeof require!="undefined"?require("underscore"):this._,Backbone=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,ko=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,Knockback=kb=this.Knockback=this.kb=typeof exports!="undefined"?exports:{},kb.VERSION="0.15.3",kb.locale_manager=void 0,kb.stats={collection_observables:0,view_models:0},kb.stats_on=!1,kb.utils={},kb.utils.legacyWarning=function(identifier,remove_version,message){var _base;return kb._legacy_warnings||(kb._legacy_warnings={}),(_base=kb._legacy_warnings)[identifier]||(_base[identifier]=0),kb._legacy_warnings[identifier]++,console.warn("warning: '"+identifier+"' has been deprecated (will be removed in Knockback "+remove_version+"). "+message+".")},kb.utils.wrappedObservable=function(instance,observable){if(arguments.length===1){if(!(instance&&instance.__kb&&instance.__kb.observable))throw"Knockback: instance is not wrapping an observable";return instance.__kb.observable}if(!instance)throw"Knockback: no instance for wrapping a observable";return instance.__kb||(instance.__kb={}),instance.__kb.observable&&instance.__kb.observable.__kb&&(instance.__kb.observable.__kb.instance=null),instance.__kb.observable=observable,observable&&(observable.__kb||(observable.__kb={}),observable.__kb.instance=instance),observable},kb.wrappedObservable=function(instance){return kb.utils.legacyWarning("kb.wrappedObservable","0.16.0","Please use kb.utils.wrappedObservable instead"),kb.utils.wrappedObservable(instance)},kb.utils.observableInstanceOf=function(observable,type){return observable?!observable.__kb||!observable.__kb.instance?!1:observable.__kb.instance instanceof type:!1},kb.utils.wrappedModel=function(view_model,model){if(arguments.length===1)return view_model&&view_model.__kb&&view_model.__kb.hasOwnProperty("model")?view_model.__kb.model:view_model;if(!view_model)throw"Knockback: no view_model for wrapping a model";return view_model.__kb||(view_model.__kb={}),view_model.__kb.model=model,model},kb.viewModelGetModel=kb.vmModel=function(view_model){return kb.utils.legacyWarning("kb.vmModel","0.16.0","Please use kb.utils.wrappedModel instead"),kb.utils.wrappedModel(view_model)},kb.utils.setToDefault=function(obj){var key,observable,_results;if(!obj)return;if(ko.isObservable(obj))return typeof obj.setToDefault=="function"?obj.setToDefault():void 0;if(_.isObject(obj)){_results=[];for(key in obj)observable=obj[key],_results.push(observable&&key!=="__kb"?kb.utils.setToDefault(observable):void 0);return _results}},kb.vmSetToDefault=function(view_model){return kb.utils.legacyWarning("kb.vmSetToDefault","0.16.0","Please use kb.utils.release instead"),kb.utils.setToDefault(view_model)},kb.utils.release=function(obj,keys_only){var key,value;if(!obj)return!1;if(!keys_only&&(ko.isObservable(obj)||obj instanceof kb.Observables||typeof obj.release=="function"||typeof obj.destroy=="function"))return obj.release?obj.release():obj.destroy?obj.destroy():obj.dispose&&obj.dispose(),!0;if(_.isObject(obj)&&typeof obj!="function"){for(key in obj){value=obj[key];if(!value||key==="__kb")continue;kb.utils.release(value)&&(obj[key]=null)}return!0}return!1},kb.vmRelease=function(view_model){return kb.utils.legacyWarning("kb.vmRelease","0.16.0","Please use kb.utils.release instead"),kb.utils.release(view_model)},kb.vmReleaseObservable=function(observable){return kb.utils.legacyWarning("kb.vmReleaseObservable","0.16.0","Please use kb.utils.release instead"),kb.utils.release(observable)},kb.utils.optionsCreateClear=function(options){return delete options.create,delete options.children,delete options.view_model,delete options.view_model_create},kb.utils.optionsCreateOverride=function(options,create_options){return kb.utils.optionsCreateClear(options),_.extend(options,create_options)},kb.RefCountable=function(){function RefCountable(){this.__kb||(this.__kb={}),this.__kb.ref_count=1}return RefCountable.extend=Backbone.Model.extend,RefCountable.prototype.__destroy=function(){},RefCountable.prototype.retain=function(){if(this.__kb.ref_count<=0)throw"RefCountable: ref_count is corrupt: "+this.__kb.ref_count;return this.__kb.ref_count++,this},RefCountable.prototype.release=function(){if(this.__kb.ref_count<=0)throw"RefCountable: ref_count is corrupt: "+this.__kb.ref_count;return this.__kb.ref_count--,this.__kb.ref_count||this.__destroy(),this},RefCountable.prototype.refCount=function(){return this.__kb.ref_count},RefCountable}(),kb.Store=function(){function Store(){this.keys=[],this.values=[]}return Store.prototype.destroy=function(){var index,value,_ref,_ref1;this.keys=null,_ref=this.values;for(index in _ref){value=_ref[index];if(!kb.utils.observableInstanceOf(value,kb.CollectionObservable))continue;this.values[index]=null;while(value.refCount()>0)value.release()}_ref1=this.values;for(index in _ref1){value=_ref1[index];if(!value)continue;this.values[index]=null;if(value instanceof kb.RefCountable)while(value.refCount()>0)value.release();else kb.utils.release(value)}return this.values=null},Store.prototype.registerValue=function(key,value){var index;return value instanceof kb.RefCountable&&value.retain(),index=_.indexOf(this.keys,key),index>=0?this.values[index]=value:(this.keys.push(key),this.values.push(value)),value},Store.prototype.resolveValue=function(key,create_fn,args){var index,value;index=_.indexOf(this.keys,key);if(index>=0){if(this.values[index]){if(!(this.values[index]instanceof kb.RefCountable&&this.values[index].refCount()<=0))return this.values[index]instanceof kb.RefCountable?this.values[index].retain():this.values[index];this.values[index]=null}}else index=this.keys.length,this.keys.push(key),this.values.push(void 0);return value=create_fn.apply(null,Array.prototype.slice.call(arguments,2)),this.keys[index]!==key?this.registerValue(key,value):this.values[index]||(value instanceof kb.RefCountable&&value.retain(),this.values[index]=value),value},Store.prototype.releaseValue=function(value){var index;if(value instanceof kb.RefCountable){value.release();if(value.refCount()>0)return;index=_.indexOf(this.values,value);if(index>=0)return this.values[index]=0;return}return},Store.prototype.addResolverToOptions=function(options,key){return _.extend(options,{store:this,store_key:key})},Store.resolveFromOptions=function(options,value){if(!options.store||!options.store_key)return;return options.store.registerValue(options.store_key,value)},Store}(),kb.CollectionObservable=function(_super){function CollectionObservable(collection,options){var bind_model_changes,observable,_this=this;options==null&&(options={});if(!collection)throw"CollectionObservable: collection is missing";CollectionObservable.__super__.constructor.apply(this,arguments),kb.stats_on&&kb.stats.collection_observables++,ko.isObservable(options)&&options.hasOwnProperty("indexOf")?(kb.utils.legacyWarning("kb.collectionObservable with an external ko.observableArray","0.16.0","Please use the kb.collectionObservable directly instead of passing a ko.observableArray"),observable=kb.utils.wrappedObservable(this,options),options=arguments[2]||{},bind_model_changes=!0):observable=kb.utils.wrappedObservable(this,ko.observableArray([])),options.store_skip_resolve||kb.Store.resolveFromOptions(options,kb.utils.wrappedObservable(this)),options.store?this.__kb.store=options.store:(this.__kb.store=new kb.Store,this.__kb.store_is_owned=!0);if(options.hasOwnProperty("view_model")){if(!options.view_model)throw"kb.CollectionObservable: options.view_model is empty";this.view_model_create_fn=options.view_model,this.view_model_create_with_new=!0}else if(options.hasOwnProperty("view_model_constructor")){if(!options.view_model_constructor)throw"kb.CollectionObservable: options.view_model_constructor is empty";kb.utils.legacyWarning("kb.collectionObservable option view_model_constructor","0.16.0","Please use view_model option instead"),this.view_model_create_fn=options.view_model_constructor,this.view_model_create_with_new=!0}else if(options.hasOwnProperty("view_model_create")){if(!options.view_model_create)throw"kb.CollectionObservable: options.view_model_create is empty";this.view_model_create_fn=options.view_model_create}else if(options.hasOwnProperty("create")){if(!options.create)throw"kb.CollectionObservable: options.create is empty";this.view_model_create_fn=options.create}return this.sort_attribute=options.sort_attribute,this.sorted_index=options.sorted_index,this.__kb._onCollectionReset=_.bind(this._onCollectionReset,this),this.__kb._onCollectionResort=_.bind(this._onCollectionResort,this),this.__kb._onModelAdd=_.bind(this._onModelAdd,this),this.__kb._onModelRemove=_.bind(this._onModelRemove,this),this.__kb._onModelChange=_.bind(this._onModelChange,this),bind_model_changes&&collection&&collection.bind("change",function(){return kb.utils.wrappedObservable(_this).valueHasMutated()}),observable.retain=_.bind(this.retain,this),observable.refCount=_.bind(this.refCount,this),observable.release=_.bind(this.release,this),observable.collection=_.bind(this.collection,this),observable.viewModelByModel=_.bind(this.viewModelByModel,this),observable.sortedIndex=_.bind(this.sortedIndex,this),observable.sortAttribute=_.bind(this.sortAttribute,this),observable.hasViewModels=_.bind(this.hasViewModels,this),observable.bind=_.bind(this.bind,this),observable.unbind=_.bind(this.unbind,this),observable.trigger=_.bind(this.trigger,this),this.collection(collection,{silent:!0,defer:options.defer}),observable}return __extends(CollectionObservable,_super),CollectionObservable.prototype.__destroy=function(){this.collection(null),this.hasViewModels()&&this.__kb.store_is_owned&&(this.__kb.store.destroy(),this.__kb.store=null),this.view_model_create_fn=null,this.__kb.collection=null,kb.utils.wrappedObservable(this,null),CollectionObservable.__super__.__destroy.apply(this,arguments);if(kb.stats_on)return kb.stats.collection_observables--},CollectionObservable.prototype.retain=function(){return CollectionObservable.__super__.retain.apply(this,arguments),kb.utils.wrappedObservable(this)},CollectionObservable.prototype.release=function(){var observable;return observable=kb.utils.wrappedObservable(this),CollectionObservable.__super__.release.apply(this,arguments),observable},CollectionObservable.prototype.collection=function(collection,options){var observable,_base,_base1;observable=kb.utils.wrappedObservable(this);if(arguments.length===0)return observable(),this.__kb.collection;if(collection===this.__kb.collection)return;this.__kb.collection&&(this._clear(),this._collectionUnbind(this.__kb.collection),typeof (_base=this.__kb.collection).release=="function"&&_base.release(),this.__kb.collection=null),this.__kb.collection=collection;if(this.__kb.collection)return typeof (_base1=this.__kb.collection).retain=="function"&&_base1.retain(),this._collectionBind(this.__kb.collection),this.sortedIndex(this.sorted_index,this.sort_attribute,options)},CollectionObservable.prototype.sortedIndex=function(sorted_index,sort_attribute,options){var _resync,_this=this;return options==null&&(options={}),sorted_index?(this.sorted_index=sorted_index,this.sort_attribute=sort_attribute):sort_attribute?(this.sort_attribute=sort_attribute,this.sorted_index=this._sortAttributeFn(sort_attribute)):(this.sort_attribute=null,this.sorted_index=null),_resync=function(){var observable;observable=kb.utils.wrappedObservable(_this);if(_this.__kb.collection.models.length===0&&observable().length===0)return;_this._collectionResync(!0);if(!options.silent)return _this.trigger("resort",observable())},options.defer?_.defer(_resync):_resync(),this},CollectionObservable.prototype.sortAttribute=function(sort_attribute,sorted_index,silent){return this.sortedIndex(sorted_index,sort_attribute,silent)},CollectionObservable.prototype.viewModelByModel=function(model){var id_attribute,observable;return this.hasViewModels()?(observable=kb.utils.wrappedObservable(this),id_attribute=model.hasOwnProperty(model.idAttribute)?model.idAttribute:"cid",_.find(observable(),function(test){return test.__kb.model[id_attribute]===model[id_attribute]})):null},CollectionObservable.prototype.hasViewModels=function(){return!!this.view_model_create_fn},CollectionObservable.prototype._collectionBind=function(collection){var event,_i,_j,_len,_len1,_ref,_ref1;if(!collection)return;collection.bind("reset",this.__kb._onCollectionReset),this.sorted_index||collection.bind("resort",this.__kb._onCollectionResort),_ref=["new","add"];for(_i=0,_len=_ref.length;_i<_len;_i++)event=_ref[_i],collection.bind(event,this.__kb._onModelAdd);_ref1=["remove","destroy"];for(_j=0,_len1=_ref1.length;_j<_len1;_j++)event=_ref1[_j],collection.bind(event,this.__kb._onModelRemove);return collection.bind("change",this.__kb._onModelChange)},CollectionObservable.prototype._collectionUnbind=function(collection){var event,_i,_j,_len,_len1,_ref,_ref1;if(!collection)return;collection.unbind("reset",this.__kb._onCollectionReset),this.sorted_index||collection.unbind("resort",this.__kb._onCollectionResort),_ref=["new","add"];for(_i=0,_len=_ref.length;_i<_len;_i++)event=_ref[_i],collection.unbind(event,this.__kb._onModelAdd);_ref1=["remove","destroy"];for(_j=0,_len1=_ref1.length;_j<_len1;_j++)event=_ref1[_j],collection.unbind(event,this.__kb._onModelRemove);return collection.unbind("change",this.__kb._onModelChange)},CollectionObservable.prototype._onCollectionReset=function(){return this._collectionResync()},CollectionObservable.prototype._onCollectionResort=function(model_or_models){var observable;if(this.sorted_index)throw"CollectionObservable: collection sorted_index unexpected";return _.isArray(model_or_models)?(observable=kb.utils.wrappedObservable(this),this.trigger("resort",observable())):this._onModelResort(model_or_models)},CollectionObservable.prototype._onModelAdd=function(model){var add_index,observable,target;return target=this.hasViewModels()?this._createTarget(model):model,observable=kb.utils.wrappedObservable(this),this.sorted_index?add_index=this.sorted_index(observable(),target):add_index=this.__kb.collection.indexOf(model),observable.splice(add_index,0,target),this.trigger("add",target,observable())},CollectionObservable.prototype._onModelRemove=function(model){var observable,target;target=this.hasViewModels()?this.viewModelByModel(model):model;if(!target)return;observable=kb.utils.wrappedObservable(this),observable.remove(target),this.trigger("remove",target,observable);if(this.hasViewModels())return this.__kb.store.releaseValue(target)},CollectionObservable.prototype._onModelChange=function(model){if(this.sorted_index&&(!this.sort_attribute||model.hasChanged(this.sort_attribute)))return this._onModelResort(model)},CollectionObservable.prototype._onModelResort=function(model){var new_index,observable,previous_index,sorted_targets,target;observable=kb.utils.wrappedObservable(this),target=this.hasViewModels()?this.viewModelByModel(model):model,previous_index=observable.indexOf(target),this.sorted_index?(sorted_targets=_.clone(observable()),sorted_targets.splice(previous_index,1),new_index=this.sorted_index(sorted_targets,target)):new_index=this.__kb.collection.indexOf(model);if(previous_index===new_index)return;return observable.splice(previous_index,1),observable.splice(new_index,0,target),this.trigger("resort",target,observable(),new_index)},CollectionObservable.prototype._clear=function(silent){var observable,target,targets,_i,_len,_results;observable=kb.utils.wrappedObservable(this),silent||this.trigger("remove",observable()),targets=observable.removeAll();if(this.hasViewModels()){_results=[];for(_i=0,_len=targets.length;_i<_len;_i++)target=targets[_i],_results.push(this.__kb.store.releaseValue(target));return _results}},CollectionObservable.prototype._collectionResync=function(silent){var add_index,model,observable,target,targets,_i,_len,_ref,_this=this;this._clear(silent),observable=kb.utils.wrappedObservable(this);if(this.sorted_index){targets=[],_ref=this.__kb.collection.models;for(_i=0,_len=_ref.length;_i<_len;_i++)model=_ref[_i],target=this._createTarget(model),add_index=this.sorted_index(targets,target),targets.splice(add_index,0,target)}else targets=this.hasViewModels()?_.map(this.__kb.collection.models,function(model){return _this._createTarget(model)}):_.clone(this.__kb.collection.models);observable(targets);if(!silent)return this.trigger("add",observable())},CollectionObservable.prototype._sortAttributeFn=function(sort_attribute){return this.hasViewModels()?function(view_models,model){return _.sortedIndex(view_models,model,function(test){return kb.utils.wrappedModel(test).get(sort_attribute)})}:function(models,model){return _.sortedIndex(models,model,function(test){return test.get(sort_attribute)})}},CollectionObservable.prototype._createTarget=function(model){var create_fn,_this=this;return create_fn=function(){var observable,options,view_model;return options=_this.__kb.store.addResolverToOptions({},model),observable=kb.utils.wrappedObservable(_this),view_model=_this.view_model_create_with_new?new _this.view_model_create_fn(model,options,observable):_this.view_model_create_fn(model,options,observable),kb.utils.wrappedModel(view_model,model),view_model},this.hasViewModels()?this.__kb.store.resolveValue(model,create_fn):model},CollectionObservable}(kb.RefCountable),__extends(kb.CollectionObservable.prototype,Backbone.Events),kb.collectionObservable=function(collection,options,legacy){return new kb.CollectionObservable(collection,options,legacy)},kb.sortedIndexWrapAttr=kb.siwa=function(attribute_name,wrapper_constructor){return function(models,model){return _.sortedIndex(models,model,function(test){return new wrapper_constructor(kb.utils.wrappedModel(test).get(attribute_name))})}},kb.DefaultWrapper=function(){function DefaultWrapper(target_observable,default_value_observable){var observable,_this=this;return this.default_value_observable=default_value_observable,this.__kb={},observable=kb.utils.wrappedObservable(this,ko.dependentObservable({read:function(){var current_default,current_target;return current_target=ko.utils.unwrapObservable(target_observable()),current_default=ko.utils.unwrapObservable(_this.default_value_observable),current_target?current_target:current_default},write:function(value){return target_observable(value)}})),observable.destroy=_.bind(this.destroy,this),observable.setToDefault=_.bind(this.setToDefault,this),observable}return DefaultWrapper.prototype.destroy=function(){return kb.utils.wrappedObservable(this,null),this.default_value=null},DefaultWrapper.prototype.setToDefault=function(){var observable;return observable=kb.utils.wrappedObservable(this),observable(this.default_value_observable)},DefaultWrapper}(),kb.defaultWrapper=function(target,default_value){return new kb.DefaultWrapper(target,default_value)},kb.toFormattedString=function(format){var arg,args,index,parameter_index,result,value;result=format.slice(),args=Array.prototype.slice.call(arguments,1);for(index in args){arg=args[index],value=ko.utils.unwrapObservable(arg),value||(value=""),parameter_index=format.indexOf("{"+index+"}");while(parameter_index>=0)result=result.replace("{"+index+"}",value),parameter_index=format.indexOf("{"+index+"}",parameter_index+1)}return result},kb.parseFormattedString=function(string,format){var count,format_indices_to_matched_indices,index,match_index,matches,parameter_count,parameter_index,positions,regex,regex_string,results,sorted_positions,_i,_results;regex_string=format.slice(),index=0,parameter_count=0,positions={};while(regex_string.search("\\{"+index+"\\}")>=0){parameter_index=format.indexOf("{"+index+"}");while(parameter_index>=0)regex_string=regex_string.replace("{"+index+"}","(.*)"),positions[parameter_index]=index,parameter_count++,parameter_index=format.indexOf("{"+index+"}",parameter_index+1);index++}count=index,regex=new RegExp(regex_string),matches=regex.exec(string),matches&&matches.shift();if(!matches||matches.length!==parameter_count)return _.map(function(){_results=[];for(var _i=1;1<=count?_i<=count:_i>=count;1<=count?_i++:_i--)_results.push(_i);return _results}.apply(this),function(){return""});sorted_positions=_.sortBy(_.keys(positions),function(parameter_index,format_index){return parseInt(parameter_index,10)}),format_indices_to_matched_indices={};for(match_index in sorted_positions){parameter_index=sorted_positions[match_index],index=positions[parameter_index];if(format_indices_to_matched_indices.hasOwnProperty(index))continue;format_indices_to_matched_indices[index]=match_index}results=[],index=0;while(index0&¤t_value.release(),CollectionAttributeConnector.__super__.destroy.apply(this,arguments)},CollectionAttributeConnector.prototype.update=function(){var current_value,model,value,_this=this;model=kb.utils.wrappedModel(this);if(!model)return;value=model.get(this.key),current_value=this.__kb.value_observable();if(!current_value)return this.options.store?this.__kb.value_observable(this.options.store.resolveValue(value,function(){return kb.collectionObservable(value,_this.options)})):this.__kb.value_observable(kb.collectionObservable(value,this.options));if(current_value.collection()!==value)return current_value.collection(value),this.__kb.value_observable.valueHasMutated()},CollectionAttributeConnector.prototype.read=function(){var current_value;return current_value=this.__kb.value_observable(),current_value?current_value():void 0},CollectionAttributeConnector}(kb.AttributeConnector),kb.collectionAttributeConnector=function(model,key,options){return new kb.CollectionAttributeConnector(model,key,options)},kb.ViewModelAttributeConnector=function(_super){function ViewModelAttributeConnector(){return ViewModelAttributeConnector.__super__.constructor.apply(this,arguments),kb.utils.wrappedObservable(this)}return __extends(ViewModelAttributeConnector,_super),ViewModelAttributeConnector.prototype.destroy=function(){var current_value;return current_value=this.__kb.value_observable(),current_value&&typeof current_value.refCount=="function"&¤t_value.refCount()>0&¤t_value.release(),ViewModelAttributeConnector.__super__.destroy.apply(this,arguments)},ViewModelAttributeConnector.prototype.update=function(){var current_value,model,value,view_model_options,_this=this;model=kb.utils.wrappedModel(this);if(!model)return;value=model.get(this.key),current_value=this.__kb.value_observable();if(!current_value)return view_model_options=this.options.options?_.clone(this.options.options):{},view_model_options.store?this.__kb.value_observable(view_model_options.store.resolveValue(value,function(){return _this.options.view_model?new _this.options.view_model(value,view_model_options):_this.options.view_model_create(value,view_model_options)})):this.__kb.value_observable(this.options.view_model?new this.options.view_model(value,view_model_options):this.options.view_model_create(value,view_model_options));if(!current_value.model||typeof current_value.model!="function")throw"kb.viewModelAttributeConnector: unknown how to model a view model";if(current_value.model()!==value)return current_value.model(value),this.__kb.value_observable.valueHasMutated()},ViewModelAttributeConnector}(kb.AttributeConnector),kb.viewModelAttributeConnector=function(model,key,options){return new kb.ViewModelAttributeConnector(model,key,options)},kb.ViewModel=function(_super){function ViewModel(model,options){var key,missing,_i,_len;options==null&&(options={}),ViewModel.__super__.constructor.apply(this,arguments),kb.stats_on&&kb.stats.view_models++,options.store_skip_resolve||kb.Store.resolveFromOptions(options,this),options.store?this.__kb.store=options.store:(this.__kb.store=new kb.Store,this.__kb.store_is_owned=!0),this.__kb._onModelChange=_.bind(this._onModelChange,this),this.__kb._onModelLoaded=_.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=_.bind(this._onModelUnloaded,this),this.__kb.internals=options.internals,this.__kb.requires=options.requires,this.__kb.children=options.children,this.__kb.create=options.create,this.__kb.read_only=options.read_only,kb.utils.wrappedModel(this,model),Backbone.ModelRef&&model instanceof Backbone.ModelRef&&(this.__kb.model_ref=model,this.__kb.model_ref.retain(),kb.utils.wrappedModel(this,this.__kb.model_ref.getModel()),this.__kb.model_ref.bind("loaded",this.__kb._onModelLoaded),this.__kb.model_ref.bind("unloaded",this.__kb._onModelUnloaded)),this.__kb.model&&this._onModelLoaded(this.__kb.model);if(!this.__kb.internals&&!this.__kb.requires)return this;missing=_.union(this.__kb.internals?this.__kb.internals:[],this.__kb.requires?this.__kb.requires:[]);if(!this.__kb.model_ref||this.__kb.model_ref.isLoaded())missing=_.difference(missing,_.keys(this.__kb.model.attributes));for(_i=0,_len=missing.length;_i<_len;_i++)key=missing[_i],this._updateAttributeConnector(this.__kb.model,key)}return __extends(ViewModel,_super),ViewModel.prototype.__destroy=function(){var model;model=this.__kb.model,kb.utils.wrappedModel(this,null),this._modelUnbind(model),this.__kb.store_is_owned&&this.__kb.store.destroy(),this.__kb.store=null,kb.utils.release(this,!0),ViewModel.__super__.__destroy.apply(this,arguments);if(kb.stats_on)return kb.stats.view_models--},ViewModel.prototype.model=function(new_model){var model;model=kb.utils.wrappedModel(this);if(arguments.length===0)return model;if(new_model===model)return;model&&this._onModelUnloaded(model);if(new_model)return this._onModelLoaded(new_model)},ViewModel.prototype._modelBind=function(model){if(!model)return;model.bind("change",this.__kb._onModelChange);if(Backbone.RelationalModel&&model instanceof Backbone.RelationalModel)return model.bind("add",this.__kb._onModelChange),model.bind("remove",this.__kb._onModelChange),model.bind("update",this.__kb._onModelChange)},ViewModel.prototype._modelUnbind=function(model){if(!model)return;model.unbind("change",this.__kb._onModelChange);if(Backbone.RelationalModel&&model instanceof Backbone.RelationalModel)return model.unbind("add",this.__kb._onModelChange),model.unbind("remove",this.__kb._onModelChange),model.unbind("update",this.__kb._onModelChange)},ViewModel.prototype._onModelLoaded=function(model){var key,_results;kb.utils.wrappedModel(this,model),this._modelBind(model),_results=[];for(key in this.__kb.model.attributes)_results.push(this._updateAttributeConnector(this.__kb.model,key));return _results},ViewModel.prototype._onModelUnloaded=function(model){var key,_results;this._modelUnbind(model),kb.utils.wrappedModel(this,null),_results=[];for(key in model.attributes)_results.push(this._updateAttributeConnector(null,key));return _results},ViewModel.prototype._onModelChange=function(){var key,_results,_results1;if(this.__kb.model._changed){_results=[];for(key in this.__kb.model.attributes)_results.push(this.__kb.model.hasChanged(key)?this._updateAttributeConnector(this.__kb.model,key):void 0);return _results}if(this.__kb.model.changed){_results1=[];for(key in this.__kb.model.changed)_results1.push(this._updateAttributeConnector(this.__kb.model,key));return _results1}},ViewModel.prototype._updateAttributeConnector=function(model,key){var vm_key;return vm_key=this.__kb.internals&&_.contains(this.__kb.internals,key)?"_"+key:key,this[vm_key]=kb.AttributeConnector.createOrUpdate(this[vm_key],model,key,this._createOptions(key))},ViewModel.prototype._createOptions=function(key){var options;if(this.__kb.children){if(this.__kb.children.hasOwnProperty(key))return options=this.__kb.children[key],typeof options=="function"&&(options={view_model:options}),options.options={read_only:this.__kb.read_only,store:this.__kb.store},options;if(this.__kb.children.hasOwnProperty("create"))return{create:this.__kb.children.create,options:{read_only:this.__kb.read_only,store:this.__kb.store}}}else if(this.__kb.create)return{create:this.__kb.create,options:{read_only:this.__kb.read_only,store:this.__kb.store}};return{read_only:this.__kb.read_only,store:this.__kb.store}},ViewModel}(kb.RefCountable),kb.viewModel=function(model,options){return new kb.ViewModel(model,options)}}).call(this);