/* knockback-core.js 0.19.0 Copyright (c) 2011-2014 Kevin Malakoff. License: MIT (http://www.opensource.org/licenses/mit-license.php) Source: https://github.com/kmalakoff/knockback Dependencies: Knockout.js, Backbone.js, and Underscore.js (or LoDash.js). Optional dependencies: Backbone.ModelRef.js and BackboneORM. */ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("knockout"),require("backbone"),require("underscore"),function(){try{return require("jquery")}catch(e){}}()):"function"==typeof define&&define.amd?define(["knockout","backbone","underscore"],function(r,n,o){return t(r,n,o,e.jQuery)}):"object"==typeof exports?exports.kb=t(require("knockout"),require("backbone"),require("underscore"),function(){try{return require("jquery")}catch(e){}}()):e.kb=t(e.ko,e.Backbone,e._,e.jQuery)}(this,function(e,t,r,n){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){r(1),r(2),r(3),r(4),r(5),r(6),r(7),r(8),r(9),r(10),r(11),e.exports=r(12)},function(e,t,r){var n,o,i,s,a,l,u=[].indexOf||function(e){for(var t=0,r=this.length;r>t;t++)if(t in this&&this[t]===e)return t;return-1};s=r(5),l=s._,a=s.ko,i=0,n=-1,o=1,s.compare=function(e,t){return l.isString(e)?e.localeCompare(""+t):l.isString(t)?t.localeCompare(""+e):e===t?i:t>e?n:o},s.CollectionObservable=function(){function e(e,t){return s.ignore(function(r){return function(){var n,o,i;return!l.isUndefined(t)||e instanceof s.Collection?l.isArray(e)&&(e=new s.Collection(e)):(i=[new s.Collection,e],e=i[0],t=i[1]),t||(t={}),o=s.utils.wrappedObservable(r,a.observableArray([])),o.__kb_is_co=!0,r.in_edit=0,r.__kb||(r.__kb={}),r.__kb._onCollectionChange=l.bind(r._onCollectionChange,r),t=s.utils.collapseOptions(t),t.auto_compact&&(r.auto_compact=!0),r._comparator=a.observable(t.sort_attribute?r._attributeComparator(t.sort_attribute):t.comparator),r._filters=a.observableArray(t.filters?l.isArray(t.filters)?t.filters:t.filters?[t.filters]:void 0:[]),n=r.create_options={store:s.Store.useOptionsOrCreate(t,e,o)},r.path=t.path,n.factory=s.utils.wrappedFactory(o,r._shareOrCreateFactory(t)),n.path=s.utils.pathJoin(t.path,"models"),n.creator=n.factory.creatorForPath(null,n.path),n.creator&&(r.models_only=n.creator.models_only),s.publishMethods(o,r,["destroy","shareOptions","filters","comparator","sortAttribute","viewModelByModel","hasViewModels"]),r._collection=a.observable(e),o.collection=r.collection=a.dependentObservable({read:function(){return r._collection()},write:function(e){return s.ignore(function(){var t;if((t=r._collection())!==e)return t&&t.unbind("all",r.__kb._onCollectionChange),e&&e.bind("all",r.__kb._onCollectionChange),r._collection(e)})}}),e&&e.bind("all",r.__kb._onCollectionChange),r._mapper=a.dependentObservable(function(){var e,t,n,i,u,c,d,p;if(e=r._comparator(),i=r._filters())for(d=0,p=i.length;p>d;d++)n=i[d],a.utils.unwrapObservable(n);return t=r._collection(),r.in_edit?void 0:(o=s.utils.wrappedObservable(r),t&&(u=t.models),u&&0!==t.models.length?(u=l.filter(u,function(e){return!i.length||r._selectModel(e)}),c=e?l.map(u,function(e){return r._createViewModel(e)}).sort(e):r.models_only?i.length?u:u.slice():l.map(u,function(e){return r._createViewModel(e)})):c=[],r.in_edit++,o(c),r.in_edit--)}),o.subscribe(l.bind(r._onObservableArrayChange,r)),!s.statistics||s.statistics.register("CollectionObservable",r),o}}(this))}return e.extend=s.extend,e.prototype.destroy=function(){var e,t,r;return r=s.utils.wrappedObservable(this),t=s.peek(this._collection),t&&(t.unbind("all",this.__kb._onCollectionChange),e=s.peek(r),e.splice(0,e.length)),this.collection.dispose(),this._collection=r.collection=this.collection=null,this._mapper.dispose(),this._mapper=null,s.release(this._filters),this._filters=null,this._comparator(null),this._comparator=null,this.create_options=null,r.collection=null,s.utils.wrappedDestroy(this),!s.statistics||s.statistics.unregister("CollectionObservable",this)},e.prototype.shareOptions=function(){var e;return e=s.utils.wrappedObservable(this),{store:s.utils.wrappedStore(e),factory:s.utils.wrappedFactory(e)}},e.prototype.filters=function(e){return this._filters(e?l.isArray(e)?e:[e]:[])},e.prototype.comparator=function(e){return this._comparator(e)},e.prototype.sortAttribute=function(e){return this._comparator(e?this._attributeComparator(e):null)},e.prototype.viewModelByModel=function(e){var t;return this.models_only?null:(t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",l.find(s.peek(s.utils.wrappedObservable(this)),function(r){var n;return(null!=r&&null!=(n=r.__kb)?n.object:void 0)?r.__kb.object[t]===e[t]:!1}))},e.prototype.hasViewModels=function(){return!this.models_only},e.prototype.compact=function(){return s.ignore(function(e){return function(){var t;return t=s.utils.wrappedObservable(e),s.utils.wrappedStoreIsOwned(t)?(s.utils.wrappedStore(t).clear(),e._collection.notifySubscribers(e._collection())):void 0}}(this))},e.prototype._shareOrCreateFactory=function(e){var t,r,n,o;if(t=s.utils.pathJoin(e.path,"models"),n=e.factories,(o=e.factory)&&(r=o.creatorForPath(null,t))&&(!n||n.models===r)){if(!n)return o;if(o.hasPathMappings(n,e.path))return o}return o=new s.Factory(e.factory),n&&o.addPathMappings(n,e.path),o.creatorForPath(null,t)||(e.hasOwnProperty("models_only")?e.models_only?o.addPathMapping(t,{models_only:!0}):o.addPathMapping(t,s.ViewModel):e.view_model?o.addPathMapping(t,e.view_model):e.create?o.addPathMapping(t,{create:e.create}):o.addPathMapping(t,s.ViewModel)),o},e.prototype._onCollectionChange=function(e,t){return s.ignore(function(r){return function(){var n,o,i,a;if(!r.in_edit)switch(e){case"reset":r.auto_compact?r.compact():r._collection.notifySubscribers(r._collection());break;case"sort":case"resort":r._collection.notifySubscribers(r._collection());break;case"new":case"add":if(!r._selectModel(t))return;if(i=s.utils.wrappedObservable(r),n=r._collection(),-1===n.indexOf(t))return;if(a=r.viewModelByModel(t))return;r.in_edit++,a=r._createViewModel(t),(o=r._comparator())?(i().push(a),i.sort(o)):i.splice(n.indexOf(t),0,a),r.in_edit--;break;case"remove":case"destroy":r._onModelRemove(t);break;case"change":r._selectModel(t)?(a=r.models_only?t:r.viewModelByModel(t),a?(o=r._comparator())&&(i=s.utils.wrappedObservable(r),r.in_edit++,i.sort(o),r.in_edit--):r._onCollectionChange("add",t)):r._onModelRemove(t)}}}(this))},e.prototype._onModelRemove=function(e){var t,r;return(r=this.models_only?e:this.viewModelByModel(e))?(t=s.utils.wrappedObservable(this),this.in_edit++,t.remove(r),this.in_edit--):void 0},e.prototype._onObservableArrayChange=function(e){return s.ignore(function(t){return function(){var r,n,o,i,a,u,c,d,p;if(!t.in_edit&&(t.models_only&&(!e.length||s.utils.hasModelSignature(e[0]))||!t.models_only&&(!e.length||l.isObject(e[0])&&!s.utils.hasModelSignature(e[0]))||s._throwUnexpected(t,"incorrect type passed"),a=s.utils.wrappedObservable(t),r=s.peek(t._collection),n=s.peek(t._filters).length,r)){if(c=e,t.models_only)i=l.filter(e,function(e){return!n||t._selectModel(e)});else for(!n||(c=[]),i=[],d=0,p=e.length;p>d;d++){if(u=e[d],o=s.utils.wrappedObject(u),n){if(!t._selectModel(o))continue;c.push(u)}t.create_options.store.findOrReplace(o,t.create_options.creator,u),i.push(o)}t.in_edit++,e.length===c.length||a(c),l.isEqual(r.models,i)||r.reset(i),t.in_edit--}}}(this))},e.prototype._attributeComparator=function(e){var t;return t=function(t,r){var n;return n=a.utils.unwrapObservable(e),s.compare(t.get(n),r.get(n))},this.models_only?t:function(e,r){return t(s.utils.wrappedModel(e),s.utils.wrappedModel(r))}},e.prototype._createViewModel=function(e){return this.models_only?e:this.create_options.store.findOrCreate(e,this.create_options)},e.prototype._selectModel=function(e){var t,r,n,o,i;for(r=s.peek(this._filters),n=0,o=r.length;o>n;n++)if(t=r[n],t=s.peek(t),l.isFunction(t)){if(!t(e))return!1}else if(l.isArray(t)){if(i=e.id,u.call(t,i)<0)return!1}else if(e.id!==t)return!1;return!0},e}(),s.collectionObservable=function(e,t){return new s.CollectionObservable(e,t)}},function(e,t,r){var n,o,i,s=function(e,t){return function(){return e.apply(t,arguments)}},a=[].indexOf||function(e){for(var t=0,r=this.length;r>t;t++)if(t in this&&this[t]===e)return t;return-1};n=r(5),i=n._,o=n.ko,n.EventWatcher=function(){function e(e,t,r){this._onModelUnloaded=s(this._onModelUnloaded,this),this._onModelLoaded=s(this._onModelLoaded,this),this.__kb||(this.__kb={}),this.__kb.callbacks={},this.__kb._onModelLoaded=i.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=i.bind(this._onModelUnloaded,this),r&&this.registerCallbacks(t,r),e?this.emitter(e):this.ee=null}return e.useOptionsOrCreate=function(e,t,r,o){return e.event_watcher?(e.event_watcher.emitter()!==t&&e.event_watcher.model_ref!==t&&n._throwUnexpected(this,"emitter not matching"),n.utils.wrappedEventWatcher(r,e.event_watcher).registerCallbacks(r,o)):(n.utils.wrappedEventWatcherIsOwned(r,!0),n.utils.wrappedEventWatcher(r,new n.EventWatcher(t)).registerCallbacks(r,o))},e.prototype.destroy=function(){return this.emitter(null),this.__kb.callbacks=null,n.utils.wrappedDestroy(this)},e.prototype.emitter=function(e){var t,r,o,i,s,a,l,u;if(0===arguments.length||this.ee===e)return this.ee;this.model_ref&&(this.model_ref.unbind("loaded",this.__kb._onModelLoaded),this.model_ref.unbind("unloaded",this.__kb._onModelUnloaded),this.model_ref.release(),this.model_ref=null),n.Backbone&&n.Backbone.ModelRef&&e instanceof n.Backbone.ModelRef?(this.model_ref=e,this.model_ref.retain(),this.model_ref.bind("loaded",this.__kb._onModelLoaded),this.model_ref.bind("unloaded",this.__kb._onModelUnloaded),e=this.model_ref.model()):delete this.model_ref,s=this.ee,this.ee=e,u=this.__kb.callbacks;for(r in u)for(t=u[r],s&&s.unbind(r,t.fn),e&&this.ee.bind(r,t.fn),i=t.list,a=0,l=i.length;l>a;a++)o=i[a],o.emitter&&o.emitter(this.ee);return e},e.prototype.registerCallbacks=function(e,t){var r,s,l,u,c,d,p,_;for(e||n._throwMissing(this,"obj"),t||n._throwMissing(this,"info"),u=t.event_selector?t.event_selector:"change",l=u.split(" "),p=0,_=l.length;_>p;p++)s=l[p],s&&(r=this.__kb.callbacks[s],r||(d=[],r={list:d,fn:function(){return function(e){var t,r,i;for(r=0,i=d.length;i>r;r++)if(t=d[r],t.update&&!t.rel_fn){if(e&&t.key&&e.hasChanged&&!e.hasChanged(o.utils.unwrapObservable(t.key)))continue;!n.statistics||n.statistics.addModelEvent({name:s,model:e,key:t.key,path:t.path}),t.update()}return null}}(this)},this.__kb.callbacks[s]=r,this.ee&&this.ee.bind(s,r.fn)),c=i.defaults({obj:e},t),r.list.push(c));this.ee&&(a.call(l,"change")>=0&&(c.unbind_fn=n.orm.bind(this.ee,c.key,c.update,c.path)),c.emitter(this.ee)&&c.emitter)},e.prototype.releaseCallbacks=function(e){var t,r,o,i,s,a;if(this.__kb.callbacks&&this.ee){s=this.__kb.callbacks;for(r in s){t=s[r],a=t.list;for(o in a)if(i=a[o],i.obj===e)return t.list.splice(o,1),i.unbind_fn&&(i.unbind_fn(),i.unbind_fn=null),void(!n.wasReleased(e)&&i.emitter&&i.emitter(null))}}},e.prototype._onModelLoaded=function(e){var t,r,o,i,s,a,l;this.ee=e,a=this.__kb.callbacks;for(r in a)for(t=a[r],e.bind(r,t.fn),l=t.list,i=0,s=l.length;s>i;i++)o=l[i],o.unbind_fn=n.orm.bind(e,o.key,o.update,o.path),o.emitter&&o.emitter(e)},e.prototype._onModelUnloaded=function(e){var t,r,n,o,i,s,a;this.ee=null,a=this.__kb.callbacks;for(r in a)for(t=a[r],e.unbind(r,t.fn),o=t.list,i=0,s=o.length;s>i;i++)n=o[i],n.unbind_fn&&(n.unbind_fn(),n.unbind_fn=null),n.emitter&&n.emitter(null)},e}(),n.emitterObservable=function(e,t){return new n.EventWatcher(e,t)}},function(e,t,r){var n,o;n=r(5),o=n._,n.Factory=function(){function e(e){this.parent_factory=e,this.paths={}}return e.useOptionsOrCreate=function(e,t,r){var o;return e.factory&&(!e.factories||e.factories&&e.factory.hasPathMappings(e.factories,r))?n.utils.wrappedFactory(t,e.factory):(o=n.utils.wrappedFactory(t,new n.Factory(e.factory)),e.factories&&o.addPathMappings(e.factories,r),o)},e.prototype.hasPath=function(e){return this.paths.hasOwnProperty(e)||this.parent_factory&&this.parent_factory.hasPath(e)},e.prototype.addPathMapping=function(e,t){return this.paths[e]=t},e.prototype.addPathMappings=function(e,t){var r,o;for(o in e)r=e[o],this.paths[n.utils.pathJoin(t,o)]=r},e.prototype.hasPathMappings=function(e,t){var r,o,i,s;r=!0;for(s in e)o=e[s],r&=(i=this.creatorForPath(null,n.utils.pathJoin(t,s)))&&o===i;return r},e.prototype.creatorForPath=function(e,t){var r;return(r=this.paths[t])?r.view_model?r.view_model:r:this.parent_factory&&(r=this.parent_factory.creatorForPath(e,t))?r:null},e}()},function(e,t,r){(function(e){var t,n,o,i,s,a,l;s=null!=s?s:e,n=r(5),a=n._,o=n.ko,t=n.$,n.RECUSIVE_AUTO_INJECT=!0,o.bindingHandlers.inject={init:function(e,t,r,i){return n.Inject.inject(o.utils.unwrapObservable(t()),i,e,t,r)}},n.Inject=function(){function e(){}return e.inject=function(e,t,r,i,s,l){var u,c,d;return u=function(e){var o,u,c;if(a.isFunction(e))t=new e(t,r,i,s),n.releaseOnNodeRemove(t,r);else{e.view_model&&(t=new e.view_model(t,r,i,s),n.releaseOnNodeRemove(t,r));for(o in e)c=e[o],"view_model"!==o&&("create"===o?c(t,r,i,s):a.isObject(c)&&!a.isFunction(c)?(u=l||c&&c.create?{}:t,t[o]=n.Inject.inject(c,u,r,i,s,!0)):t[o]=c)}return t},l?u(e):(c=(d=o.dependentObservable(function(){return u(e)}))(),d.dispose(),c)},e.injectViewModels=function(e){var t,r,o,i,l,u,c,d,p,_;for(d=[],u=function(e){var t,r,n,o,i;for(e.__kb_injected||e.attributes&&(t=a.find(e.attributes,function(e){return"kb-inject"===e.name}))&&(e.__kb_injected=!0,d.push({el:e,view_model:{},binding:t.value})),i=e.childNodes,n=0,o=i.length;o>n;n++)r=i[n],u(r)},!e&&(null!=s?s.document:void 0)&&(e=s.document),u(e),p=0,_=d.length;_>p;p++)r=d[p],(l=r.binding)&&(l.search(/[:]/)<0||(l="{"+l+"}"),i=new Function("","return ( "+l+" )")(),i||(i={}),!i.options||(c=i.options,delete i.options),c||(c={}),r.view_model=n.Inject.inject(i,r.view_model,r.el,null,null,!0),t=r.view_model.afterBinding||c.afterBinding,o=r.view_model.beforeBinding||c.beforeBinding),o&&o(r.view_model,r.el,c),n.applyBindings(r.view_model,r.el,c),t&&t(r.view_model,r.el,c);return d},e}(),l=o.applyBindings,o.applyBindings=function(e,t){var r;return r=n.RECUSIVE_AUTO_INJECT?n.injectViewModels(t):[],r.length?void 0:l.apply(this,arguments)},n.injectViewModels=n.Inject.injectViewModels,"undefined"!=typeof document&&null!==document&&(t?t(function(){return n.injectViewModels()}):(i=function(){return"complete"!==document.readyState?setTimeout(i,0):n.injectViewModels()})())}).call(t,function(){return this}())},function(e,t,r){(function(t){var n,o,i,s,a,l;a=null!=a?a:t,s=r(13),o=function(e,t){var r,n;for(r in t)n=t[r],e[r]=n;return e};var u=function(){},c=function(e,t,r){var n;return n=t&&t.hasOwnProperty("constructor")?t.constructor:function(){e.apply(this,arguments)},o(n,e),u.prototype=e.prototype,n.prototype=new u,t&&o(n.prototype,t),r&&o(n,r),n.prototype.constructor=n,n.__super__=e.prototype,n},d=function(e,t){var r=c(this,e,t);return r.extend=this.extend,r};e.exports=i=function(){function e(){}var t;return e.VERSION="0.18.6",e.TYPE_UNKNOWN=0,e.TYPE_SIMPLE=1,e.TYPE_ARRAY=2,e.TYPE_MODEL=3,e.TYPE_COLLECTION=4,e.wasReleased=function(e){return!e||e.__kb_released},e.isReleaseable=function(t,r){var n,o;if(null==r&&(r=0),!t||t!==Object(t)||t.__kb_released)return!1;if(s.isObservable(t)||t instanceof e.ViewModel)return!0;if("function"==typeof t||t instanceof e.Model||t instanceof e.Collection)return!1;if("function"==typeof t.dispose||"function"==typeof t.destroy||"function"==typeof t.release)return!0;if(1>r)for(n in t)if(o=t[n],"__kb"!==n&&e.isReleaseable(o,r+1))return!0;return!1},e.release=function(t){var r,n,o;if(e.isReleaseable(t))if(l.isArray(t))for(n in t)o=t[n],e.isReleaseable(o)&&(t[n]=null,e.release(o));else if(t.__kb_released=!0,s.isObservable(t)&&l.isArray(r=e.peek(t))){if(t.__kb_is_co||t.__kb_is_o&&t.valueType()===e.TYPE_COLLECTION)t.destroy?t.destroy():t.dispose&&t.dispose();else if(r.length)for(n in r)o=r[n],e.isReleaseable(o)&&(r[n]=null,e.release(o))}else"function"==typeof t.release?t.release():"function"==typeof t.destroy?t.destroy():"function"==typeof t.dispose?t.dispose():s.isObservable(t)||this.releaseKeys(t)},e.releaseKeys=function(t){var r,n;for(r in t)n=t[r],"__kb"!==r&&e.isReleaseable(n)&&(t[r]=null,e.release(n))},e.releaseOnNodeRemove=function(t,r){return t||e._throwUnexpected(this,"missing view model"),r||e._throwUnexpected(this,"missing node"),s.utils.domNodeDisposal.addDisposeCallback(r,function(){return e.release(t)})},e.renderTemplate=function(t,r,n){var o,i,l;return null==n&&(n={}),(o=null!=a?a.document:void 0)?(i=o.createElement("div"),l=s.renderTemplate(t,r,n,i,"replaceChildren"),1===i.children.length&&(i=i.children[0]),e.releaseOnNodeRemove(r,i),l.dispose(),r.afterRender&&!n.afterRender&&r.afterRender(i),i):"undefined"!=typeof console&&null!==console?console.log("renderTemplate: document is undefined"):void 0},e.applyBindings=function(t,r){return s.applyBindings(t,r),e.releaseOnNodeRemove(t,r)},e.getValue=function(t,r,n){return t?l.isFunction(t[r])&&e.orm.useFunction(t,r)?t[r]():n?t.get.apply(t,l.map([r].concat(n),function(t){return e.peek(t)})):t.get(r):void 0},e.setValue=function(t,r,n){var o;if(t)return l.isFunction(t[r])&&e.orm.useFunction(t,r)?t[r](n):((o={})[r]=n,t.set(o))},e.ignore=(null!=(t=s.dependencyDetection)?t.ignore:void 0)||function(e,t,r){var n;return n=null,s.dependentObservable(function(){return n=e.apply(t,r||[])}).dispose(),n},e.extend=d,e._throwMissing=function(e,t){throw""+(l.isString(e)?e:e.constructor.name)+": "+t+" is missing"},e._throwUnexpected=function(e,t){throw""+(l.isString(e)?e:e.constructor.name)+": "+t+" is unexpected"},e.publishMethods=function(t,r,n){var o,i,s;for(i=0,s=n.length;s>i;i++)o=n[i],t[o]=e._.bind(r[o],r)},e.peek=function(t){return s.isObservable(t)?t.peek?t.peek():e.ignore(function(){return t()}):t},e}(),a.Parse?(n=i.Parse=a.Parse,l=i._=a.Parse._):(n=i.Backbone=r(14),l=i._=r(15)),i.ko=s,i.Collection=n.Collection,i.Model=n.Object||n.Model,i.Events=n.Events,i.$=a.jQuery||a.$;try{i.$||(i.$=r(16))}catch(p){}}).call(t,function(){return this}())},function(e,t,r){var n,o,i;n=r(5),i=n._,o=n.ko,n.Observable=function(){function e(e,t,r){return this._vm=null!=r?r:{},n.ignore(function(r){return function(){var s,a,l;return t||n._throwMissing(r,"options"),s=r.create_options=i.isString(t)||o.isObservable(t)?{key:t}:n.utils.collapseOptions(t),r.key=s.key,delete s.key,r.key||n._throwMissing(r,"key"),!s.args||(r.args=s.args,delete s.args),!s.read||(r.read=s.read,delete s.read),!s.write||(r.write=s.write,delete s.write),a=s.event_watcher,delete s.event_watcher,r._vo=o.observable(null),r._model=o.observable(),l=n.utils.wrappedObservable(r,o.dependentObservable({read:function(){var e,t,s,a,l,u;for(l=r._model(),u=t=[r.key].concat(r.args||[]),s=0,a=u.length;a>s;s++)e=u[s],o.utils.unwrapObservable(e);return r.read?r.update(r.read.apply(r._vm,t)):i.isUndefined(l)||n.ignore(function(){return r.update(n.getValue(l,n.peek(r.key),r.args))}),o.utils.unwrapObservable(r._vo())},write:function(e){return n.ignore(function(){var t,o;return t=n.utils.unwrapModels(e),o=n.peek(r._model),r.write?(r.write.call(r._vm,t),e=n.getValue(o,n.peek(r.key),r.args)):o&&n.setValue(o,n.peek(r.key),t),r.update(e)})},owner:r._vm})),l.__kb_is_o=!0,s.store=n.utils.wrappedStore(l,s.store),s.path=n.utils.pathJoin(s.path,r.key),s.factories&&("function"==typeof s.factories||s.factories.create)?(s.factory=n.utils.wrappedFactory(l,new n.Factory(s.factory)),s.factory.addPathMapping(s.path,s.factories)):s.factory=n.Factory.useOptionsOrCreate(s,l,s.path),delete s.factories,n.publishMethods(l,r,["value","valueType","destroy"]),l.model=r.model=o.dependentObservable({read:function(){return o.utils.unwrapObservable(r._model)},write:function(e){return n.ignore(function(){var t;if(!r.__kb_released&&n.peek(r._model)!==e)return t=n.getValue(e,n.peek(r.key),r.args),r._model(e),e?i.isUndefined(t)?void 0:r.update(t):r.update(null)})}}),n.EventWatcher.useOptionsOrCreate({event_watcher:a},e,r,{emitter:r.model,update:i.bind(r.update,r),key:r.key,path:s.path}),r.__kb_value||r.update(),n.LocalizedObservable&&s.localizer&&(l=new s.localizer(l),delete s.localizer),n.DefaultObservable&&s.hasOwnProperty("default")&&(l=n.defaultObservable(l,s["default"]),delete s["default"]),l}}(this))}return e.prototype.destroy=function(){var e;return e=n.utils.wrappedObservable(this),this.__kb_released=!0,n.release(this.__kb_value),this.__kb_value=null,this.model.dispose(),this.model=e.model=null,n.utils.wrappedDestroy(this)},e.prototype.value=function(){return this.__kb_value},e.prototype.valueType=function(){var e;return e=n.getValue(n.peek(this._model),n.peek(this.key)),this.value_type||this._updateValueObservable(e),this.value_type},e.prototype.update=function(e){var t,r;if(!this.__kb_released){if(arguments.length||(e=n.getValue(n.peek(this._model),n.peek(this.key))),void 0!==e||(e=null),t=n.utils.valueType(e),(!this.__kb_value||this.__kb_value.__kb_released||this.__kb_value.__kb_null&&e)&&(this.__kb_value=void 0,this.value_type=void 0),r=this.__kb_value,i.isUndefined(this.value_type)||this.value_type!==t&&t!==n.TYPE_UNKNOWN)return this.value_type===n.TYPE_COLLECTION&&t===n.TYPE_ARRAY?r(e):this._updateValueObservable(e);if(this.value_type===n.TYPE_MODEL){if("function"==typeof r.model){if(r.model()!==e)return r.model(e)}else if(n.utils.wrappedObject(r)!==e)return this._updateValueObservable(e)}else if(this.value_type===n.TYPE_COLLECTION){if(r.collection()!==e)return r.collection(e)}else if(r()!==e)return r(e)}},e.prototype._updateValueObservable=function(e){var t,r,s,a;return t=this.create_options,t.creator=n.utils.inferCreator(e,t.factory,t.path,n.peek(this._model),this.key),this.value_type=n.TYPE_UNKNOWN,r=t.creator,s=this.__kb_value,this.__kb_value=void 0,s&&n.release(s),r?t.store?a=t.store.findOrCreate(e,t):r.models_only?(a=e,this.value_type=n.TYPE_SIMPLE):a=r.create?r.create(e,t):new r(e,t):i.isArray(e)?(this.value_type=n.TYPE_ARRAY,a=o.observableArray(e)):(this.value_type=n.TYPE_SIMPLE,a=o.observable(e)),this.value_type===n.TYPE_UNKNOWN&&(o.isObservable(a)?this.value_type=a.__kb_is_co?n.TYPE_COLLECTION:n.TYPE_SIMPLE:(this.value_type=n.TYPE_MODEL,"function"!=typeof a.model&&n.utils.wrappedObject(a,e))),this.__kb_value=a,this._vo(a)},e}(),n.observable=function(e,t,r){return new n.Observable(e,t,r)}},function(e,t,r){(function(e){var t,n,o,i,s,a,l,u,c,d;c="undefined"!=typeof window&&null!==window?window:e,u=r(5),d=u._,n=function(){function e(){this.adapters=[]}return e.prototype.initialize=function(){return this.adapters=d.select(this.adapters,function(e){return e.isAvailable()}),this.initialized=!0},e.prototype.addAdapter=function(e){return this.adapters.push(e),this.initialized=!1},e.prototype.keys=function(){return this._call("keys",arguments)},e.prototype.bind=function(){return this._call("bind",arguments)},e.prototype.useFunction=function(){return this._call("useFunction",arguments)},e.prototype._call=function(e,t){var r,n,o,i,s;if(this.adapters.length)for(this.initialized||this.initialize(),s=this.adapters,o=0,i=s.length;i>o;o++)if(r=s[o],r[e]&&(n=r[e].apply(r,t)))return n},e}(),u.orm=new n,a=null,i=function(){function e(){}return e.prototype.isAvailable=function(){var e;return!!(a=null!=(e=u.Backbone)?e.RelationalModel:void 0)},e.prototype.relationType=function(e,t){var r;return e instanceof a&&(r=d.find(e.getRelations(),function(e){return e.key===t}))?r.collectionType||d.isArray(r.keyContents)?u.TYPE_COLLECTION:u.TYPE_MODEL:null},e.prototype.bind=function(e,t,r,n){var o,i,s,a,l,c;if(!(a=this.relationType(e,t)))return null;if(s=function(e){return!u.statistics||u.statistics.addModelEvent({name:"update (relational)",model:e,key:t,path:n}),r()},i=u.Backbone.Relation.prototype.sanitizeOptions?["update","add","remove"]:["change","add","remove"],a===u.TYPE_COLLECTION)for(l=0,c=i.length;c>l;l++)o=i[l],e.bind(""+o+":"+t,s);else e.bind(""+i[0]+":"+t,s);return function(){var r,n;if(a===u.TYPE_COLLECTION)for(r=0,n=i.length;n>r;r++)o=i[r],e.unbind(""+o+":"+t,s);else e.unbind(""+i[0]+":"+t,s)}},e}(),u.orm.addAdapter(new i),t=null,o=function(){function e(){}return e.prototype.isAvailable=function(){var e;return!!(t=null!=(e=u.Backbone)?e.AssociatedModel:void 0)},e.prototype.keys=function(e){return e instanceof t?d.map(e.relations,function(e){return e.key}):null},e.prototype.relationType=function(e,r){var n;return e instanceof t&&(n=d.find(e.relations,function(e){return e.key===r}))?"Many"===n.type?u.TYPE_COLLECTION:u.TYPE_MODEL:null},e}(),u.orm.addAdapter(new o),l=null,s=function(){function e(){}return e.prototype.isAvailable=function(){return!!(l=c.Supermodel)},e.prototype.keys=function(e){return e instanceof l.Model?d.keys(e.constructor.associations()):null},e.prototype.relationType=function(e,t){var r;return e instanceof l.Model&&(r=e.constructor.associations()[t])?r.add?u.TYPE_COLLECTION:u.TYPE_MODEL:null},e.prototype.bind=function(e,t,r,n){var o,i;return(i=this.relationType(e,t))?(o=function(e,o){var i,s;return!u.statistics||u.statistics.addModelEvent({name:"update (supermodel)",model:e,key:t,path:n}),s=e.constructor.associations()[t],i=e[s.store],e[s.store]=o,r(o),e[s.store]=i},i===u.TYPE_MODEL?(e.bind("associate:"+t,o),function(){return e.unbind("associate:"+t,o)}):void 0):null},e.prototype.useFunction=function(e,t){return!!this.relationType(e,t)},e}(),u.orm.addAdapter(new s)}).call(t,function(){return this}())},function(e,t,r){var n,o;n=r(5),o=n._,e.exports=n.Statistics=function(){function e(){this.model_events_tracker=[],this.registered_tracker={}}return e.prototype.clear=function(){return this.model_events_tracker=[]},e.prototype.addModelEvent=function(e){return this.model_events_tracker.push(e)},e.prototype.modelEventsStatsString=function(){var e,t,r,n;r="",r+="Total Count: "+this.model_events_tracker.length,e=o.groupBy(this.model_events_tracker,function(e){return"event name: '"+e.name+"', attribute name: '"+e.key+"'"});for(t in e)n=e[t],r+="\n "+t+", count: "+n.length;return r},e.prototype.register=function(e,t){return this.registeredTracker(e).push(t)},e.prototype.unregister=function(e,t){var r,n;return n=this.registeredTracker(e),r=o.indexOf(n,t),0>r&&"undefined"!=typeof console&&null!==console&&console.log("kb.Statistics: failed to unregister type: "+e),n.splice(r,1)},e.prototype.registeredCount=function(e){var t,r,n;if(e)return this.registeredTracker(e).length;t=0,n=this.registered_tracker[e];for(e in n)r=n[e],t+=r.length;return t},e.prototype.registeredStatsString=function(e){var t,r,n,o,i;t="",i=this.registered_tracker;for(r in i)n=i[r],n.length&&(o&&(t+="\n "),t+=""+(r?r:"No Name")+": "+n.length,o=!0);return t?t:e},e.prototype.registeredTracker=function(e){var t;return this.registered_tracker.hasOwnProperty(e)?this.registered_tracker[e]:(t=[],this.registered_tracker[e]=t,t)},e}()},function(e,t,r){var n,o,i;n=r(5),i=n._,o=n.ko,e.exports=n.Store=function(){function e(){this.observable_records=[],this.replaced_observables=[]}return e.useOptionsOrCreate=function(e,t,r){return e.store?(e.store.register(t,r,e),n.utils.wrappedStore(r,e.store)):(n.utils.wrappedStoreIsOwned(r,!0),n.utils.wrappedStore(r,new n.Store))},e.prototype.destroy=function(){return this.clear()},e.prototype.clear=function(){var e,t,r,o;for(o=this.observable_records.splice(0,this.observable_records.length),t=0,r=o.length;r>t;t++)e=o[t],n.release(e.observable);n.release(this.replaced_observables)},e.prototype.compact=function(){var e,t,r,n,o;r=[],n=this.observable_records;for(e in n)t=n[e],(null!=(o=t.observable)?o.__kb_released:void 0)&&r.push(t);r.length&&(this.observable_records=i.difference(this.observable_records,r))},e.prototype.register=function(e,t,r){var i;if(t&&!o.isObservable(t)&&!t.__kb_is_co)return n.utils.wrappedObject(t,e),e||(t.__kb_null=!0),i=r.creator?r.creator:r.path&&r.factory?r.factory.creatorForPath(e,r.path):null,i||(i=t.constructor),this.observable_records.push({obj:e,observable:t,creator:i}),t},e.prototype.findIndex=function(e,t){var r,o,s,a;if(s=[],!e||e instanceof n.Model){a=this.observable_records;for(r in a)if(o=a[r],o.observable)if(o.observable.__kb_released)s.push(o);else if((e||o.observable.__kb_null)&&(!e||!o.observable.__kb_null&&o.obj===e)&&(o.creator===t||o.creator.create&&o.creator.create===t.create))return s.length?(this.observable_records=i.difference(this.observable_records,s),i.indexOf(this.observable_records,o)):r}return s.length&&(this.observable_records=i.difference(this.observable_records,s)),-1},e.prototype.find=function(e,t){var r;return(r=this.findIndex(e,t))<0?null:this.observable_records[r].observable},e.prototype.isRegistered=function(e){var t,r,n,o;for(o=this.observable_records,r=0,n=o.length;n>r;r++)if(t=o[r],t.observable===e)return!0;return!1},e.prototype.findOrCreate=function(e,t){var r,i;return t.store=this,t.creator||(t.creator=n.utils.inferCreator(e,t.factory,t.path)),!t.creator&&e instanceof n.Model&&(t.creator=n.ViewModel),(r=t.creator)?r.models_only?e:(r&&(i=this.find(e,r)),i?i:(i=n.ignore(function(){return function(){return i=r.create?r.create(e,t):new r(e,t),i||o.observable(null)}}(this)),o.isObservable(i)||this.isRegistered(i)||this.register(e,i,t),i)):n.utils.createFromDefaultCreator(e,t)},e.prototype.findOrReplace=function(e,t,r){var o,i;return e||n._throwUnexpected(this,"obj missing"),(o=this.findIndex(e,t))<0?this.register(e,r,{creator:t}):(i=this.observable_records[o],n.utils.wrappedObject(i.observable)===e||n._throwUnexpected(this,"different object"),i.observable!==r&&(i.observable.constructor===r.constructor||n._throwUnexpected(this,"replacing different type"),this.replaced_observables.push(i.observable),i.observable=r),r)},e}()},function(e,t,r){var n,o,i,s,a,l,u,c;n=r(5),i=n._,o=n.ko,c=n._wrappedKey=function(e,t,r){return 2===arguments.length?e&&e.__kb&&e.__kb.hasOwnProperty(t)?e.__kb[t]:void 0:(e||n._throwUnexpected(this,"no obj for wrapping "+t),e.__kb||(e.__kb={}),e.__kb[t]=r,r)},s=function(e,t){return Array.prototype.splice.call(e,1,0,t),e},l=function(e,t,r){return e[t]||(e[t]=[]),i.isArray(r)||(r=[r]),e[t]=e[t].length?i.union(e[t],r):r,e},u=function(e,t,r){return e[t]||(e[t]={}),i.extend(e[t],r)},a=function(e){var t,r,n,o;for(r={},n=0,o=e.length;o>n;n++)t=e[n],r[t]={key:t};return r},n.utils=function(){function e(){}return e.wrappedObservable=function(){return c.apply(this,s(arguments,"observable"))},e.wrappedObject=function(){return c.apply(this,s(arguments,"object"))},e.wrappedModel=function(e,t){return 1===arguments.length?(t=c(e,"object"),i.isUndefined(t)?e:t):c(e,"object",t)},e.wrappedStore=function(){return c.apply(this,s(arguments,"store"))},e.wrappedStoreIsOwned=function(){return c.apply(this,s(arguments,"store_is_owned"))},e.wrappedFactory=function(){return c.apply(this,s(arguments,"factory"))},e.wrappedEventWatcher=function(){return c.apply(this,s(arguments,"event_watcher"))},e.wrappedEventWatcherIsOwned=function(){return c.apply(this,s(arguments,"event_watcher_is_owned"))},e.wrappedDestroy=function(e){var t;if(e.__kb)return e.__kb.event_watcher&&e.__kb.event_watcher.releaseCallbacks(e),t=e.__kb,e.__kb=null,t.observable&&(t.observable.destroy=t.observable.release=null,this.wrappedDestroy(t.observable),t.observable=null),t.factory=null,t.event_watcher_is_owned&&t.event_watcher.destroy(),t.event_watcher=null,t.store_is_owned&&t.store.destroy(),t.store=null},e.valueType=function(e){return e?e.__kb_is_o?e.valueType():e.__kb_is_co||e instanceof n.Collection?n.TYPE_COLLECTION:e instanceof n.ViewModel||e instanceof n.Model?n.TYPE_MODEL:i.isArray(e)?n.TYPE_ARRAY:n.TYPE_SIMPLE:n.TYPE_UNKNOWN},e.pathJoin=function(e,t){return(e?"."!==e[e.length-1]?""+e+".":e:"")+t},e.optionsPathJoin=function(e,t){return i.defaults({path:this.pathJoin(e.path,t)},e)},e.inferCreator=function(e,t,r){var o;return t&&(o=t.creatorForPath(e,r)),o?o:e?e instanceof n.Model?n.ViewModel:e instanceof n.Collection?n.CollectionObservable:null:null},e.createFromDefaultCreator=function(e,t){return e instanceof n.Model?n.viewModel(e,t):e instanceof n.Collection?n.collectionObservable(e,t):i.isArray(e)?o.observableArray(e):o.observable(e)},e.hasModelSignature=function(e){return e&&e.attributes&&!e.models&&"function"==typeof e.get&&"function"==typeof e.trigger},e.hasCollectionSignature=function(e){return e&&e.models&&"function"==typeof e.get&&"function"==typeof e.trigger },e.collapseOptions=function(e){var t,r,n,o;for(r={},e={options:e};e.options;){o=e.options;for(t in o)switch(n=o[t],t){case"internals":case"requires":case"excludes":case"statics":l(r,t,n);break;case"keys":i.isObject(n)&&!i.isArray(n)||i.isObject(r[t])&&!i.isArray(r[t])?(i.isObject(n)||(n=[n]),i.isArray(n)&&(n=a(n)),i.isArray(r[t])&&(r[t]=a(r[t])),u(r,t,n)):l(r,t,n);break;case"factories":i.isFunction(n)?r[t]=n:u(r,t,n);break;case"static_defaults":u(r,t,n);break;case"options":break;default:r[t]=n}e=e.options}return r},e.unwrapModels=function(e){var t,r,o;if(!e)return e;if(e.__kb)return"object"in e.__kb?e.__kb.object:e;if(i.isArray(e))return i.map(e,function(e){return n.utils.unwrapModels(e)});if(i.isObject(e)&&e.constructor==={}.constructor){r={};for(t in e)o=e[t],r[t]=n.utils.unwrapModels(o);return r}return e},e}()},function(e,t,r){var n,o,i;n=r(5),i=n._,o=n.ko,n.ViewModel=function(){function e(e,t,r){return n.ignore(function(s){return function(){var a,l,u,c,d,p,_,f,h,b;if(!e||e instanceof n.Model||"function"==typeof e.get&&"function"==typeof e.bind||n._throwUnexpected(s,"not a model"),t||(t={}),r||(r={}),t=i.isArray(t)?{keys:t}:n.utils.collapseOptions(t),s.__kb||(s.__kb={}),s.__kb.vm_keys={},s.__kb.model_keys={},s.__kb.view_model=i.isUndefined(r)?s:r,!t.internals||(s.__kb.internals=t.internals),!t.excludes||(s.__kb.excludes=t.excludes),!t.statics||(s.__kb.statics=t.statics),!t.static_defaults||(s.__kb.static_defaults=t.static_defaults),n.Store.useOptionsOrCreate(t,e,s),s.__kb.path=t.path,n.Factory.useOptionsOrCreate(t,s,t.path),h=n._wrappedKey(s,"_mdl",o.observable()),s.model=o.dependentObservable({read:function(){return h(),n.utils.wrappedObject(s)},write:function(e){return n.ignore(function(){var t,r,o,a;if(n.utils.wrappedObject(s)!==e){if(s.__kb_null)return void(!e||n._throwUnexpected(s,"model set on shared null"));if(n.utils.wrappedObject(s,e),t=n.utils.wrappedEventWatcher(s),!t)return void h(e);t.emitter(e),!s.__kb.keys&&e&&e.attributes&&(r=i.keys(e.attributes),e&&(a=n.orm.keys(e))&&(r=i.union(r,a)),o=i.difference(r,i.keys(s.__kb.model_keys)),o&&s.createObservables(e,o)),h(e)}})}}),u=n.utils.wrappedEventWatcher(s,new n.EventWatcher(e,s,{emitter:s.model})),c=t.requires,s.__kb.internals&&(c=i.union(c||[],s.__kb.internals)),e&&(_=n.orm.keys(e))&&(c=i.union(c||[],_)),t.keys)if(i.isObject(t.keys)&&!i.isArray(t.keys)){d={},b=t.keys;for(f in b)p=b[f],d[i.isString(p)?p:p.key?p.key:f]=!0;s.__kb.keys=i.keys(d)}else s.__kb.keys=t.keys,c=c?i.union(c,s.__kb.keys):i.clone(s.__kb.keys);else l=u.emitter(),l&&l.attributes&&(a=i.keys(l.attributes),c=c?i.union(c,a):a);return c&&s.__kb.excludes&&(c=i.difference(c,s.__kb.excludes)),c&&s.__kb.statics&&(c=i.difference(c,s.__kb.statics)),i.isObject(t.keys)&&!i.isArray(t.keys)&&s.mapObservables(e,t.keys),i.isObject(t.requires)&&!i.isArray(t.requires)&&s.mapObservables(e,t.requires),!t.mappings||s.mapObservables(e,t.mappings),!c||s.createObservables(e,c),!s.__kb.statics||s.createObservables(e,s.__kb.statics,!0),!n.statistics||n.statistics.register("ViewModel",s),s}}(this))}return e.extend=n.extend,e.prototype.destroy=function(){var e;if(this.__kb.view_model!==this)for(e in this.__kb.vm_keys)this.__kb.view_model[e]=null;return this.__kb.view_model=null,n.releaseKeys(this),n.utils.wrappedDestroy(this),!n.statistics||n.statistics.unregister("ViewModel",this)},e.prototype.shareOptions=function(){return{store:n.utils.wrappedStore(this),factory:n.utils.wrappedFactory(this)}},e.prototype.createObservables=function(e,t,r){var o,s,a,l,u,c;for(r?a=this.__kb.static_defaults||{}:o={store:n.utils.wrappedStore(this),factory:n.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:n.utils.wrappedEventWatcher(this)},u=0,c=t.length;c>u;u++)s=t[u],l=this.__kb.internals&&i.contains(this.__kb.internals,s)?"_"+s:s,this[l]||(this.__kb.vm_keys[l]=this.__kb.model_keys[s]=!0,r?e.has(l)?this[l]=this.__kb.view_model[l]=e.get(l):l in a&&(this[l]=this.__kb.view_model[l]=a[l]):(o.key=s,this[l]=this.__kb.view_model[l]=n.observable(e,o,this)))},e.prototype.mapObservables=function(e,t){var r,o,s;r={store:n.utils.wrappedStore(this),factory:n.utils.wrappedFactory(this),path:this.__kb.path,event_watcher:n.utils.wrappedEventWatcher(this)};for(s in t)o=t[s],this[s]||(o=i.isString(o)?{key:o}:i.clone(o),o.key||(o.key=s),this.__kb.vm_keys[s]=this.__kb.model_keys[o.key]=!0,this[s]=this.__kb.view_model[s]=n.observable(e,i.defaults(o,r),this))},e}(),n.viewModel=function(e,t,r){return new n.ViewModel(e,t,r)}},function(e,t,r){var n,o,i,s,a;if(e.exports=n=r(5),n.modules={underscore:n._,backbone:n.Parse||n.Backbone,knockout:n.ko},"undefined"!=typeof window&&null!==window)for(a=["_","Backbone","Parse","ko","$"],i=0,s=a.length;s>i;i++)o=a[i],n[o]&&!window.hasOwnProperty(o)&&(window[o]=n[o])},function(t){t.exports=e},function(e){e.exports=t},function(e){e.exports=r},function(e){if("undefined"==typeof n){var t=new Error('Cannot find module "undefined"');throw t.code="MODULE_NOT_FOUND",t}e.exports=n}])});