--- a/web/res/js/backbone-min.js Wed Dec 11 10:17:08 2019 +0100
+++ b/web/res/js/backbone-min.js Wed Dec 11 11:02:15 2019 +0100
@@ -1,2 +1,2 @@
-(function(t){var e=typeof self=="object"&&self.self==self&&self||typeof global=="object"&&global.global==global&&global;if(typeof define==="function"&&define.amd){define(["underscore","jquery","exports"],function(i,r,n){e.Backbone=t(e,n,i,r)})}else if(typeof exports!=="undefined"){var i=require("underscore"),r;try{r=require("jquery")}catch(n){}t(e,exports,i,r)}else{e.Backbone=t(e,{},e._,e.jQuery||e.Zepto||e.ender||e.$)}})(function(t,e,i,r){var n=t.Backbone;var s=Array.prototype.slice;e.VERSION="1.2.3";e.$=r;e.noConflict=function(){t.Backbone=n;return this};e.emulateHTTP=false;e.emulateJSON=false;var a=function(t,e,r){switch(t){case 1:return function(){return i[e](this[r])};case 2:return function(t){return i[e](this[r],t)};case 3:return function(t,n){return i[e](this[r],h(t,this),n)};case 4:return function(t,n,s){return i[e](this[r],h(t,this),n,s)};default:return function(){var t=s.call(arguments);t.unshift(this[r]);return i[e].apply(i,t)}}};var o=function(t,e,r){i.each(e,function(e,n){if(i[n])t.prototype[n]=a(e,n,r)})};var h=function(t,e){if(i.isFunction(t))return t;if(i.isObject(t)&&!e._isModel(t))return u(t);if(i.isString(t))return function(e){return e.get(t)};return t};var u=function(t){var e=i.matches(t);return function(t){return e(t.attributes)}};var l=e.Events={};var c=/\s+/;var f=function(t,e,r,n,s){var a=0,o;if(r&&typeof r==="object"){if(n!==void 0&&"context"in s&&s.context===void 0)s.context=n;for(o=i.keys(r);a<o.length;a++){e=f(t,e,o[a],r[o[a]],s)}}else if(r&&c.test(r)){for(o=r.split(c);a<o.length;a++){e=t(e,o[a],n,s)}}else{e=t(e,r,n,s)}return e};l.on=function(t,e,i){return d(this,t,e,i)};var d=function(t,e,i,r,n){t._events=f(v,t._events||{},e,i,{context:r,ctx:t,listening:n});if(n){var s=t._listeners||(t._listeners={});s[n.id]=n}return t};l.listenTo=function(t,e,r){if(!t)return this;var n=t._listenId||(t._listenId=i.uniqueId("l"));var s=this._listeningTo||(this._listeningTo={});var a=s[n];if(!a){var o=this._listenId||(this._listenId=i.uniqueId("l"));a=s[n]={obj:t,objId:n,id:o,listeningTo:s,count:0}}d(t,e,r,this,a);return this};var v=function(t,e,i,r){if(i){var n=t[e]||(t[e]=[]);var s=r.context,a=r.ctx,o=r.listening;if(o)o.count++;n.push({callback:i,context:s,ctx:s||a,listening:o})}return t};l.off=function(t,e,i){if(!this._events)return this;this._events=f(g,this._events,t,e,{context:i,listeners:this._listeners});return this};l.stopListening=function(t,e,r){var n=this._listeningTo;if(!n)return this;var s=t?[t._listenId]:i.keys(n);for(var a=0;a<s.length;a++){var o=n[s[a]];if(!o)break;o.obj.off(e,r,this)}if(i.isEmpty(n))this._listeningTo=void 0;return this};var g=function(t,e,r,n){if(!t)return;var s=0,a;var o=n.context,h=n.listeners;if(!e&&!r&&!o){var u=i.keys(h);for(;s<u.length;s++){a=h[u[s]];delete h[a.id];delete a.listeningTo[a.objId]}return}var l=e?[e]:i.keys(t);for(;s<l.length;s++){e=l[s];var c=t[e];if(!c)break;var f=[];for(var d=0;d<c.length;d++){var v=c[d];if(r&&r!==v.callback&&r!==v.callback._callback||o&&o!==v.context){f.push(v)}else{a=v.listening;if(a&&--a.count===0){delete h[a.id];delete a.listeningTo[a.objId]}}}if(f.length){t[e]=f}else{delete t[e]}}if(i.size(t))return t};l.once=function(t,e,r){var n=f(p,{},t,e,i.bind(this.off,this));return this.on(n,void 0,r)};l.listenToOnce=function(t,e,r){var n=f(p,{},e,r,i.bind(this.stopListening,this,t));return this.listenTo(t,n)};var p=function(t,e,r,n){if(r){var s=t[e]=i.once(function(){n(e,s);r.apply(this,arguments)});s._callback=r}return t};l.trigger=function(t){if(!this._events)return this;var e=Math.max(0,arguments.length-1);var i=Array(e);for(var r=0;r<e;r++)i[r]=arguments[r+1];f(m,this._events,t,void 0,i);return this};var m=function(t,e,i,r){if(t){var n=t[e];var s=t.all;if(n&&s)s=s.slice();if(n)_(n,r);if(s)_(s,[e].concat(r))}return t};var _=function(t,e){var i,r=-1,n=t.length,s=e[0],a=e[1],o=e[2];switch(e.length){case 0:while(++r<n)(i=t[r]).callback.call(i.ctx);return;case 1:while(++r<n)(i=t[r]).callback.call(i.ctx,s);return;case 2:while(++r<n)(i=t[r]).callback.call(i.ctx,s,a);return;case 3:while(++r<n)(i=t[r]).callback.call(i.ctx,s,a,o);return;default:while(++r<n)(i=t[r]).callback.apply(i.ctx,e);return}};l.bind=l.on;l.unbind=l.off;i.extend(e,l);var y=e.Model=function(t,e){var r=t||{};e||(e={});this.cid=i.uniqueId(this.cidPrefix);this.attributes={};if(e.collection)this.collection=e.collection;if(e.parse)r=this.parse(r,e)||{};r=i.defaults({},r,i.result(this,"defaults"));this.set(r,e);this.changed={};this.initialize.apply(this,arguments)};i.extend(y.prototype,l,{changed:null,validationError:null,idAttribute:"id",cidPrefix:"c",initialize:function(){},toJSON:function(t){return i.clone(this.attributes)},sync:function(){return e.sync.apply(this,arguments)},get:function(t){return this.attributes[t]},escape:function(t){return i.escape(this.get(t))},has:function(t){return this.get(t)!=null},matches:function(t){return!!i.iteratee(t,this)(this.attributes)},set:function(t,e,r){if(t==null)return this;var n;if(typeof t==="object"){n=t;r=e}else{(n={})[t]=e}r||(r={});if(!this._validate(n,r))return false;var s=r.unset;var a=r.silent;var o=[];var h=this._changing;this._changing=true;if(!h){this._previousAttributes=i.clone(this.attributes);this.changed={}}var u=this.attributes;var l=this.changed;var c=this._previousAttributes;for(var f in n){e=n[f];if(!i.isEqual(u[f],e))o.push(f);if(!i.isEqual(c[f],e)){l[f]=e}else{delete l[f]}s?delete u[f]:u[f]=e}this.id=this.get(this.idAttribute);if(!a){if(o.length)this._pending=r;for(var d=0;d<o.length;d++){this.trigger("change:"+o[d],this,u[o[d]],r)}}if(h)return this;if(!a){while(this._pending){r=this._pending;this._pending=false;this.trigger("change",this,r)}}this._pending=false;this._changing=false;return this},unset:function(t,e){return this.set(t,void 0,i.extend({},e,{unset:true}))},clear:function(t){var e={};for(var r in this.attributes)e[r]=void 0;return this.set(e,i.extend({},t,{unset:true}))},hasChanged:function(t){if(t==null)return!i.isEmpty(this.changed);return i.has(this.changed,t)},changedAttributes:function(t){if(!t)return this.hasChanged()?i.clone(this.changed):false;var e=this._changing?this._previousAttributes:this.attributes;var r={};for(var n in t){var s=t[n];if(i.isEqual(e[n],s))continue;r[n]=s}return i.size(r)?r:false},previous:function(t){if(t==null||!this._previousAttributes)return null;return this._previousAttributes[t]},previousAttributes:function(){return i.clone(this._previousAttributes)},fetch:function(t){t=i.extend({parse:true},t);var e=this;var r=t.success;t.success=function(i){var n=t.parse?e.parse(i,t):i;if(!e.set(n,t))return false;if(r)r.call(t.context,e,i,t);e.trigger("sync",e,i,t)};z(this,t);return this.sync("read",this,t)},save:function(t,e,r){var n;if(t==null||typeof t==="object"){n=t;r=e}else{(n={})[t]=e}r=i.extend({validate:true,parse:true},r);var s=r.wait;if(n&&!s){if(!this.set(n,r))return false}else{if(!this._validate(n,r))return false}var a=this;var o=r.success;var h=this.attributes;r.success=function(t){a.attributes=h;var e=r.parse?a.parse(t,r):t;if(s)e=i.extend({},n,e);if(e&&!a.set(e,r))return false;if(o)o.call(r.context,a,t,r);a.trigger("sync",a,t,r)};z(this,r);if(n&&s)this.attributes=i.extend({},h,n);var u=this.isNew()?"create":r.patch?"patch":"update";if(u==="patch"&&!r.attrs)r.attrs=n;var l=this.sync(u,this,r);this.attributes=h;return l},destroy:function(t){t=t?i.clone(t):{};var e=this;var r=t.success;var n=t.wait;var s=function(){e.stopListening();e.trigger("destroy",e,e.collection,t)};t.success=function(i){if(n)s();if(r)r.call(t.context,e,i,t);if(!e.isNew())e.trigger("sync",e,i,t)};var a=false;if(this.isNew()){i.defer(t.success)}else{z(this,t);a=this.sync("delete",this,t)}if(!n)s();return a},url:function(){var t=i.result(this,"urlRoot")||i.result(this.collection,"url")||F();if(this.isNew())return t;var e=this.get(this.idAttribute);return t.replace(/[^\/]$/,"$&/")+encodeURIComponent(e)},parse:function(t,e){return t},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return!this.has(this.idAttribute)},isValid:function(t){return this._validate({},i.defaults({validate:true},t))},_validate:function(t,e){if(!e.validate||!this.validate)return true;t=i.extend({},this.attributes,t);var r=this.validationError=this.validate(t,e)||null;if(!r)return true;this.trigger("invalid",this,r,i.extend(e,{validationError:r}));return false}});var b={keys:1,values:1,pairs:1,invert:1,pick:0,omit:0,chain:1,isEmpty:1};o(y,b,"attributes");var x=e.Collection=function(t,e){e||(e={});if(e.model)this.model=e.model;if(e.comparator!==void 0)this.comparator=e.comparator;this._reset();this.initialize.apply(this,arguments);if(t)this.reset(t,i.extend({silent:true},e))};var w={add:true,remove:true,merge:true};var E={add:true,remove:false};var k=function(t,e,i){i=Math.min(Math.max(i,0),t.length);var r=Array(t.length-i);var n=e.length;for(var s=0;s<r.length;s++)r[s]=t[s+i];for(s=0;s<n;s++)t[s+i]=e[s];for(s=0;s<r.length;s++)t[s+n+i]=r[s]};i.extend(x.prototype,l,{model:y,initialize:function(){},toJSON:function(t){return this.map(function(e){return e.toJSON(t)})},sync:function(){return e.sync.apply(this,arguments)},add:function(t,e){return this.set(t,i.extend({merge:false},e,E))},remove:function(t,e){e=i.extend({},e);var r=!i.isArray(t);t=r?[t]:i.clone(t);var n=this._removeModels(t,e);if(!e.silent&&n)this.trigger("update",this,e);return r?n[0]:n},set:function(t,e){if(t==null)return;e=i.defaults({},e,w);if(e.parse&&!this._isModel(t))t=this.parse(t,e);var r=!i.isArray(t);t=r?[t]:t.slice();var n=e.at;if(n!=null)n=+n;if(n<0)n+=this.length+1;var s=[];var a=[];var o=[];var h={};var u=e.add;var l=e.merge;var c=e.remove;var f=false;var d=this.comparator&&n==null&&e.sort!==false;var v=i.isString(this.comparator)?this.comparator:null;var g;for(var p=0;p<t.length;p++){g=t[p];var m=this.get(g);if(m){if(l&&g!==m){var _=this._isModel(g)?g.attributes:g;if(e.parse)_=m.parse(_,e);m.set(_,e);if(d&&!f)f=m.hasChanged(v)}if(!h[m.cid]){h[m.cid]=true;s.push(m)}t[p]=m}else if(u){g=t[p]=this._prepareModel(g,e);if(g){a.push(g);this._addReference(g,e);h[g.cid]=true;s.push(g)}}}if(c){for(p=0;p<this.length;p++){g=this.models[p];if(!h[g.cid])o.push(g)}if(o.length)this._removeModels(o,e)}var y=false;var b=!d&&u&&c;if(s.length&&b){y=this.length!=s.length||i.some(this.models,function(t,e){return t!==s[e]});this.models.length=0;k(this.models,s,0);this.length=this.models.length}else if(a.length){if(d)f=true;k(this.models,a,n==null?this.length:n);this.length=this.models.length}if(f)this.sort({silent:true});if(!e.silent){for(p=0;p<a.length;p++){if(n!=null)e.index=n+p;g=a[p];g.trigger("add",g,this,e)}if(f||y)this.trigger("sort",this,e);if(a.length||o.length)this.trigger("update",this,e)}return r?t[0]:t},reset:function(t,e){e=e?i.clone(e):{};for(var r=0;r<this.models.length;r++){this._removeReference(this.models[r],e)}e.previousModels=this.models;this._reset();t=this.add(t,i.extend({silent:true},e));if(!e.silent)this.trigger("reset",this,e);return t},push:function(t,e){return this.add(t,i.extend({at:this.length},e))},pop:function(t){var e=this.at(this.length-1);return this.remove(e,t)},unshift:function(t,e){return this.add(t,i.extend({at:0},e))},shift:function(t){var e=this.at(0);return this.remove(e,t)},slice:function(){return s.apply(this.models,arguments)},get:function(t){if(t==null)return void 0;var e=this.modelId(this._isModel(t)?t.attributes:t);return this._byId[t]||this._byId[e]||this._byId[t.cid]},at:function(t){if(t<0)t+=this.length;return this.models[t]},where:function(t,e){return this[e?"find":"filter"](t)},findWhere:function(t){return this.where(t,true)},sort:function(t){var e=this.comparator;if(!e)throw new Error("Cannot sort a set without a comparator");t||(t={});var r=e.length;if(i.isFunction(e))e=i.bind(e,this);if(r===1||i.isString(e)){this.models=this.sortBy(e)}else{this.models.sort(e)}if(!t.silent)this.trigger("sort",this,t);return this},pluck:function(t){return i.invoke(this.models,"get",t)},fetch:function(t){t=i.extend({parse:true},t);var e=t.success;var r=this;t.success=function(i){var n=t.reset?"reset":"set";r[n](i,t);if(e)e.call(t.context,r,i,t);r.trigger("sync",r,i,t)};z(this,t);return this.sync("read",this,t)},create:function(t,e){e=e?i.clone(e):{};var r=e.wait;t=this._prepareModel(t,e);if(!t)return false;if(!r)this.add(t,e);var n=this;var s=e.success;e.success=function(t,e,i){if(r)n.add(t,i);if(s)s.call(i.context,t,e,i)};t.save(null,e);return t},parse:function(t,e){return t},clone:function(){return new this.constructor(this.models,{model:this.model,comparator:this.comparator})},modelId:function(t){return t[this.model.prototype.idAttribute||"id"]},_reset:function(){this.length=0;this.models=[];this._byId={}},_prepareModel:function(t,e){if(this._isModel(t)){if(!t.collection)t.collection=this;return t}e=e?i.clone(e):{};e.collection=this;var r=new this.model(t,e);if(!r.validationError)return r;this.trigger("invalid",this,r.validationError,e);return false},_removeModels:function(t,e){var i=[];for(var r=0;r<t.length;r++){var n=this.get(t[r]);if(!n)continue;var s=this.indexOf(n);this.models.splice(s,1);this.length--;if(!e.silent){e.index=s;n.trigger("remove",n,this,e)}i.push(n);this._removeReference(n,e)}return i.length?i:false},_isModel:function(t){return t instanceof y},_addReference:function(t,e){this._byId[t.cid]=t;var i=this.modelId(t.attributes);if(i!=null)this._byId[i]=t;t.on("all",this._onModelEvent,this)},_removeReference:function(t,e){delete this._byId[t.cid];var i=this.modelId(t.attributes);if(i!=null)delete this._byId[i];if(this===t.collection)delete t.collection;t.off("all",this._onModelEvent,this)},_onModelEvent:function(t,e,i,r){if((t==="add"||t==="remove")&&i!==this)return;if(t==="destroy")this.remove(e,r);if(t==="change"){var n=this.modelId(e.previousAttributes());var s=this.modelId(e.attributes);if(n!==s){if(n!=null)delete this._byId[n];if(s!=null)this._byId[s]=e}}this.trigger.apply(this,arguments)}});var S={forEach:3,each:3,map:3,collect:3,reduce:4,foldl:4,inject:4,reduceRight:4,foldr:4,find:3,detect:3,filter:3,select:3,reject:3,every:3,all:3,some:3,any:3,include:3,includes:3,contains:3,invoke:0,max:3,min:3,toArray:1,size:1,first:3,head:3,take:3,initial:3,rest:3,tail:3,drop:3,last:3,without:0,difference:0,indexOf:3,shuffle:1,lastIndexOf:3,isEmpty:1,chain:1,sample:3,partition:3,groupBy:3,countBy:3,sortBy:3,indexBy:3};o(x,S,"models");var I=e.View=function(t){this.cid=i.uniqueId("view");i.extend(this,i.pick(t,P));this._ensureElement();this.initialize.apply(this,arguments)};var T=/^(\S+)\s*(.*)$/;var P=["model","collection","el","id","attributes","className","tagName","events"];i.extend(I.prototype,l,{tagName:"div",$:function(t){return this.$el.find(t)},initialize:function(){},render:function(){return this},remove:function(){this._removeElement();this.stopListening();return this},_removeElement:function(){this.$el.remove()},setElement:function(t){this.undelegateEvents();this._setElement(t);this.delegateEvents();return this},_setElement:function(t){this.$el=t instanceof e.$?t:e.$(t);this.el=this.$el[0]},delegateEvents:function(t){t||(t=i.result(this,"events"));if(!t)return this;this.undelegateEvents();for(var e in t){var r=t[e];if(!i.isFunction(r))r=this[r];if(!r)continue;var n=e.match(T);this.delegate(n[1],n[2],i.bind(r,this))}return this},delegate:function(t,e,i){this.$el.on(t+".delegateEvents"+this.cid,e,i);return this},undelegateEvents:function(){if(this.$el)this.$el.off(".delegateEvents"+this.cid);return this},undelegate:function(t,e,i){this.$el.off(t+".delegateEvents"+this.cid,e,i);return this},_createElement:function(t){return document.createElement(t)},_ensureElement:function(){if(!this.el){var t=i.extend({},i.result(this,"attributes"));if(this.id)t.id=i.result(this,"id");if(this.className)t["class"]=i.result(this,"className");this.setElement(this._createElement(i.result(this,"tagName")));this._setAttributes(t)}else{this.setElement(i.result(this,"el"))}},_setAttributes:function(t){this.$el.attr(t)}});e.sync=function(t,r,n){var s=H[t];i.defaults(n||(n={}),{emulateHTTP:e.emulateHTTP,emulateJSON:e.emulateJSON});var a={type:s,dataType:"json"};if(!n.url){a.url=i.result(r,"url")||F()}if(n.data==null&&r&&(t==="create"||t==="update"||t==="patch")){a.contentType="application/json";a.data=JSON.stringify(n.attrs||r.toJSON(n))}if(n.emulateJSON){a.contentType="application/x-www-form-urlencoded";a.data=a.data?{model:a.data}:{}}if(n.emulateHTTP&&(s==="PUT"||s==="DELETE"||s==="PATCH")){a.type="POST";if(n.emulateJSON)a.data._method=s;var o=n.beforeSend;n.beforeSend=function(t){t.setRequestHeader("X-HTTP-Method-Override",s);if(o)return o.apply(this,arguments)}}if(a.type!=="GET"&&!n.emulateJSON){a.processData=false}var h=n.error;n.error=function(t,e,i){n.textStatus=e;n.errorThrown=i;if(h)h.call(n.context,t,e,i)};var u=n.xhr=e.ajax(i.extend(a,n));r.trigger("request",r,u,n);return u};var H={create:"POST",update:"PUT",patch:"PATCH","delete":"DELETE",read:"GET"};e.ajax=function(){return e.$.ajax.apply(e.$,arguments)};var $=e.Router=function(t){t||(t={});if(t.routes)this.routes=t.routes;this._bindRoutes();this.initialize.apply(this,arguments)};var A=/\((.*?)\)/g;var C=/(\(\?)?:\w+/g;var R=/\*\w+/g;var j=/[\-{}\[\]+?.,\\\^$|#\s]/g;i.extend($.prototype,l,{initialize:function(){},route:function(t,r,n){if(!i.isRegExp(t))t=this._routeToRegExp(t);if(i.isFunction(r)){n=r;r=""}if(!n)n=this[r];var s=this;e.history.route(t,function(i){var a=s._extractParameters(t,i);if(s.execute(n,a,r)!==false){s.trigger.apply(s,["route:"+r].concat(a));s.trigger("route",r,a);e.history.trigger("route",s,r,a)}});return this},execute:function(t,e,i){if(t)t.apply(this,e)},navigate:function(t,i){e.history.navigate(t,i);return this},_bindRoutes:function(){if(!this.routes)return;this.routes=i.result(this,"routes");var t,e=i.keys(this.routes);while((t=e.pop())!=null){this.route(t,this.routes[t])}},_routeToRegExp:function(t){t=t.replace(j,"\\$&").replace(A,"(?:$1)?").replace(C,function(t,e){return e?t:"([^/?]+)"}).replace(R,"([^?]*?)");return new RegExp("^"+t+"(?:\\?([\\s\\S]*))?$")},_extractParameters:function(t,e){var r=t.exec(e).slice(1);return i.map(r,function(t,e){if(e===r.length-1)return t||null;return t?decodeURIComponent(t):null})}});var M=e.History=function(){this.handlers=[];this.checkUrl=i.bind(this.checkUrl,this);if(typeof window!=="undefined"){this.location=window.location;this.history=window.history}};var N=/^[#\/]|\s+$/g;var O=/^\/+|\/+$/g;var U=/#.*$/;M.started=false;i.extend(M.prototype,l,{interval:50,atRoot:function(){var t=this.location.pathname.replace(/[^\/]$/,"$&/");return t===this.root&&!this.getSearch()},matchRoot:function(){var t=this.decodeFragment(this.location.pathname);var e=t.slice(0,this.root.length-1)+"/";return e===this.root},decodeFragment:function(t){return decodeURI(t.replace(/%25/g,"%2525"))},getSearch:function(){var t=this.location.href.replace(/#.*/,"").match(/\?.+/);return t?t[0]:""},getHash:function(t){var e=(t||this).location.href.match(/#(.*)$/);return e?e[1]:""},getPath:function(){var t=this.decodeFragment(this.location.pathname+this.getSearch()).slice(this.root.length-1);return t.charAt(0)==="/"?t.slice(1):t},getFragment:function(t){if(t==null){if(this._usePushState||!this._wantsHashChange){t=this.getPath()}else{t=this.getHash()}}return t.replace(N,"")},start:function(t){if(M.started)throw new Error("Backbone.history has already been started");M.started=true;this.options=i.extend({root:"/"},this.options,t);this.root=this.options.root;this._wantsHashChange=this.options.hashChange!==false;this._hasHashChange="onhashchange"in window&&(document.documentMode===void 0||document.documentMode>7);this._useHashChange=this._wantsHashChange&&this._hasHashChange;this._wantsPushState=!!this.options.pushState;this._hasPushState=!!(this.history&&this.history.pushState);this._usePushState=this._wantsPushState&&this._hasPushState;this.fragment=this.getFragment();this.root=("/"+this.root+"/").replace(O,"/");if(this._wantsHashChange&&this._wantsPushState){if(!this._hasPushState&&!this.atRoot()){var e=this.root.slice(0,-1)||"/";this.location.replace(e+"#"+this.getPath());return true}else if(this._hasPushState&&this.atRoot()){this.navigate(this.getHash(),{replace:true})}}if(!this._hasHashChange&&this._wantsHashChange&&!this._usePushState){this.iframe=document.createElement("iframe");this.iframe.src="javascript:0";this.iframe.style.display="none";this.iframe.tabIndex=-1;var r=document.body;var n=r.insertBefore(this.iframe,r.firstChild).contentWindow;n.document.open();n.document.close();n.location.hash="#"+this.fragment}var s=window.addEventListener||function(t,e){return attachEvent("on"+t,e)};if(this._usePushState){s("popstate",this.checkUrl,false)}else if(this._useHashChange&&!this.iframe){s("hashchange",this.checkUrl,false)}else if(this._wantsHashChange){this._checkUrlInterval=setInterval(this.checkUrl,this.interval)}if(!this.options.silent)return this.loadUrl()},stop:function(){var t=window.removeEventListener||function(t,e){return detachEvent("on"+t,e)};if(this._usePushState){t("popstate",this.checkUrl,false)}else if(this._useHashChange&&!this.iframe){t("hashchange",this.checkUrl,false)}if(this.iframe){document.body.removeChild(this.iframe);this.iframe=null}if(this._checkUrlInterval)clearInterval(this._checkUrlInterval);M.started=false},route:function(t,e){this.handlers.unshift({route:t,callback:e})},checkUrl:function(t){var e=this.getFragment();if(e===this.fragment&&this.iframe){e=this.getHash(this.iframe.contentWindow)}if(e===this.fragment)return false;if(this.iframe)this.navigate(e);this.loadUrl()},loadUrl:function(t){if(!this.matchRoot())return false;t=this.fragment=this.getFragment(t);return i.some(this.handlers,function(e){if(e.route.test(t)){e.callback(t);return true}})},navigate:function(t,e){if(!M.started)return false;if(!e||e===true)e={trigger:!!e};t=this.getFragment(t||"");var i=this.root;if(t===""||t.charAt(0)==="?"){i=i.slice(0,-1)||"/"}var r=i+t;t=this.decodeFragment(t.replace(U,""));if(this.fragment===t)return;this.fragment=t;if(this._usePushState){this.history[e.replace?"replaceState":"pushState"]({},document.title,r)}else if(this._wantsHashChange){this._updateHash(this.location,t,e.replace);if(this.iframe&&t!==this.getHash(this.iframe.contentWindow)){var n=this.iframe.contentWindow;if(!e.replace){n.document.open();n.document.close()}this._updateHash(n.location,t,e.replace)}}else{return this.location.assign(r)}if(e.trigger)return this.loadUrl(t)},_updateHash:function(t,e,i){if(i){var r=t.href.replace(/(javascript:|#).*$/,"");t.replace(r+"#"+e)}else{t.hash="#"+e}}});e.history=new M;var q=function(t,e){var r=this;var n;if(t&&i.has(t,"constructor")){n=t.constructor}else{n=function(){return r.apply(this,arguments)}}i.extend(n,r,e);var s=function(){this.constructor=n};s.prototype=r.prototype;n.prototype=new s;if(t)i.extend(n.prototype,t);n.__super__=r.prototype;return n};y.extend=x.extend=$.extend=I.extend=M.extend=q;var F=function(){throw new Error('A "url" property or function must be specified')};var z=function(t,e){var i=e.error;e.error=function(r){if(i)i.call(e.context,t,r,e);t.trigger("error",t,r,e)}};return e});
+(function(t){var e=typeof self=="object"&&self.self===self&&self||typeof global=="object"&&global.global===global&&global;if(typeof define==="function"&&define.amd){define(["underscore","jquery","exports"],function(i,n,r){e.Backbone=t(e,r,i,n)})}else if(typeof exports!=="undefined"){var i=require("underscore"),n;try{n=require("jquery")}catch(r){}t(e,exports,i,n)}else{e.Backbone=t(e,{},e._,e.jQuery||e.Zepto||e.ender||e.$)}})(function(t,e,i,n){var r=t.Backbone;var s=Array.prototype.slice;e.VERSION="1.4.0";e.$=n;e.noConflict=function(){t.Backbone=r;return this};e.emulateHTTP=false;e.emulateJSON=false;var a=e.Events={};var o=/\s+/;var h;var u=function(t,e,n,r,s){var a=0,h;if(n&&typeof n==="object"){if(r!==void 0&&"context"in s&&s.context===void 0)s.context=r;for(h=i.keys(n);a<h.length;a++){e=u(t,e,h[a],n[h[a]],s)}}else if(n&&o.test(n)){for(h=n.split(o);a<h.length;a++){e=t(e,h[a],r,s)}}else{e=t(e,n,r,s)}return e};a.on=function(t,e,i){this._events=u(l,this._events||{},t,e,{context:i,ctx:this,listening:h});if(h){var n=this._listeners||(this._listeners={});n[h.id]=h;h.interop=false}return this};a.listenTo=function(t,e,n){if(!t)return this;var r=t._listenId||(t._listenId=i.uniqueId("l"));var s=this._listeningTo||(this._listeningTo={});var a=h=s[r];if(!a){this._listenId||(this._listenId=i.uniqueId("l"));a=h=s[r]=new g(this,t)}var o=c(t,e,n,this);h=void 0;if(o)throw o;if(a.interop)a.on(e,n);return this};var l=function(t,e,i,n){if(i){var r=t[e]||(t[e]=[]);var s=n.context,a=n.ctx,o=n.listening;if(o)o.count++;r.push({callback:i,context:s,ctx:s||a,listening:o})}return t};var c=function(t,e,i,n){try{t.on(e,i,n)}catch(r){return r}};a.off=function(t,e,i){if(!this._events)return this;this._events=u(f,this._events,t,e,{context:i,listeners:this._listeners});return this};a.stopListening=function(t,e,n){var r=this._listeningTo;if(!r)return this;var s=t?[t._listenId]:i.keys(r);for(var a=0;a<s.length;a++){var o=r[s[a]];if(!o)break;o.obj.off(e,n,this);if(o.interop)o.off(e,n)}if(i.isEmpty(r))this._listeningTo=void 0;return this};var f=function(t,e,n,r){if(!t)return;var s=r.context,a=r.listeners;var o=0,h;if(!e&&!s&&!n){for(h=i.keys(a);o<h.length;o++){a[h[o]].cleanup()}return}h=e?[e]:i.keys(t);for(;o<h.length;o++){e=h[o];var u=t[e];if(!u)break;var l=[];for(var c=0;c<u.length;c++){var f=u[c];if(n&&n!==f.callback&&n!==f.callback._callback||s&&s!==f.context){l.push(f)}else{var d=f.listening;if(d)d.off(e,n)}}if(l.length){t[e]=l}else{delete t[e]}}return t};a.once=function(t,e,i){var n=u(d,{},t,e,this.off.bind(this));if(typeof t==="string"&&i==null)e=void 0;return this.on(n,e,i)};a.listenToOnce=function(t,e,i){var n=u(d,{},e,i,this.stopListening.bind(this,t));return this.listenTo(t,n)};var d=function(t,e,n,r){if(n){var s=t[e]=i.once(function(){r(e,s);n.apply(this,arguments)});s._callback=n}return t};a.trigger=function(t){if(!this._events)return this;var e=Math.max(0,arguments.length-1);var i=Array(e);for(var n=0;n<e;n++)i[n]=arguments[n+1];u(v,this._events,t,void 0,i);return this};var v=function(t,e,i,n){if(t){var r=t[e];var s=t.all;if(r&&s)s=s.slice();if(r)p(r,n);if(s)p(s,[e].concat(n))}return t};var p=function(t,e){var i,n=-1,r=t.length,s=e[0],a=e[1],o=e[2];switch(e.length){case 0:while(++n<r)(i=t[n]).callback.call(i.ctx);return;case 1:while(++n<r)(i=t[n]).callback.call(i.ctx,s);return;case 2:while(++n<r)(i=t[n]).callback.call(i.ctx,s,a);return;case 3:while(++n<r)(i=t[n]).callback.call(i.ctx,s,a,o);return;default:while(++n<r)(i=t[n]).callback.apply(i.ctx,e);return}};var g=function(t,e){this.id=t._listenId;this.listener=t;this.obj=e;this.interop=true;this.count=0;this._events=void 0};g.prototype.on=a.on;g.prototype.off=function(t,e){var i;if(this.interop){this._events=u(f,this._events,t,e,{context:void 0,listeners:void 0});i=!this._events}else{this.count--;i=this.count===0}if(i)this.cleanup()};g.prototype.cleanup=function(){delete this.listener._listeningTo[this.obj._listenId];if(!this.interop)delete this.obj._listeners[this.id]};a.bind=a.on;a.unbind=a.off;i.extend(e,a);var m=e.Model=function(t,e){var n=t||{};e||(e={});this.preinitialize.apply(this,arguments);this.cid=i.uniqueId(this.cidPrefix);this.attributes={};if(e.collection)this.collection=e.collection;if(e.parse)n=this.parse(n,e)||{};var r=i.result(this,"defaults");n=i.defaults(i.extend({},r,n),r);this.set(n,e);this.changed={};this.initialize.apply(this,arguments)};i.extend(m.prototype,a,{changed:null,validationError:null,idAttribute:"id",cidPrefix:"c",preinitialize:function(){},initialize:function(){},toJSON:function(t){return i.clone(this.attributes)},sync:function(){return e.sync.apply(this,arguments)},get:function(t){return this.attributes[t]},escape:function(t){return i.escape(this.get(t))},has:function(t){return this.get(t)!=null},matches:function(t){return!!i.iteratee(t,this)(this.attributes)},set:function(t,e,n){if(t==null)return this;var r;if(typeof t==="object"){r=t;n=e}else{(r={})[t]=e}n||(n={});if(!this._validate(r,n))return false;var s=n.unset;var a=n.silent;var o=[];var h=this._changing;this._changing=true;if(!h){this._previousAttributes=i.clone(this.attributes);this.changed={}}var u=this.attributes;var l=this.changed;var c=this._previousAttributes;for(var f in r){e=r[f];if(!i.isEqual(u[f],e))o.push(f);if(!i.isEqual(c[f],e)){l[f]=e}else{delete l[f]}s?delete u[f]:u[f]=e}if(this.idAttribute in r)this.id=this.get(this.idAttribute);if(!a){if(o.length)this._pending=n;for(var d=0;d<o.length;d++){this.trigger("change:"+o[d],this,u[o[d]],n)}}if(h)return this;if(!a){while(this._pending){n=this._pending;this._pending=false;this.trigger("change",this,n)}}this._pending=false;this._changing=false;return this},unset:function(t,e){return this.set(t,void 0,i.extend({},e,{unset:true}))},clear:function(t){var e={};for(var n in this.attributes)e[n]=void 0;return this.set(e,i.extend({},t,{unset:true}))},hasChanged:function(t){if(t==null)return!i.isEmpty(this.changed);return i.has(this.changed,t)},changedAttributes:function(t){if(!t)return this.hasChanged()?i.clone(this.changed):false;var e=this._changing?this._previousAttributes:this.attributes;var n={};var r;for(var s in t){var a=t[s];if(i.isEqual(e[s],a))continue;n[s]=a;r=true}return r?n:false},previous:function(t){if(t==null||!this._previousAttributes)return null;return this._previousAttributes[t]},previousAttributes:function(){return i.clone(this._previousAttributes)},fetch:function(t){t=i.extend({parse:true},t);var e=this;var n=t.success;t.success=function(i){var r=t.parse?e.parse(i,t):i;if(!e.set(r,t))return false;if(n)n.call(t.context,e,i,t);e.trigger("sync",e,i,t)};G(this,t);return this.sync("read",this,t)},save:function(t,e,n){var r;if(t==null||typeof t==="object"){r=t;n=e}else{(r={})[t]=e}n=i.extend({validate:true,parse:true},n);var s=n.wait;if(r&&!s){if(!this.set(r,n))return false}else if(!this._validate(r,n)){return false}var a=this;var o=n.success;var h=this.attributes;n.success=function(t){a.attributes=h;var e=n.parse?a.parse(t,n):t;if(s)e=i.extend({},r,e);if(e&&!a.set(e,n))return false;if(o)o.call(n.context,a,t,n);a.trigger("sync",a,t,n)};G(this,n);if(r&&s)this.attributes=i.extend({},h,r);var u=this.isNew()?"create":n.patch?"patch":"update";if(u==="patch"&&!n.attrs)n.attrs=r;var l=this.sync(u,this,n);this.attributes=h;return l},destroy:function(t){t=t?i.clone(t):{};var e=this;var n=t.success;var r=t.wait;var s=function(){e.stopListening();e.trigger("destroy",e,e.collection,t)};t.success=function(i){if(r)s();if(n)n.call(t.context,e,i,t);if(!e.isNew())e.trigger("sync",e,i,t)};var a=false;if(this.isNew()){i.defer(t.success)}else{G(this,t);a=this.sync("delete",this,t)}if(!r)s();return a},url:function(){var t=i.result(this,"urlRoot")||i.result(this.collection,"url")||V();if(this.isNew())return t;var e=this.get(this.idAttribute);return t.replace(/[^\/]$/,"$&/")+encodeURIComponent(e)},parse:function(t,e){return t},clone:function(){return new this.constructor(this.attributes)},isNew:function(){return!this.has(this.idAttribute)},isValid:function(t){return this._validate({},i.extend({},t,{validate:true}))},_validate:function(t,e){if(!e.validate||!this.validate)return true;t=i.extend({},this.attributes,t);var n=this.validationError=this.validate(t,e)||null;if(!n)return true;this.trigger("invalid",this,n,i.extend(e,{validationError:n}));return false}});var _=e.Collection=function(t,e){e||(e={});this.preinitialize.apply(this,arguments);if(e.model)this.model=e.model;if(e.comparator!==void 0)this.comparator=e.comparator;this._reset();this.initialize.apply(this,arguments);if(t)this.reset(t,i.extend({silent:true},e))};var y={add:true,remove:true,merge:true};var b={add:true,remove:false};var x=function(t,e,i){i=Math.min(Math.max(i,0),t.length);var n=Array(t.length-i);var r=e.length;var s;for(s=0;s<n.length;s++)n[s]=t[s+i];for(s=0;s<r;s++)t[s+i]=e[s];for(s=0;s<n.length;s++)t[s+r+i]=n[s]};i.extend(_.prototype,a,{model:m,preinitialize:function(){},initialize:function(){},toJSON:function(t){return this.map(function(e){return e.toJSON(t)})},sync:function(){return e.sync.apply(this,arguments)},add:function(t,e){return this.set(t,i.extend({merge:false},e,b))},remove:function(t,e){e=i.extend({},e);var n=!i.isArray(t);t=n?[t]:t.slice();var r=this._removeModels(t,e);if(!e.silent&&r.length){e.changes={added:[],merged:[],removed:r};this.trigger("update",this,e)}return n?r[0]:r},set:function(t,e){if(t==null)return;e=i.extend({},y,e);if(e.parse&&!this._isModel(t)){t=this.parse(t,e)||[]}var n=!i.isArray(t);t=n?[t]:t.slice();var r=e.at;if(r!=null)r=+r;if(r>this.length)r=this.length;if(r<0)r+=this.length+1;var s=[];var a=[];var o=[];var h=[];var u={};var l=e.add;var c=e.merge;var f=e.remove;var d=false;var v=this.comparator&&r==null&&e.sort!==false;var p=i.isString(this.comparator)?this.comparator:null;var g,m;for(m=0;m<t.length;m++){g=t[m];var _=this.get(g);if(_){if(c&&g!==_){var b=this._isModel(g)?g.attributes:g;if(e.parse)b=_.parse(b,e);_.set(b,e);o.push(_);if(v&&!d)d=_.hasChanged(p)}if(!u[_.cid]){u[_.cid]=true;s.push(_)}t[m]=_}else if(l){g=t[m]=this._prepareModel(g,e);if(g){a.push(g);this._addReference(g,e);u[g.cid]=true;s.push(g)}}}if(f){for(m=0;m<this.length;m++){g=this.models[m];if(!u[g.cid])h.push(g)}if(h.length)this._removeModels(h,e)}var w=false;var E=!v&&l&&f;if(s.length&&E){w=this.length!==s.length||i.some(this.models,function(t,e){return t!==s[e]});this.models.length=0;x(this.models,s,0);this.length=this.models.length}else if(a.length){if(v)d=true;x(this.models,a,r==null?this.length:r);this.length=this.models.length}if(d)this.sort({silent:true});if(!e.silent){for(m=0;m<a.length;m++){if(r!=null)e.index=r+m;g=a[m];g.trigger("add",g,this,e)}if(d||w)this.trigger("sort",this,e);if(a.length||h.length||o.length){e.changes={added:a,removed:h,merged:o};this.trigger("update",this,e)}}return n?t[0]:t},reset:function(t,e){e=e?i.clone(e):{};for(var n=0;n<this.models.length;n++){this._removeReference(this.models[n],e)}e.previousModels=this.models;this._reset();t=this.add(t,i.extend({silent:true},e));if(!e.silent)this.trigger("reset",this,e);return t},push:function(t,e){return this.add(t,i.extend({at:this.length},e))},pop:function(t){var e=this.at(this.length-1);return this.remove(e,t)},unshift:function(t,e){return this.add(t,i.extend({at:0},e))},shift:function(t){var e=this.at(0);return this.remove(e,t)},slice:function(){return s.apply(this.models,arguments)},get:function(t){if(t==null)return void 0;return this._byId[t]||this._byId[this.modelId(this._isModel(t)?t.attributes:t)]||t.cid&&this._byId[t.cid]},has:function(t){return this.get(t)!=null},at:function(t){if(t<0)t+=this.length;return this.models[t]},where:function(t,e){return this[e?"find":"filter"](t)},findWhere:function(t){return this.where(t,true)},sort:function(t){var e=this.comparator;if(!e)throw new Error("Cannot sort a set without a comparator");t||(t={});var n=e.length;if(i.isFunction(e))e=e.bind(this);if(n===1||i.isString(e)){this.models=this.sortBy(e)}else{this.models.sort(e)}if(!t.silent)this.trigger("sort",this,t);return this},pluck:function(t){return this.map(t+"")},fetch:function(t){t=i.extend({parse:true},t);var e=t.success;var n=this;t.success=function(i){var r=t.reset?"reset":"set";n[r](i,t);if(e)e.call(t.context,n,i,t);n.trigger("sync",n,i,t)};G(this,t);return this.sync("read",this,t)},create:function(t,e){e=e?i.clone(e):{};var n=e.wait;t=this._prepareModel(t,e);if(!t)return false;if(!n)this.add(t,e);var r=this;var s=e.success;e.success=function(t,e,i){if(n)r.add(t,i);if(s)s.call(i.context,t,e,i)};t.save(null,e);return t},parse:function(t,e){return t},clone:function(){return new this.constructor(this.models,{model:this.model,comparator:this.comparator})},modelId:function(t){return t[this.model.prototype.idAttribute||"id"]},values:function(){return new E(this,k)},keys:function(){return new E(this,I)},entries:function(){return new E(this,S)},_reset:function(){this.length=0;this.models=[];this._byId={}},_prepareModel:function(t,e){if(this._isModel(t)){if(!t.collection)t.collection=this;return t}e=e?i.clone(e):{};e.collection=this;var n=new this.model(t,e);if(!n.validationError)return n;this.trigger("invalid",this,n.validationError,e);return false},_removeModels:function(t,e){var i=[];for(var n=0;n<t.length;n++){var r=this.get(t[n]);if(!r)continue;var s=this.indexOf(r);this.models.splice(s,1);this.length--;delete this._byId[r.cid];var a=this.modelId(r.attributes);if(a!=null)delete this._byId[a];if(!e.silent){e.index=s;r.trigger("remove",r,this,e)}i.push(r);this._removeReference(r,e)}return i},_isModel:function(t){return t instanceof m},_addReference:function(t,e){this._byId[t.cid]=t;var i=this.modelId(t.attributes);if(i!=null)this._byId[i]=t;t.on("all",this._onModelEvent,this)},_removeReference:function(t,e){delete this._byId[t.cid];var i=this.modelId(t.attributes);if(i!=null)delete this._byId[i];if(this===t.collection)delete t.collection;t.off("all",this._onModelEvent,this)},_onModelEvent:function(t,e,i,n){if(e){if((t==="add"||t==="remove")&&i!==this)return;if(t==="destroy")this.remove(e,n);if(t==="change"){var r=this.modelId(e.previousAttributes());var s=this.modelId(e.attributes);if(r!==s){if(r!=null)delete this._byId[r];if(s!=null)this._byId[s]=e}}}this.trigger.apply(this,arguments)}});var w=typeof Symbol==="function"&&Symbol.iterator;if(w){_.prototype[w]=_.prototype.values}var E=function(t,e){this._collection=t;this._kind=e;this._index=0};var k=1;var I=2;var S=3;if(w){E.prototype[w]=function(){return this}}E.prototype.next=function(){if(this._collection){if(this._index<this._collection.length){var t=this._collection.at(this._index);this._index++;var e;if(this._kind===k){e=t}else{var i=this._collection.modelId(t.attributes);if(this._kind===I){e=i}else{e=[i,t]}}return{value:e,done:false}}this._collection=void 0}return{value:void 0,done:true}};var T=e.View=function(t){this.cid=i.uniqueId("view");this.preinitialize.apply(this,arguments);i.extend(this,i.pick(t,H));this._ensureElement();this.initialize.apply(this,arguments)};var P=/^(\S+)\s*(.*)$/;var H=["model","collection","el","id","attributes","className","tagName","events"];i.extend(T.prototype,a,{tagName:"div",$:function(t){return this.$el.find(t)},preinitialize:function(){},initialize:function(){},render:function(){return this},remove:function(){this._removeElement();this.stopListening();return this},_removeElement:function(){this.$el.remove()},setElement:function(t){this.undelegateEvents();this._setElement(t);this.delegateEvents();return this},_setElement:function(t){this.$el=t instanceof e.$?t:e.$(t);this.el=this.$el[0]},delegateEvents:function(t){t||(t=i.result(this,"events"));if(!t)return this;this.undelegateEvents();for(var e in t){var n=t[e];if(!i.isFunction(n))n=this[n];if(!n)continue;var r=e.match(P);this.delegate(r[1],r[2],n.bind(this))}return this},delegate:function(t,e,i){this.$el.on(t+".delegateEvents"+this.cid,e,i);return this},undelegateEvents:function(){if(this.$el)this.$el.off(".delegateEvents"+this.cid);return this},undelegate:function(t,e,i){this.$el.off(t+".delegateEvents"+this.cid,e,i);return this},_createElement:function(t){return document.createElement(t)},_ensureElement:function(){if(!this.el){var t=i.extend({},i.result(this,"attributes"));if(this.id)t.id=i.result(this,"id");if(this.className)t["class"]=i.result(this,"className");this.setElement(this._createElement(i.result(this,"tagName")));this._setAttributes(t)}else{this.setElement(i.result(this,"el"))}},_setAttributes:function(t){this.$el.attr(t)}});var $=function(t,e,i,n){switch(e){case 1:return function(){return t[i](this[n])};case 2:return function(e){return t[i](this[n],e)};case 3:return function(e,r){return t[i](this[n],C(e,this),r)};case 4:return function(e,r,s){return t[i](this[n],C(e,this),r,s)};default:return function(){var e=s.call(arguments);e.unshift(this[n]);return t[i].apply(t,e)}}};var A=function(t,e,n,r){i.each(n,function(i,n){if(e[n])t.prototype[n]=$(e,i,n,r)})};var C=function(t,e){if(i.isFunction(t))return t;if(i.isObject(t)&&!e._isModel(t))return R(t);if(i.isString(t))return function(e){return e.get(t)};return t};var R=function(t){var e=i.matches(t);return function(t){return e(t.attributes)}};var M={forEach:3,each:3,map:3,collect:3,reduce:0,foldl:0,inject:0,reduceRight:0,foldr:0,find:3,detect:3,filter:3,select:3,reject:3,every:3,all:3,some:3,any:3,include:3,includes:3,contains:3,invoke:0,max:3,min:3,toArray:1,size:1,first:3,head:3,take:3,initial:3,rest:3,tail:3,drop:3,last:3,without:0,difference:0,indexOf:3,shuffle:1,lastIndexOf:3,isEmpty:1,chain:1,sample:3,partition:3,groupBy:3,countBy:3,sortBy:3,indexBy:3,findIndex:3,findLastIndex:3};var N={keys:1,values:1,pairs:1,invert:1,pick:0,omit:0,chain:1,isEmpty:1};i.each([[_,M,"models"],[m,N,"attributes"]],function(t){var e=t[0],n=t[1],r=t[2];e.mixin=function(t){var n=i.reduce(i.functions(t),function(t,e){t[e]=0;return t},{});A(e,t,n,r)};A(e,i,n,r)});e.sync=function(t,n,r){var s=j[t];i.defaults(r||(r={}),{emulateHTTP:e.emulateHTTP,emulateJSON:e.emulateJSON});var a={type:s,dataType:"json"};if(!r.url){a.url=i.result(n,"url")||V()}if(r.data==null&&n&&(t==="create"||t==="update"||t==="patch")){a.contentType="application/json";a.data=JSON.stringify(r.attrs||n.toJSON(r))}if(r.emulateJSON){a.contentType="application/x-www-form-urlencoded";a.data=a.data?{model:a.data}:{}}if(r.emulateHTTP&&(s==="PUT"||s==="DELETE"||s==="PATCH")){a.type="POST";if(r.emulateJSON)a.data._method=s;var o=r.beforeSend;r.beforeSend=function(t){t.setRequestHeader("X-HTTP-Method-Override",s);if(o)return o.apply(this,arguments)}}if(a.type!=="GET"&&!r.emulateJSON){a.processData=false}var h=r.error;r.error=function(t,e,i){r.textStatus=e;r.errorThrown=i;if(h)h.call(r.context,t,e,i)};var u=r.xhr=e.ajax(i.extend(a,r));n.trigger("request",n,u,r);return u};var j={create:"POST",update:"PUT",patch:"PATCH","delete":"DELETE",read:"GET"};e.ajax=function(){return e.$.ajax.apply(e.$,arguments)};var O=e.Router=function(t){t||(t={});this.preinitialize.apply(this,arguments);if(t.routes)this.routes=t.routes;this._bindRoutes();this.initialize.apply(this,arguments)};var U=/\((.*?)\)/g;var z=/(\(\?)?:\w+/g;var q=/\*\w+/g;var F=/[\-{}\[\]+?.,\\\^$|#\s]/g;i.extend(O.prototype,a,{preinitialize:function(){},initialize:function(){},route:function(t,n,r){if(!i.isRegExp(t))t=this._routeToRegExp(t);if(i.isFunction(n)){r=n;n=""}if(!r)r=this[n];var s=this;e.history.route(t,function(i){var a=s._extractParameters(t,i);if(s.execute(r,a,n)!==false){s.trigger.apply(s,["route:"+n].concat(a));s.trigger("route",n,a);e.history.trigger("route",s,n,a)}});return this},execute:function(t,e,i){if(t)t.apply(this,e)},navigate:function(t,i){e.history.navigate(t,i);return this},_bindRoutes:function(){if(!this.routes)return;this.routes=i.result(this,"routes");var t,e=i.keys(this.routes);while((t=e.pop())!=null){this.route(t,this.routes[t])}},_routeToRegExp:function(t){t=t.replace(F,"\\$&").replace(U,"(?:$1)?").replace(z,function(t,e){return e?t:"([^/?]+)"}).replace(q,"([^?]*?)");return new RegExp("^"+t+"(?:\\?([\\s\\S]*))?$")},_extractParameters:function(t,e){var n=t.exec(e).slice(1);return i.map(n,function(t,e){if(e===n.length-1)return t||null;return t?decodeURIComponent(t):null})}});var B=e.History=function(){this.handlers=[];this.checkUrl=this.checkUrl.bind(this);if(typeof window!=="undefined"){this.location=window.location;this.history=window.history}};var J=/^[#\/]|\s+$/g;var L=/^\/+|\/+$/g;var W=/#.*$/;B.started=false;i.extend(B.prototype,a,{interval:50,atRoot:function(){var t=this.location.pathname.replace(/[^\/]$/,"$&/");return t===this.root&&!this.getSearch()},matchRoot:function(){var t=this.decodeFragment(this.location.pathname);var e=t.slice(0,this.root.length-1)+"/";return e===this.root},decodeFragment:function(t){return decodeURI(t.replace(/%25/g,"%2525"))},getSearch:function(){var t=this.location.href.replace(/#.*/,"").match(/\?.+/);return t?t[0]:""},getHash:function(t){var e=(t||this).location.href.match(/#(.*)$/);return e?e[1]:""},getPath:function(){var t=this.decodeFragment(this.location.pathname+this.getSearch()).slice(this.root.length-1);return t.charAt(0)==="/"?t.slice(1):t},getFragment:function(t){if(t==null){if(this._usePushState||!this._wantsHashChange){t=this.getPath()}else{t=this.getHash()}}return t.replace(J,"")},start:function(t){if(B.started)throw new Error("Backbone.history has already been started");B.started=true;this.options=i.extend({root:"/"},this.options,t);this.root=this.options.root;this._wantsHashChange=this.options.hashChange!==false;this._hasHashChange="onhashchange"in window&&(document.documentMode===void 0||document.documentMode>7);this._useHashChange=this._wantsHashChange&&this._hasHashChange;this._wantsPushState=!!this.options.pushState;this._hasPushState=!!(this.history&&this.history.pushState);this._usePushState=this._wantsPushState&&this._hasPushState;this.fragment=this.getFragment();this.root=("/"+this.root+"/").replace(L,"/");if(this._wantsHashChange&&this._wantsPushState){if(!this._hasPushState&&!this.atRoot()){var e=this.root.slice(0,-1)||"/";this.location.replace(e+"#"+this.getPath());return true}else if(this._hasPushState&&this.atRoot()){this.navigate(this.getHash(),{replace:true})}}if(!this._hasHashChange&&this._wantsHashChange&&!this._usePushState){this.iframe=document.createElement("iframe");this.iframe.src="javascript:0";this.iframe.style.display="none";this.iframe.tabIndex=-1;var n=document.body;var r=n.insertBefore(this.iframe,n.firstChild).contentWindow;r.document.open();r.document.close();r.location.hash="#"+this.fragment}var s=window.addEventListener||function(t,e){return attachEvent("on"+t,e)};if(this._usePushState){s("popstate",this.checkUrl,false)}else if(this._useHashChange&&!this.iframe){s("hashchange",this.checkUrl,false)}else if(this._wantsHashChange){this._checkUrlInterval=setInterval(this.checkUrl,this.interval)}if(!this.options.silent)return this.loadUrl()},stop:function(){var t=window.removeEventListener||function(t,e){return detachEvent("on"+t,e)};if(this._usePushState){t("popstate",this.checkUrl,false)}else if(this._useHashChange&&!this.iframe){t("hashchange",this.checkUrl,false)}if(this.iframe){document.body.removeChild(this.iframe);this.iframe=null}if(this._checkUrlInterval)clearInterval(this._checkUrlInterval);B.started=false},route:function(t,e){this.handlers.unshift({route:t,callback:e})},checkUrl:function(t){var e=this.getFragment();if(e===this.fragment&&this.iframe){e=this.getHash(this.iframe.contentWindow)}if(e===this.fragment)return false;if(this.iframe)this.navigate(e);this.loadUrl()},loadUrl:function(t){if(!this.matchRoot())return false;t=this.fragment=this.getFragment(t);return i.some(this.handlers,function(e){if(e.route.test(t)){e.callback(t);return true}})},navigate:function(t,e){if(!B.started)return false;if(!e||e===true)e={trigger:!!e};t=this.getFragment(t||"");var i=this.root;if(t===""||t.charAt(0)==="?"){i=i.slice(0,-1)||"/"}var n=i+t;t=t.replace(W,"");var r=this.decodeFragment(t);if(this.fragment===r)return;this.fragment=r;if(this._usePushState){this.history[e.replace?"replaceState":"pushState"]({},document.title,n)}else if(this._wantsHashChange){this._updateHash(this.location,t,e.replace);if(this.iframe&&t!==this.getHash(this.iframe.contentWindow)){var s=this.iframe.contentWindow;if(!e.replace){s.document.open();s.document.close()}this._updateHash(s.location,t,e.replace)}}else{return this.location.assign(n)}if(e.trigger)return this.loadUrl(t)},_updateHash:function(t,e,i){if(i){var n=t.href.replace(/(javascript:|#).*$/,"");t.replace(n+"#"+e)}else{t.hash="#"+e}}});e.history=new B;var D=function(t,e){var n=this;var r;if(t&&i.has(t,"constructor")){r=t.constructor}else{r=function(){return n.apply(this,arguments)}}i.extend(r,n,e);r.prototype=i.create(n.prototype,t);r.prototype.constructor=r;r.__super__=n.prototype;return r};m.extend=_.extend=O.extend=T.extend=B.extend=D;var V=function(){throw new Error('A "url" property or function must be specified')};var G=function(t,e){var i=e.error;e.error=function(n){if(i)i.call(e.context,t,n,e);t.trigger("error",t,n,e)}};return e});
//# sourceMappingURL=backbone-min.map
\ No newline at end of file
--- a/web/res/js/backbone-relational.js Wed Dec 11 10:17:08 2019 +0100
+++ b/web/res/js/backbone-relational.js Wed Dec 11 11:02:15 2019 +0100
@@ -1,29 +1,65 @@
/* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */
/**
- * Backbone-relational.js 0.8.5
- * (c) 2011-2013 Paul Uithol and contributors (https://github.com/PaulUithol/Backbone-relational/graphs/contributors)
- *
+ * Backbone-relational.js 0.10.0
+ * (c) 2011-2014 Paul Uithol and contributors (https://github.com/PaulUithol/Backbone-relational/graphs/contributors)
+ *
* Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
* For details and documentation: https://github.com/PaulUithol/Backbone-relational.
* Depends on Backbone (and thus on Underscore as well): https://github.com/documentcloud/backbone.
- */
-( function( undefined ) {
- "use strict";
+ *
+ * Example:
+ *
+ Zoo = Backbone.RelationalModel.extend({
+ relations: [ {
+ type: Backbone.HasMany,
+ key: 'animals',
+ relatedModel: 'Animal',
+ reverseRelation: {
+ key: 'livesIn',
+ includeInJSON: 'id'
+ // 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
+ }
+ } ],
+
+ toString: function() {
+ return this.get( 'name' );
+ }
+ });
+
+ Animal = Backbone.RelationalModel.extend({
+ toString: function() {
+ return this.get( 'species' );
+ }
+ });
- /**
- * CommonJS shim
- **/
- var _, Backbone, exports;
- if ( typeof window === 'undefined' ) {
- _ = require( 'underscore' );
- Backbone = require( 'backbone' );
- exports = module.exports = Backbone;
+ // Creating the zoo will give it a collection with one animal in it: the monkey.
+ // The animal created after that has a relation `livesIn` that points to the zoo it's currently associated with.
+ // If you instantiate (or fetch) the zebra later, it will automatically be added.
+
+ var zoo = new Zoo({
+ name: 'Artis',
+ animals: [ { id: 'monkey-1', species: 'Chimp' }, 'lion-1', 'zebra-1' ]
+ });
+
+ var lion = new Animal( { id: 'lion-1', species: 'Lion' } ),
+ monkey = zoo.get( 'animals' ).first(),
+ sameZoo = lion.get( 'livesIn' );
+ */
+( function( root, factory ) {
+ // Set up Backbone-relational for the environment. Start with AMD.
+ if ( typeof define === 'function' && define.amd ) {
+ define( [ 'exports', 'backbone', 'underscore' ], factory );
}
+ // Next for Node.js or CommonJS.
+ else if ( typeof exports !== 'undefined' ) {
+ factory( exports, require( 'backbone' ), require( 'underscore' ) );
+ }
+ // Finally, as a browser global. Use `root` here as it references `window`.
else {
- _ = window._;
- Backbone = window.Backbone;
- exports = window;
+ factory( root, root.Backbone, root._ );
}
+}( this, function( exports, Backbone, _ ) {
+ "use strict";
Backbone.Relational = {
showWarnings: true
@@ -86,9 +122,21 @@
}
},
+ // Some of the queued events may trigger other blocking events. By
+ // copying the queue here it allows queued events to process closer to
+ // the natural order.
+ //
+ // queue events [ 'A', 'B', 'C' ]
+ // A handler of 'B' triggers 'D' and 'E'
+ // By copying `this._queue` this executes:
+ // [ 'A', 'B', 'D', 'E', 'C' ]
+ // The same order the would have executed if they didn't have to be
+ // delayed and queued.
process: function() {
- while ( this._queue && this._queue.length ) {
- this._queue.shift()();
+ var queue = this._queue;
+ this._queue = [];
+ while ( queue && queue.length ) {
+ queue.shift()();
}
},
@@ -134,7 +182,7 @@
initializeRelation: function( model, relation, options ) {
var type = !_.isString( relation.type ) ? relation.type : Backbone[ relation.type ] || this.getObjectByName( relation.type );
if ( type && type.prototype instanceof Backbone.Relation ) {
- new type( model, relation, options ); // Also pushes the new Relation into `model._relations`
+ var rel = new type( model, relation, options ); // Also pushes the new Relation into `model._relations`
}
else {
Backbone.Relational.showWarnings && typeof console !== 'undefined' && console.warn( 'Relation=%o; missing or invalid relation type!', relation );
@@ -179,7 +227,7 @@
*/
setupSuperModel: function( modelType ) {
_.find( this._subModels, function( subModelDef ) {
- return _.find( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
+ return _.filter( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
var subModelType = this.getObjectByName( subModelTypeName );
if ( modelType === subModelType ) {
@@ -192,7 +240,7 @@
modelType._subModelTypeAttribute = subModelDef.superModelType.prototype.subModelTypeAttribute;
return true;
}
- }, this );
+ }, this ).length;
}, this );
},
@@ -211,7 +259,7 @@
return val === rel[ key ];
});
});
-
+
if ( !exists && relation.model && relation.type ) {
this._reverseRelations.push( relation );
this._addRelation( relation.model, relation );
@@ -278,7 +326,7 @@
return;
}
- new relation.type( model, relation );
+ var rel = new relation.type( model, relation );
}, this );
},
@@ -292,18 +340,20 @@
if ( type instanceof Backbone.RelationalModel ) {
type = type.constructor;
}
-
+
var rootModel = type;
while ( rootModel._superModel ) {
rootModel = rootModel._superModel;
}
-
- var coll = _.findWhere( this._collections, { model: rootModel } );
-
+
+ var coll = _.find( this._collections, function( item ) {
+ return item.model === rootModel;
+ });
+
if ( !coll && create !== false ) {
coll = this._createCollection( rootModel );
}
-
+
return coll;
},
@@ -331,20 +381,20 @@
_createCollection: function( type ) {
var coll;
-
+
// If 'type' is an instance, take its constructor
if ( type instanceof Backbone.RelationalModel ) {
type = type.constructor;
}
-
+
// Type should inherit from Backbone.RelationalModel.
if ( type.prototype instanceof Backbone.RelationalModel ) {
coll = new Backbone.Collection();
coll.model = type;
-
+
this._collections.push( coll );
}
-
+
return coll;
},
@@ -380,9 +430,9 @@
* @param {String|Number|Object|Backbone.RelationalModel} item
*/
find: function( type, item ) {
- var id = this.resolveIdForItem( type, item );
- var coll = this.getCollection( type );
-
+ var id = this.resolveIdForItem( type, item ),
+ coll = this.getCollection( type );
+
// Because the found object could be of any of the type's superModel
// types, only return it if it's actually of the type asked for.
if ( coll ) {
@@ -406,7 +456,6 @@
if ( coll ) {
var modelColl = model.collection;
coll.add( model );
- this.listenTo( model, 'destroy', this.unregister, this );
model.collection = modelColl;
}
},
@@ -435,6 +484,12 @@
*/
update: function( model ) {
var coll = this.getCollection( model );
+
+ // Register a model if it isn't yet (which happens if it was created without an id).
+ if ( !coll.contains( model ) ) {
+ this.register( model );
+ }
+
// This triggers updating the lookup indices kept in a collection
coll._onModelEvent( 'change:' + model.idAttribute, model, coll );
@@ -443,13 +498,47 @@
},
/**
- * Remove a 'model' from the store.
- * @param {Backbone.RelationalModel} model
+ * Unregister from the store: a specific model, a collection, or a model type.
+ * @param {Backbone.RelationalModel|Backbone.RelationalModel.constructor|Backbone.Collection} type
*/
- unregister: function( model ) {
- this.stopListening( model, 'destroy', this.unregister );
- var coll = this.getCollection( model );
- coll && coll.remove( model );
+ unregister: function( type ) {
+ var coll,
+ models;
+
+ if ( type instanceof Backbone.Model ) {
+ coll = this.getCollection( type );
+ models = [ type ];
+ }
+ else if ( type instanceof Backbone.Collection ) {
+ coll = this.getCollection( type.model );
+ models = _.clone( type.models );
+ }
+ else {
+ coll = this.getCollection( type );
+ models = _.clone( coll.models );
+ }
+
+ _.each( models, function( model ) {
+ this.stopListening( model );
+ _.invoke( model.getRelations(), 'stopListening' );
+ }, this );
+
+
+ // If we've unregistered an entire store collection, reset the collection (which is much faster).
+ // Otherwise, remove each model one by one.
+ if ( _.contains( this._collections, type ) ) {
+ coll.reset( [] );
+ }
+ else {
+ _.each( models, function( model ) {
+ if ( coll.get( model ) ) {
+ coll.remove( model );
+ }
+ else {
+ coll.trigger( 'relational:remove', model, coll );
+ }
+ }, this );
+ }
},
/**
@@ -458,6 +547,12 @@
*/
reset: function() {
this.stopListening();
+
+ // Unregister each collection to remove event listeners
+ _.each( this._collections, function( coll ) {
+ this.unregister( coll );
+ }, this );
+
this._collections = [];
this._subModels = [];
this._modelScopes = [ exports ];
@@ -496,11 +591,23 @@
this.keyDestination = this.options.keyDestination || this.keySource || this.key;
this.model = this.options.model || this.instance.constructor;
+
this.relatedModel = this.options.relatedModel;
+
+ // No 'relatedModel' is interpreted as self-referential
+ if ( _.isUndefined( this.relatedModel ) ) {
+ this.relatedModel = this.model;
+ }
+
+ // Otherwise, try to resolve the given value to an object
+ if ( _.isFunction( this.relatedModel ) && !( this.relatedModel.prototype instanceof Backbone.RelationalModel ) ) {
+ this.relatedModel = _.result( this, 'relatedModel' );
+ }
if ( _.isString( this.relatedModel ) ) {
this.relatedModel = Backbone.Relational.store.getObjectByName( this.relatedModel );
}
+
if ( !this.checkPreconditions() ) {
return;
}
@@ -519,7 +626,7 @@
if ( instance ) {
var contentKey = this.keySource;
- if ( contentKey !== this.key && typeof this.instance.get( this.key ) === 'object' ) {
+ if ( contentKey !== this.key && _.isObject( this.instance.get( this.key ) ) ) {
contentKey = this.key;
}
@@ -528,7 +635,7 @@
// Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'.
if ( this.keySource !== this.key ) {
- this.instance.unset( this.keySource, { silent: true } );
+ delete this.instance.attributes[ this.keySource ];
}
// Add this Relation to instance._relations
@@ -537,13 +644,13 @@
this.initialize( opts );
if ( this.options.autoFetch ) {
- this.instance.fetchRelated( this.key, _.isObject( this.options.autoFetch ) ? this.options.autoFetch : {} );
+ this.instance.getAsync( this.key, _.isObject( this.options.autoFetch ) ? this.options.autoFetch : {} );
}
// When 'relatedModel' are created or destroyed, check if it affects this relation.
this.listenTo( this.instance, 'destroy', this.destroy )
.listenTo( this.relatedCollection, 'relational:add relational:change:id', this.tryAddRelated )
- .listenTo( this.relatedCollection, 'relational:remove', this.removeRelated )
+ .listenTo( this.relatedCollection, 'relational:remove', this.removeRelated );
}
};
// Fix inheritance :\
@@ -618,10 +725,7 @@
*/
setRelated: function( related ) {
this.related = related;
-
- this.instance.acquire();
this.instance.attributes[ this.key ] = related;
- this.instance.release();
},
/**
@@ -644,15 +748,22 @@
getReverseRelations: function( model ) {
var reverseRelations = [];
// Iterate over 'model', 'this.related.models' (if this.related is a Backbone.Collection), or wrap 'this.related' in an array.
- var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] );
- _.each( models || [], function( related ) {
- _.each( related.getRelations() || [], function( relation ) {
- if ( this._isReverseRelation( relation ) ) {
- reverseRelations.push( relation );
- }
- }, this );
- }, this );
-
+ var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] ),
+ relations = null,
+ relation = null;
+
+ for( var i = 0; i < ( models || [] ).length; i++ ) {
+ relations = models[ i ].getRelations() || [];
+
+ for( var j = 0; j < relations.length; j++ ) {
+ relation = relations[ j ];
+
+ if ( this._isReverseRelation( relation ) ) {
+ reverseRelations.push( relation );
+ }
+ }
+ }
+
return reverseRelations;
},
@@ -669,7 +780,7 @@
else if ( this instanceof Backbone.HasMany ) {
this.setRelated( this._prepareCollection() );
}
-
+
_.each( this.getReverseRelations(), function( relation ) {
relation.removeRelated( this.instance );
}, this );
@@ -712,7 +823,7 @@
}
// Nullify `keyId` if we have a related model; in case it was already part of the relation
- if ( this.related ) {
+ if ( related ) {
this.keyId = null;
}
@@ -739,19 +850,19 @@
}
this.acquire();
options = options ? _.clone( options ) : {};
-
+
// 'options.__related' is set by 'addRelated'/'removeRelated'. If it is set, the change
- // is the result of a call from a relation. If it's not, the change is the result of
+ // is the result of a call from a relation. If it's not, the change is the result of
// a 'set' call on this.instance.
var changed = _.isUndefined( options.__related ),
oldRelated = changed ? this.related : options.__related;
-
+
if ( changed ) {
this.setKeyContents( attr );
var related = this.findRelated( options );
this.setRelated( related );
}
-
+
// Notify old 'related' object of the terminated relation
if ( oldRelated && this.related !== oldRelated ) {
_.each( this.getReverseRelations( oldRelated ), function( relation ) {
@@ -765,7 +876,7 @@
_.each( this.getReverseRelations(), function( relation ) {
relation.addRelated( this.instance, options );
}, this );
-
+
// Fire the 'change:<key>' event if 'related' was updated
if ( !options.silent && this.related !== oldRelated ) {
var dit = this;
@@ -805,7 +916,7 @@
if ( !this.related ) {
return;
}
-
+
if ( model === this.related ) {
var oldRelated = this.related || null;
this.setRelated( null );
@@ -826,9 +937,12 @@
initialize: function( opts ) {
this.listenTo( this.instance, 'relational:change:' + this.key, this.onChange );
-
+
// Handle a custom 'collectionType'
this.collectionType = this.options.collectionType;
+ if ( _.isFunction( this.collectionType ) && this.collectionType !== Backbone.Collection && !( this.collectionType.prototype instanceof Backbone.Collection ) ) {
+ this.collectionType = _.result( this, 'collectionType' );
+ }
if ( _.isString( this.collectionType ) ) {
this.collectionType = Backbone.Relational.store.getObjectByName( this.collectionType );
}
@@ -859,10 +973,10 @@
}
collection.model = this.relatedModel;
-
+
if ( this.options.collectionKey ) {
var key = this.options.collectionKey === true ? this.options.reverseRelation.key : this.options.collectionKey;
-
+
if ( collection[ key ] && collection[ key ] !== this.instance ) {
if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) {
console.warn( 'Relation=%o; collectionKey=%s already exists on collection=%o', this, key, this.options.collectionKey );
@@ -876,7 +990,7 @@
this.listenTo( collection, 'relational:add', this.handleAddition )
.listenTo( collection, 'relational:remove', this.handleRemoval )
.listenTo( collection, 'relational:reset', this.handleReset );
-
+
return collection;
},
@@ -901,14 +1015,15 @@
var toAdd = [];
_.each( this.keyContents, function( attributes ) {
+ var model = null;
+
if ( attributes instanceof this.relatedModel ) {
- var model = attributes;
+ model = attributes;
}
else {
// If `merge` is true, update models here, instead of during update.
- model = this.relatedModel.findOrCreate( attributes,
- _.extend( { merge: true }, options, { create: this.options.createModels } )
- );
+ model = ( _.isObject( attributes ) && options.parse && this.relatedModel.prototype.parse ) ?
+ this.relatedModel.prototype.parse( _.clone( attributes ), options ) : attributes;
}
model && toAdd.push( model );
@@ -921,9 +1036,9 @@
related = this._prepareCollection();
}
- // By now, both `merge` and `parse` will already have been executed for models if they were specified.
- // Disable them to prevent additional calls.
- related.set( toAdd, _.defaults( { merge: false, parse: false }, options ) );
+ // By now, `parse` will already have been executed just above for models if specified.
+ // Disable to prevent additional calls.
+ related.set( toAdd, _.defaults( { parse: false }, options ) );
}
// Remove entries from `keyIds` that were already part of the relation (and are thus 'unchanged')
@@ -980,12 +1095,12 @@
/**
* When a model is added to a 'HasMany', trigger 'add' on 'this.instance' and notify reverse relations.
* (should be 'HasOne', must set 'this.instance' as their related).
- */
+ */
handleAddition: function( model, coll, options ) {
//console.debug('handleAddition called; args=%o', arguments);
options = options ? _.clone( options ) : {};
this.changed = true;
-
+
_.each( this.getReverseRelations( model ), function( relation ) {
relation.addRelated( this.instance, options );
}, this );
@@ -1005,14 +1120,14 @@
//console.debug('handleRemoval called; args=%o', arguments);
options = options ? _.clone( options ) : {};
this.changed = true;
-
+
_.each( this.getReverseRelations( model ), function( relation ) {
relation.removeRelated( this.instance, null, options );
}, this );
-
+
var dit = this;
!options.silent && Backbone.Relational.eventQueue.add( function() {
- dit.instance.trigger( 'remove:' + dit.key, model, dit.related, options );
+ dit.instance.trigger( 'remove:' + dit.key, model, dit.related, options );
});
},
@@ -1064,6 +1179,7 @@
_isInitialized: false,
_deferProcessing: false,
_queue: null,
+ _attributeChangeFired: false, // Keeps track of `change` event firing under some conditions (like nested `set`s)
subModelTypeAttribute: 'type',
subModelTypes: null,
@@ -1075,7 +1191,7 @@
// Example: event for "p.on( 'add:jobs' )" -> "p.get('jobs').add( { company: c.id, person: p.id } )".
if ( options && options.collection ) {
var dit = this,
- collection = this.collection = options.collection;
+ collection = this.collection = options.collection;
// Prevent `collection` from cascading down to nested models; they shouldn't go into this `if` clause.
delete options.collection;
@@ -1098,7 +1214,8 @@
}
Backbone.Relational.store.processOrphanRelations();
-
+ Backbone.Relational.store.listenTo( this, 'relational:unregister', Backbone.Relational.store.unregister );
+
this._queue = new Backbone.BlockingQueue();
this._queue.block();
Backbone.Relational.eventQueue.block();
@@ -1120,42 +1237,53 @@
var dit = this,
args = arguments;
- Backbone.Relational.eventQueue.add( function() {
- if ( !dit._isInitialized ) {
- return;
- }
-
- // Determine if the `change` event is still valid, now that all relations are populated
- var changed = true;
- if ( eventName === 'change' ) {
- changed = dit.hasChanged();
- }
- else {
- var attr = eventName.slice( 7 ),
- rel = dit.getRelation( attr );
+ if ( !Backbone.Relational.eventQueue.isBlocked() ) {
+ // If we're not in a more complicated nested scenario, fire the change event right away
+ Backbone.Model.prototype.trigger.apply( dit, args );
+ }
+ else {
+ Backbone.Relational.eventQueue.add( function() {
+ // Determine if the `change` event is still valid, now that all relations are populated
+ var changed = true;
+ if ( eventName === 'change' ) {
+ // `hasChanged` may have gotten reset by nested calls to `set`.
+ changed = dit.hasChanged() || dit._attributeChangeFired;
+ dit._attributeChangeFired = false;
+ }
+ else {
+ var attr = eventName.slice( 7 ),
+ rel = dit.getRelation( attr );
- if ( rel ) {
- // If `attr` is a relation, `change:attr` get triggered from `Relation.onChange`.
- // These take precedence over `change:attr` events triggered by `Model.set`.
- // The relation set a fourth attribute to `true`. If this attribute is present,
- // continue triggering this event; otherwise, it's from `Model.set` and should be stopped.
- changed = ( args[ 4 ] === true );
+ if ( rel ) {
+ // If `attr` is a relation, `change:attr` get triggered from `Relation.onChange`.
+ // These take precedence over `change:attr` events triggered by `Model.set`.
+ // The relation sets a fourth attribute to `true`. If this attribute is present,
+ // continue triggering this event; otherwise, it's from `Model.set` and should be stopped.
+ changed = ( args[ 4 ] === true );
- // If this event was triggered by a relation, set the right value in `this.changed`
- // (a Collection or Model instead of raw data).
- if ( changed ) {
- dit.changed[ attr ] = args[ 2 ];
+ // If this event was triggered by a relation, set the right value in `this.changed`
+ // (a Collection or Model instead of raw data).
+ if ( changed ) {
+ dit.changed[ attr ] = args[ 2 ];
+ }
+ // Otherwise, this event is from `Model.set`. If the relation doesn't report a change,
+ // remove attr from `dit.changed` so `hasChanged` doesn't take it into account.
+ else if ( !rel.changed ) {
+ delete dit.changed[ attr ];
+ }
}
- // Otherwise, this event is from `Model.set`. If the relation doesn't report a change,
- // remove attr from `dit.changed` so `hasChanged` doesn't take it into account.
- else if ( !rel.changed ) {
- delete dit.changed[ attr ];
+ else if ( changed ) {
+ dit._attributeChangeFired = true;
}
}
- }
- changed && Backbone.Model.prototype.trigger.apply( dit, args );
- });
+ changed && Backbone.Model.prototype.trigger.apply( dit, args );
+ });
+ }
+ }
+ else if ( eventName === 'destroy' ) {
+ Backbone.Model.prototype.trigger.apply( this, arguments );
+ Backbone.Relational.store.unregister( this );
}
else {
Backbone.Model.prototype.trigger.apply( this, arguments );
@@ -1183,15 +1311,28 @@
/**
* When new values are set, notify this model's relations (also if options.silent is set).
- * (Relation.setRelated locks this model before calling 'set' on it to prevent loops)
+ * (called from `set`; Relation.setRelated locks this model before calling 'set' on it to prevent loops)
+ * @param {Object} [changedAttrs]
+ * @param {Object} [options]
*/
- updateRelations: function( options ) {
+ updateRelations: function( changedAttrs, options ) {
if ( this._isInitialized && !this.isLocked() ) {
_.each( this._relations, function( rel ) {
- // Update from data in `rel.keySource` if set, or `rel.key` otherwise
- var val = this.attributes[ rel.keySource ] || this.attributes[ rel.key ];
- if ( rel.related !== val ) {
- this.trigger( 'relational:change:' + rel.key, this, val, options || {} );
+ if ( !changedAttrs || ( rel.keySource in changedAttrs || rel.key in changedAttrs ) ) {
+ // Fetch data in `rel.keySource` if data got set in there, or `rel.key` otherwise
+ var value = this.attributes[ rel.keySource ] || this.attributes[ rel.key ],
+ attr = changedAttrs && ( changedAttrs[ rel.keySource ] || changedAttrs[ rel.key ] );
+
+ // Update a relation if its value differs from this model's attributes, or it's been explicitly nullified.
+ // Which can also happen before the originally intended related model has been found (`val` is null).
+ if ( rel.related !== value || ( value === null && attr === null ) ) {
+ this.trigger( 'relational:change:' + rel.key, this, value, options || {} );
+ }
+ }
+
+ // Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'.
+ if ( rel.keySource !== rel.key ) {
+ delete this.attributes[ rel.keySource ];
}
}, this );
}
@@ -1215,11 +1356,11 @@
/**
* Get a specific relation.
- * @param key {string} The relation key to look for.
- * @return {Backbone.Relation} An instance of 'Backbone.Relation', if a relation was found for 'key', or null.
+ * @param {string} attr The relation key to look for.
+ * @return {Backbone.Relation} An instance of 'Backbone.Relation', if a relation was found for 'attr', or null.
*/
- getRelation: function( key ) {
- return this._relations[ key ];
+ getRelation: function( attr ) {
+ return this._relations[ attr ];
},
/**
@@ -1230,82 +1371,119 @@
return _.values( this._relations );
},
+
/**
- * Retrieve related objects.
- * @param key {string} The relation key to fetch models for.
- * @param [options] {Object} Options for 'Backbone.Model.fetch' and 'Backbone.sync'.
- * @param [refresh=false] {boolean} Fetch existing models from the server as well (in order to update them).
- * @return {jQuery.when[]} An array of request objects
+ * Get a list of ids that will be fetched on a call to `getAsync`.
+ * @param {string|Backbone.Relation} attr The relation key to fetch models for.
+ * @param [refresh=false] Add ids for models that are already in the relation, refreshing them?
+ * @return {Array} An array of ids that need to be fetched.
*/
- fetchRelated: function( key, options, refresh ) {
- // Set default `options` for fetch
- options = _.extend( { update: true, remove: false }, options );
-
- var setUrl,
- requests = [],
- rel = this.getRelation( key ),
- idsToFetch = rel && ( rel.keyIds || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) );
+ getIdsToFetch: function( attr, refresh ) {
+ var rel = attr instanceof Backbone.Relation ? attr : this.getRelation( attr ),
+ ids = rel ? ( rel.keyIds && rel.keyIds.slice( 0 ) ) || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) : [];
// On `refresh`, add the ids for current models in the relation to `idsToFetch`
if ( refresh ) {
- var models = rel.related instanceof Backbone.Collection ? rel.related.models : [ rel.related ];
+ var models = rel.related && ( rel.related.models || [ rel.related ] );
_.each( models, function( model ) {
if ( model.id || model.id === 0 ) {
- idsToFetch.push( model.id );
+ ids.push( model.id );
}
});
}
+ return ids;
+ },
+
+ /**
+ * Get related objects. Returns a single promise, which can either resolve immediately (if the related model[s])
+ * are already present locally, or after fetching the contents of the requested attribute.
+ * @param {string} attr The relation key to fetch models for.
+ * @param {Object} [options] Options for 'Backbone.Model.fetch' and 'Backbone.sync'.
+ * @param {Boolean} [options.refresh=false] Fetch existing models from the server as well (in order to update them).
+ * @return {jQuery.Deferred} A jQuery promise object. When resolved, its `done` callback will be called with
+ * contents of `attr`.
+ */
+ getAsync: function( attr, options ) {
+ // Set default `options` for fetch
+ options = _.extend( { add: true, remove: false, refresh: false }, options );
+
+ var dit = this,
+ requests = [],
+ rel = this.getRelation( attr ),
+ idsToFetch = rel && this.getIdsToFetch( rel, options.refresh ),
+ coll = rel.related instanceof Backbone.Collection ? rel.related : rel.relatedCollection;
+
if ( idsToFetch && idsToFetch.length ) {
- // Find (or create) a model for each one that is to be fetched
- var created = [],
- models = _.map( idsToFetch, function( id ) {
- var model = Backbone.Relational.store.find( rel.relatedModel, id );
-
- if ( !model ) {
- var attrs = {};
- attrs[ rel.relatedModel.prototype.idAttribute ] = id;
- model = rel.relatedModel.findOrCreate( attrs, options );
- created.push( model );
+ var models = [],
+ createdModels = [],
+ setUrl,
+ createModels = function() {
+ // Find (or create) a model for each one that is to be fetched
+ models = _.map( idsToFetch, function( id ) {
+ var model = rel.relatedModel.findModel( id );
+
+ if ( !model ) {
+ var attrs = {};
+ attrs[ rel.relatedModel.prototype.idAttribute ] = id;
+ model = rel.relatedModel.findOrCreate( attrs, options );
+ createdModels.push( model );
+ }
+
+ return model;
+ }, this );
+ };
+
+ // Try if the 'collection' can provide a url to fetch a set of models in one request.
+ // This assumes that when 'Backbone.Collection.url' is a function, it can handle building of set urls.
+ // To make sure it can, test if the url we got by supplying a list of models to fetch is different from
+ // the one supplied for the default fetch action (without args to 'url').
+ if ( coll instanceof Backbone.Collection && _.isFunction( coll.url ) ) {
+ var defaultUrl = coll.url();
+ setUrl = coll.url( idsToFetch );
+
+ if ( setUrl === defaultUrl ) {
+ createModels();
+ setUrl = coll.url( models );
+
+ if ( setUrl === defaultUrl ) {
+ setUrl = null;
}
-
- return model;
- }, this );
-
- // Try if the 'collection' can provide a url to fetch a set of models in one request.
- if ( rel.related instanceof Backbone.Collection && _.isFunction( rel.related.url ) ) {
- setUrl = rel.related.url( models );
+ }
}
- // An assumption is that when 'Backbone.Collection.url' is a function, it can handle building of set urls.
- // To make sure it can, test if the url we got by supplying a list of models to fetch is different from
- // the one supplied for the default fetch action (without args to 'url').
- if ( setUrl && setUrl !== rel.related.url() ) {
+ if ( setUrl ) {
+ // Do a single request to fetch all models
var opts = _.defaults(
{
error: function() {
- var args = arguments;
- _.each( created, function( model ) {
+ _.each( createdModels, function( model ) {
model.trigger( 'destroy', model, model.collection, options );
- options.error && options.error.apply( model, args );
});
+
+ options.error && options.error.apply( models, arguments );
},
url: setUrl
},
options
);
- requests = [ rel.related.fetch( opts ) ];
+ requests = [ coll.fetch( opts ) ];
}
else {
+ // Make a request per model to fetch
+ if ( !models.length ) {
+ createModels();
+ }
+
requests = _.map( models, function( model ) {
var opts = _.defaults(
{
error: function() {
- if ( _.contains( created, model ) ) {
+ if ( _.contains( createdModels, model ) ) {
model.trigger( 'destroy', model, model.collection, options );
- options.error && options.error.apply( model, arguments );
}
+ options.error && options.error.apply( models, arguments );
}
},
options
@@ -1314,40 +1492,25 @@
}, this );
}
}
-
- return requests;
+
+ return this.deferArray(requests).then(
+ function() {
+ return Backbone.Model.prototype.get.call( dit, attr );
+ }
+ );
},
-
- get: function( attr ) {
- var originalResult = Backbone.Model.prototype.get.call( this, attr );
-
- // Use `originalResult` get if dotNotation not enabled or not required because no dot is in `attr`
- if ( !this.dotNotation || attr.indexOf( '.' ) === -1 ) {
- return originalResult;
- }
-
- // Go through all splits and return the final result
- var splits = attr.split( '.' );
- var result = _.reduce(splits, function( model, split ) {
- if ( !( model instanceof Backbone.Model ) ) {
- throw new Error( 'Attribute must be an instanceof Backbone.Model. Is: ' + model + ', currentSplit: ' + split );
- }
-
- return Backbone.Model.prototype.get.call( model, split );
- }, this );
-
- if ( originalResult !== undefined && result !== undefined ) {
- throw new Error( "Ambiguous result for '" + attr + "'. direct result: " + originalResult + ", dotNotation: " + result );
- }
-
- return originalResult || result;
+
+ deferArray: function(deferArray) {
+ return Backbone.$.when.apply(null, deferArray);
},
set: function( key, value, options ) {
Backbone.Relational.eventQueue.block();
// Duplicate backbone's behavior to allow separate key/value parameters, instead of a single 'attributes' object
- var attributes;
+ var attributes,
+ result;
+
if ( _.isObject( key ) || key == null ) {
attributes = key;
options = value;
@@ -1364,12 +1527,17 @@
// Check if we're not setting a duplicate id before actually calling `set`.
Backbone.Relational.store.checkId( this, newId );
- var result = Backbone.Model.prototype.set.apply( this, arguments );
+ result = Backbone.Model.prototype.set.apply( this, arguments );
// Ideal place to set up relations, if this is the first time we're here for this model
if ( !this._isInitialized && !this.isLocked() ) {
this.constructor.initializeModelHierarchy();
- Backbone.Relational.store.register( this );
+
+ // Only register models that have an id. A model will be registered when/if it gets an id later on.
+ if ( newId || newId === 0 ) {
+ Backbone.Relational.store.register( this );
+ }
+
this.initializeRelations( options );
}
// The store should know about an `id` update asap
@@ -1378,37 +1546,13 @@
}
if ( attributes ) {
- this.updateRelations( options );
+ this.updateRelations( attributes, options );
}
}
finally {
// Try to run the global queue holding external events
Backbone.Relational.eventQueue.unblock();
}
-
- return result;
- },
-
- unset: function( attribute, options ) {
- Backbone.Relational.eventQueue.block();
-
- var result = Backbone.Model.prototype.unset.apply( this, arguments );
- this.updateRelations( options );
-
- // Try to run the global queue holding external events
- Backbone.Relational.eventQueue.unblock();
-
- return result;
- },
-
- clear: function( options ) {
- Backbone.Relational.eventQueue.block();
-
- var result = Backbone.Model.prototype.clear.apply( this, arguments );
- this.updateRelations( options );
-
- // Try to run the global queue holding external events
- Backbone.Relational.eventQueue.unblock();
return result;
},
@@ -1465,8 +1609,12 @@
if ( rel instanceof Backbone.HasMany ) {
value = value.concat( rel.keyIds );
}
- else if ( rel instanceof Backbone.HasOne ) {
+ else if ( rel instanceof Backbone.HasOne ) {
value = value || rel.keyId;
+
+ if ( !value && !_.isObject( rel.keyContents ) ) {
+ value = rel.keyContents || null;
+ }
}
}
}
@@ -1492,6 +1640,13 @@
delete json[ rel.key ];
}
+ // In case of `wait: true`, Backbone will simply push whatever's passed into `save` into attributes.
+ // We'll want to get this information into the JSON, even if it doesn't conform to our normal
+ // expectations of what's contained in it (no model/collection for a relation, etc).
+ if ( value === null && options && options.wait ) {
+ value = related;
+ }
+
if ( includeInJSON ) {
json[ rel.keyDestination ] = value;
}
@@ -1500,7 +1655,7 @@
delete json[ rel.key ];
}
});
-
+
this.release();
return json;
}
@@ -1512,8 +1667,8 @@
* @returns {Backbone.RelationalModel.constructor}
*/
setup: function( superModel ) {
- // We don't want to share a relations array with a parent, as this will cause problems with
- // reverse relations.
+ // We don't want to share a relations array with a parent, as this will cause problems with reverse
+ // relations. Since `relations` may also be a property or function, only use slice if we have an array.
this.prototype.relations = ( this.prototype.relations || [] ).slice( 0 );
this._subModels = {};
@@ -1533,7 +1688,7 @@
if ( !rel.model ) {
rel.model = this;
}
-
+
if ( rel.reverseRelation && rel.model === this ) {
var preInitialize = true;
if ( _.isString( rel.relatedModel ) ) {
@@ -1558,7 +1713,7 @@
}
}
}, this );
-
+
return this;
},
@@ -1569,39 +1724,82 @@
* @return {Backbone.Model}
*/
build: function( attributes, options ) {
- var model = this;
-
// 'build' is a possible entrypoint; it's possible no model hierarchy has been determined yet.
this.initializeModelHierarchy();
// Determine what type of (sub)model should be built if applicable.
- // Lookup the proper subModelType in 'this._subModels'.
- if ( this._subModels && this.prototype.subModelTypeAttribute in attributes ) {
- var subModelTypeAttribute = attributes[ this.prototype.subModelTypeAttribute ];
- var subModelType = this._subModels[ subModelTypeAttribute ];
+ var model = this._findSubModelType( this, attributes ) || this;
+
+ return new model( attributes, options );
+ },
+
+ /**
+ * Determines what type of (sub)model should be built if applicable.
+ * Looks up the proper subModelType in 'this._subModels', recursing into
+ * types until a match is found. Returns the applicable 'Backbone.Model'
+ * or null if no match is found.
+ * @param {Backbone.Model} type
+ * @param {Object} attributes
+ * @return {Backbone.Model}
+ */
+ _findSubModelType: function( type, attributes ) {
+ if ( type._subModels && type.prototype.subModelTypeAttribute in attributes ) {
+ var subModelTypeAttribute = attributes[ type.prototype.subModelTypeAttribute ];
+ var subModelType = type._subModels[ subModelTypeAttribute ];
if ( subModelType ) {
- model = subModelType;
+ return subModelType;
+ }
+ else {
+ // Recurse into subModelTypes to find a match
+ for ( subModelTypeAttribute in type._subModels ) {
+ subModelType = this._findSubModelType( type._subModels[ subModelTypeAttribute ], attributes );
+ if ( subModelType ) {
+ return subModelType;
+ }
+ }
}
}
-
- return new model( attributes, options );
+
+ return null;
},
/**
*
*/
initializeModelHierarchy: function() {
- // If we're here for the first time, try to determine if this modelType has a 'superModel'.
- if ( _.isUndefined( this._superModel ) || _.isNull( this._superModel ) ) {
- Backbone.Relational.store.setupSuperModel( this );
+ // Inherit any relations that have been defined in the parent model.
+ this.inheritRelations();
+
+ // If we came here through 'build' for a model that has 'subModelTypes' then try to initialize the ones that
+ // haven't been resolved yet.
+ if ( this.prototype.subModelTypes ) {
+ var resolvedSubModels = _.keys( this._subModels );
+ var unresolvedSubModels = _.omit( this.prototype.subModelTypes, resolvedSubModels );
+ _.each( unresolvedSubModels, function( subModelTypeName ) {
+ var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName );
+ subModelType && subModelType.initializeModelHierarchy();
+ });
+ }
+ },
- // If a superModel has been found, copy relations from the _superModel if they haven't been
- // inherited automatically (due to a redefinition of 'relations').
- // Otherwise, make sure we don't get here again for this type by making '_superModel' false so we fail
- // the isUndefined/isNull check next time.
- if ( this._superModel && this._superModel.prototype.relations ) {
- // Find relations that exist on the `_superModel`, but not yet on this model.
- var inheritedRelations = _.select( this._superModel.prototype.relations || [], function( superRel ) {
+ inheritRelations: function() {
+ // Bail out if we've been here before.
+ if ( !_.isUndefined( this._superModel ) && !_.isNull( this._superModel ) ) {
+ return;
+ }
+ // Try to initialize the _superModel.
+ Backbone.Relational.store.setupSuperModel( this );
+
+ // If a superModel has been found, copy relations from the _superModel if they haven't been inherited automatically
+ // (due to a redefinition of 'relations').
+ if ( this._superModel ) {
+ // The _superModel needs a chance to initialize its own inherited relations before we attempt to inherit relations
+ // from the _superModel. You don't want to call 'initializeModelHierarchy' because that could cause sub-models of
+ // this class to inherit their relations before this class has had chance to inherit it's relations.
+ this._superModel.inheritRelations();
+ if ( this._superModel.prototype.relations ) {
+ // Find relations that exist on the '_superModel', but not yet on this model.
+ var inheritedRelations = _.filter( this._superModel.prototype.relations || [], function( superRel ) {
return !_.any( this.prototype.relations || [], function( rel ) {
return superRel.relatedModel === rel.relatedModel && superRel.key === rel.key;
}, this );
@@ -1609,25 +1807,19 @@
this.prototype.relations = inheritedRelations.concat( this.prototype.relations );
}
- else {
- this._superModel = false;
- }
}
-
- // If we came here through 'build' for a model that has 'subModelTypes', and not all of them have been resolved yet, try to resolve each.
- if ( this.prototype.subModelTypes && _.keys( this.prototype.subModelTypes ).length !== _.keys( this._subModels ).length ) {
- _.each( this.prototype.subModelTypes || [], function( subModelTypeName ) {
- var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName );
- subModelType && subModelType.initializeModelHierarchy();
- });
+ // Otherwise, make sure we don't get here again for this type by making '_superModel' false so we fail the
+ // isUndefined/isNull check next time.
+ else {
+ this._superModel = false;
}
},
/**
* Find an instance of `this` type in 'Backbone.Relational.store'.
- * - If `attributes` is a string or a number, `findOrCreate` will just query the `store` and return a model if found.
- * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.update` is `false`.
- * Otherwise, a new model is created with `attributes` (unless `options.create` is explicitly set to `false`).
+ * A new model is created if no matching model is found, `attributes` is an object, and `options.create` is true.
+ * - If `attributes` is a string or a number, `findOrCreate` will query the `store` and return a model if found.
+ * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.merge` is `false`.
* @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
* @param {Object} [options]
* @param {Boolean} [options.create=true]
@@ -1638,26 +1830,54 @@
findOrCreate: function( attributes, options ) {
options || ( options = {} );
var parsedAttributes = ( _.isObject( attributes ) && options.parse && this.prototype.parse ) ?
- this.prototype.parse( attributes ) : attributes;
+ this.prototype.parse( _.clone( attributes ), options ) : attributes;
- // Try to find an instance of 'this' model type in the store
- var model = Backbone.Relational.store.find( this, parsedAttributes );
+ // If specified, use a custom `find` function to match up existing models to the given attributes.
+ // Otherwise, try to find an instance of 'this' model type in the store
+ var model = this.findModel( parsedAttributes );
// If we found an instance, update it with the data in 'item' (unless 'options.merge' is false).
// If not, create an instance (unless 'options.create' is false).
if ( _.isObject( attributes ) ) {
if ( model && options.merge !== false ) {
- // Make sure `options.collection` doesn't cascade to nested models
+ // Make sure `options.collection` and `options.url` doesn't cascade to nested models
delete options.collection;
+ delete options.url;
model.set( parsedAttributes, options );
}
else if ( !model && options.create !== false ) {
- model = this.build( attributes, options );
+ model = this.build( parsedAttributes, _.defaults( { parse: false }, options ) );
}
}
return model;
+ },
+
+ /**
+ * Find an instance of `this` type in 'Backbone.Relational.store'.
+ * - If `attributes` is a string or a number, `find` will query the `store` and return a model if found.
+ * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.merge` is `false`.
+ * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
+ * @param {Object} [options]
+ * @param {Boolean} [options.merge=true]
+ * @param {Boolean} [options.parse=false]
+ * @return {Backbone.RelationalModel}
+ */
+ find: function( attributes, options ) {
+ options || ( options = {} );
+ options.create = false;
+ return this.findOrCreate( attributes, options );
+ },
+
+ /**
+ * A hook to override the matching when updating (or creating) a model.
+ * The default implementation is to look up the model by id in the store.
+ * @param {Object} attributes
+ * @returns {Backbone.RelationalModel}
+ */
+ findModel: function( attributes ) {
+ return Backbone.Relational.store.find( this, attributes );
}
});
_.extend( Backbone.RelationalModel.prototype, Backbone.Semaphore );
@@ -1669,9 +1889,9 @@
* (which sets the new properties on it if found), or instantiates a new model.
*/
Backbone.Collection.prototype.__prepareModel = Backbone.Collection.prototype._prepareModel;
- Backbone.Collection.prototype._prepareModel = function ( attrs, options ) {
+ Backbone.Collection.prototype._prepareModel = function( attrs, options ) {
var model;
-
+
if ( attrs instanceof Backbone.Model ) {
if ( !attrs.collection ) {
attrs.collection = this;
@@ -1679,22 +1899,22 @@
model = attrs;
}
else {
- options || ( options = {} );
+ options = options ? _.clone( options ) : {};
options.collection = this;
-
+
if ( typeof this.model.findOrCreate !== 'undefined' ) {
model = this.model.findOrCreate( attrs, options );
}
else {
model = new this.model( attrs, options );
}
-
- if ( model && model.isNew() && !model._validate( attrs, options ) ) {
+
+ if ( model && model.validationError ) {
this.trigger( 'invalid', this, attrs, options );
model = false;
}
}
-
+
return model;
};
@@ -1707,84 +1927,80 @@
Backbone.Collection.prototype.set = function( models, options ) {
// Short-circuit if this Collection doesn't hold RelationalModels
if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) {
- return set.apply( this, arguments );
+ return set.call( this, models, options );
}
if ( options && options.parse ) {
models = this.parse( models, options );
}
- if ( !_.isArray( models ) ) {
- models = models ? [ models ] : [];
- }
+ var singular = !_.isArray( models ),
+ newModels = [],
+ toAdd = [],
+ model = null;
- var newModels = [],
- toAdd = [];
+ models = singular ? ( models ? [ models ] : [] ) : _.clone( models );
//console.debug( 'calling add on coll=%o; model=%o, options=%o', this, models, options );
- _.each( models, function( model ) {
+ for ( var i = 0; i < models.length; i++ ) {
+ model = models[i];
if ( !( model instanceof Backbone.Model ) ) {
model = Backbone.Collection.prototype._prepareModel.call( this, model, options );
}
-
if ( model ) {
toAdd.push( model );
-
if ( !( this.get( model ) || this.get( model.cid ) ) ) {
newModels.push( model );
}
// If we arrive in `add` while performing a `set` (after a create, so the model gains an `id`),
// we may get here before `_onModelEvent` has had the chance to update `_byId`.
- else if ( model.id != null ) {
+ else if ( model.id !== null && model.id !== undefined ) {
this._byId[ model.id ] = model;
}
}
- }, this );
+ }
// Add 'models' in a single batch, so the original add will only be called once (and thus 'sort', etc).
// If `parse` was specified, the collection and contained models have been parsed now.
- set.call( this, toAdd, _.defaults( { parse: false }, options ) );
+ toAdd = singular ? ( toAdd.length ? toAdd[ 0 ] : null ) : toAdd;
+ var result = set.call( this, toAdd, _.defaults( { merge: false, parse: false }, options ) );
- _.each( newModels, function( model ) {
+ for ( i = 0; i < newModels.length; i++ ) {
+ model = newModels[i];
// Fire a `relational:add` event for any model in `newModels` that has actually been added to the collection.
if ( this.get( model ) || this.get( model.cid ) ) {
this.trigger( 'relational:add', model, this, options );
}
- }, this );
-
- return this;
+ }
+
+ return result;
};
/**
- * Override 'Backbone.Collection.remove' to trigger 'relational:remove'.
+ * Override 'Backbone.Collection._removeModels' to trigger 'relational:remove'.
*/
- var remove = Backbone.Collection.prototype.__remove = Backbone.Collection.prototype.remove;
- Backbone.Collection.prototype.remove = function( models, options ) {
+ var _removeModels = Backbone.Collection.prototype.___removeModels = Backbone.Collection.prototype._removeModels;
+ Backbone.Collection.prototype._removeModels = function( models, options ) {
// Short-circuit if this Collection doesn't hold RelationalModels
if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) {
- return remove.apply( this, arguments );
+ return _removeModels.call( this, models, options );
}
- models = _.isArray( models ) ? models.slice() : [ models ];
- options || ( options = {} );
-
var toRemove = [];
//console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options );
_.each( models, function( model ) {
- model = this.get( model ) || this.get( model.cid );
+ model = this.get( model ) || ( model && this.get( model.cid ) );
model && toRemove.push( model );
}, this );
- if ( toRemove.length ) {
- remove.call( this, toRemove, options );
+ var result = _removeModels.call( this, toRemove, options );
- _.each( toRemove, function( model ) {
- this.trigger('relational:remove', model, this, options);
- }, this );
- }
-
- return this;
+ _.each( toRemove, function( model ) {
+ this.trigger( 'relational:remove', model, this, options );
+ }, this );
+
+ return result;
};
/**
@@ -1793,13 +2009,13 @@
var reset = Backbone.Collection.prototype.__reset = Backbone.Collection.prototype.reset;
Backbone.Collection.prototype.reset = function( models, options ) {
options = _.extend( { merge: true }, options );
- reset.call( this, models, options );
+ var result = reset.call( this, models, options );
if ( this.model.prototype instanceof Backbone.RelationalModel ) {
this.trigger( 'relational:reset', this, options );
}
- return this;
+ return result;
};
/**
@@ -1807,13 +2023,13 @@
*/
var sort = Backbone.Collection.prototype.__sort = Backbone.Collection.prototype.sort;
Backbone.Collection.prototype.sort = function( options ) {
- sort.call( this, options );
+ var result = sort.call( this, options );
if ( this.model.prototype instanceof Backbone.RelationalModel ) {
this.trigger( 'relational:reset', this, options );
}
- return this;
+ return result;
};
/**
@@ -1827,17 +2043,17 @@
return trigger.apply( this, arguments );
}
- if ( eventName === 'add' || eventName === 'remove' || eventName === 'reset' ) {
+ if ( eventName === 'add' || eventName === 'remove' || eventName === 'reset' || eventName === 'sort' ) {
var dit = this,
args = arguments;
-
+
if ( _.isObject( args[ 3 ] ) ) {
args = _.toArray( args );
// the fourth argument is the option object.
// we need to clone it, as it could be modified while we wait on the eventQueue to be unblocked
args[ 3 ] = _.clone( args[ 3 ] );
}
-
+
Backbone.Relational.eventQueue.add( function() {
trigger.apply( dit, args );
});
@@ -1845,16 +2061,16 @@
else {
trigger.apply( this, arguments );
}
-
+
return this;
};
// Override .extend() to automatically call .setup()
Backbone.RelationalModel.extend = function( protoProps, classProps ) {
- var child = Backbone.Model.extend.apply( this, arguments );
-
+ var child = Backbone.Model.extend.call( this, protoProps, classProps );
+
child.setup( this );
return child;
};
-})();
+}));
--- a/web/res/js/backbone.js Wed Dec 11 10:17:08 2019 +0100
+++ b/web/res/js/backbone.js Wed Dec 11 11:02:15 2019 +0100
@@ -1,6 +1,6 @@
-// Backbone.js 1.2.3
+// Backbone.js 1.4.0
-// (c) 2010-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
+// (c) 2010-2019 Jeremy Ashkenas and DocumentCloud
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
// http://backbonejs.org
@@ -9,8 +9,8 @@
// Establish the root object, `window` (`self`) in the browser, or `global` on the server.
// We use `self` instead of `window` for `WebWorker` support.
- var root = (typeof self == 'object' && self.self == self && self) ||
- (typeof global == 'object' && global.global == global && global);
+ var root = typeof self == 'object' && self.self === self && self ||
+ typeof global == 'object' && global.global === global && global;
// Set up Backbone appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
@@ -23,15 +23,15 @@
// Next for Node.js or CommonJS. jQuery may not be needed as a module.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore'), $;
- try { $ = require('jquery'); } catch(e) {}
+ try { $ = require('jquery'); } catch (e) {}
factory(root, exports, _, $);
// Finally, as a browser global.
} else {
- root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$));
+ root.Backbone = factory(root, {}, root._, root.jQuery || root.Zepto || root.ender || root.$);
}
-}(function(root, Backbone, _, $) {
+})(function(root, Backbone, _, $) {
// Initial Setup
// -------------
@@ -44,7 +44,7 @@
var slice = Array.prototype.slice;
// Current version of the library. Keep in sync with `package.json`.
- Backbone.VERSION = '1.2.3';
+ Backbone.VERSION = '1.4.0';
// For Backbone's purposes, jQuery, Zepto, Ender, or My Library (kidding) owns
// the `$` variable.
@@ -68,54 +68,6 @@
// form param named `model`.
Backbone.emulateJSON = false;
- // Proxy Backbone class methods to Underscore functions, wrapping the model's
- // `attributes` object or collection's `models` array behind the scenes.
- //
- // collection.filter(function(model) { return model.get('age') > 10 });
- // collection.each(this.addView);
- //
- // `Function#apply` can be slow so we use the method's arg count, if we know it.
- var addMethod = function(length, method, attribute) {
- switch (length) {
- case 1: return function() {
- return _[method](this[attribute]);
- };
- case 2: return function(value) {
- return _[method](this[attribute], value);
- };
- case 3: return function(iteratee, context) {
- return _[method](this[attribute], cb(iteratee, this), context);
- };
- case 4: return function(iteratee, defaultVal, context) {
- return _[method](this[attribute], cb(iteratee, this), defaultVal, context);
- };
- default: return function() {
- var args = slice.call(arguments);
- args.unshift(this[attribute]);
- return _[method].apply(_, args);
- };
- }
- };
- var addUnderscoreMethods = function(Class, methods, attribute) {
- _.each(methods, function(length, method) {
- if (_[method]) Class.prototype[method] = addMethod(length, method, attribute);
- });
- };
-
- // Support `collection.sortBy('attr')` and `collection.findWhere({id: 1})`.
- var cb = function(iteratee, instance) {
- if (_.isFunction(iteratee)) return iteratee;
- if (_.isObject(iteratee) && !instance._isModel(iteratee)) return modelMatcher(iteratee);
- if (_.isString(iteratee)) return function(model) { return model.get(iteratee); };
- return iteratee;
- };
- var modelMatcher = function(attrs) {
- var matcher = _.matches(attrs);
- return function(model) {
- return matcher(model.attributes);
- };
- };
-
// Backbone.Events
// ---------------
@@ -134,6 +86,9 @@
// Regular expression used to split event strings.
var eventSplitter = /\s+/;
+ // A private global variable to share between listeners and listenees.
+ var _listening;
+
// Iterates over the standard `event, callback` (as well as the fancy multiple
// space-separated events `"change blur", callback` and jQuery-style event
// maps `{event: callback}`).
@@ -146,7 +101,7 @@
events = eventsApi(iteratee, events, names[i], name[names[i]], opts);
}
} else if (name && eventSplitter.test(name)) {
- // Handle space separated event names by delegating them individually.
+ // Handle space-separated event names by delegating them individually.
for (names = name.split(eventSplitter); i < names.length; i++) {
events = iteratee(events, names[i], callback, opts);
}
@@ -160,43 +115,47 @@
// Bind an event to a `callback` function. Passing `"all"` will bind
// the callback to all events fired.
Events.on = function(name, callback, context) {
- return internalOn(this, name, callback, context);
- };
-
- // Guard the `listening` argument from the public API.
- var internalOn = function(obj, name, callback, context, listening) {
- obj._events = eventsApi(onApi, obj._events || {}, name, callback, {
- context: context,
- ctx: obj,
- listening: listening
+ this._events = eventsApi(onApi, this._events || {}, name, callback, {
+ context: context,
+ ctx: this,
+ listening: _listening
});
- if (listening) {
- var listeners = obj._listeners || (obj._listeners = {});
- listeners[listening.id] = listening;
+ if (_listening) {
+ var listeners = this._listeners || (this._listeners = {});
+ listeners[_listening.id] = _listening;
+ // Allow the listening to use a counter, instead of tracking
+ // callbacks for library interop
+ _listening.interop = false;
}
- return obj;
+ return this;
};
// Inversion-of-control versions of `on`. Tell *this* object to listen to
// an event in another object... keeping track of what it's listening to
// for easier unbinding later.
- Events.listenTo = function(obj, name, callback) {
+ Events.listenTo = function(obj, name, callback) {
if (!obj) return this;
var id = obj._listenId || (obj._listenId = _.uniqueId('l'));
var listeningTo = this._listeningTo || (this._listeningTo = {});
- var listening = listeningTo[id];
+ var listening = _listening = listeningTo[id];
// This object is not listening to any other events on `obj` yet.
// Setup the necessary references to track the listening callbacks.
if (!listening) {
- var thisId = this._listenId || (this._listenId = _.uniqueId('l'));
- listening = listeningTo[id] = {obj: obj, objId: id, id: thisId, listeningTo: listeningTo, count: 0};
+ this._listenId || (this._listenId = _.uniqueId('l'));
+ listening = _listening = listeningTo[id] = new Listening(this, obj);
}
- // Bind callbacks on obj, and keep track of them on listening.
- internalOn(obj, name, callback, this, listening);
+ // Bind callbacks on obj.
+ var error = tryCatchOn(obj, name, callback, this);
+ _listening = void 0;
+
+ if (error) throw error;
+ // If the target obj is not Backbone.Events, track events manually.
+ if (listening.interop) listening.on(name, callback);
+
return this;
};
@@ -207,32 +166,42 @@
var context = options.context, ctx = options.ctx, listening = options.listening;
if (listening) listening.count++;
- handlers.push({ callback: callback, context: context, ctx: context || ctx, listening: listening });
+ handlers.push({callback: callback, context: context, ctx: context || ctx, listening: listening});
}
return events;
};
+ // An try-catch guarded #on function, to prevent poisoning the global
+ // `_listening` variable.
+ var tryCatchOn = function(obj, name, callback, context) {
+ try {
+ obj.on(name, callback, context);
+ } catch (e) {
+ return e;
+ }
+ };
+
// Remove one or many callbacks. If `context` is null, removes all
// callbacks with that function. If `callback` is null, removes all
// callbacks for the event. If `name` is null, removes all bound
// callbacks for all events.
- Events.off = function(name, callback, context) {
+ Events.off = function(name, callback, context) {
if (!this._events) return this;
this._events = eventsApi(offApi, this._events, name, callback, {
- context: context,
- listeners: this._listeners
+ context: context,
+ listeners: this._listeners
});
+
return this;
};
// Tell this object to stop listening to either specific events ... or
// to every object it's currently listening to.
- Events.stopListening = function(obj, name, callback) {
+ Events.stopListening = function(obj, name, callback) {
var listeningTo = this._listeningTo;
if (!listeningTo) return this;
var ids = obj ? [obj._listenId] : _.keys(listeningTo);
-
for (var i = 0; i < ids.length; i++) {
var listening = listeningTo[ids[i]];
@@ -241,6 +210,7 @@
if (!listening) break;
listening.obj.off(name, callback, this);
+ if (listening.interop) listening.off(name, callback);
}
if (_.isEmpty(listeningTo)) this._listeningTo = void 0;
@@ -251,21 +221,18 @@
var offApi = function(events, name, callback, options) {
if (!events) return;
- var i = 0, listening;
var context = options.context, listeners = options.listeners;
+ var i = 0, names;
- // Delete all events listeners and "drop" events.
- if (!name && !callback && !context) {
- var ids = _.keys(listeners);
- for (; i < ids.length; i++) {
- listening = listeners[ids[i]];
- delete listeners[listening.id];
- delete listening.listeningTo[listening.objId];
+ // Delete all event listeners and "drop" events.
+ if (!name && !context && !callback) {
+ for (names = _.keys(listeners); i < names.length; i++) {
+ listeners[names[i]].cleanup();
}
return;
}
- var names = name ? [name] : _.keys(events);
+ names = name ? [name] : _.keys(events);
for (; i < names.length; i++) {
name = names[i];
var handlers = events[name];
@@ -273,7 +240,7 @@
// Bail out if there are no events stored.
if (!handlers) break;
- // Replace events if there are any remaining. Otherwise, clean up.
+ // Find any remaining events.
var remaining = [];
for (var j = 0; j < handlers.length; j++) {
var handler = handlers[j];
@@ -284,38 +251,37 @@
) {
remaining.push(handler);
} else {
- listening = handler.listening;
- if (listening && --listening.count === 0) {
- delete listeners[listening.id];
- delete listening.listeningTo[listening.objId];
- }
+ var listening = handler.listening;
+ if (listening) listening.off(name, callback);
}
}
- // Update tail event if the list has any events. Otherwise, clean up.
+ // Replace events if there are any remaining. Otherwise, clean up.
if (remaining.length) {
events[name] = remaining;
} else {
delete events[name];
}
}
- if (_.size(events)) return events;
+
+ return events;
};
// Bind an event to only be triggered a single time. After the first time
// the callback is invoked, its listener will be removed. If multiple events
// are passed in using the space-separated syntax, the handler will fire
// once for each event, not once for a combination of all events.
- Events.once = function(name, callback, context) {
+ Events.once = function(name, callback, context) {
// Map the event into a `{event: once}` object.
- var events = eventsApi(onceMap, {}, name, callback, _.bind(this.off, this));
- return this.on(events, void 0, context);
+ var events = eventsApi(onceMap, {}, name, callback, this.off.bind(this));
+ if (typeof name === 'string' && context == null) callback = void 0;
+ return this.on(events, callback, context);
};
// Inversion-of-control versions of `once`.
- Events.listenToOnce = function(obj, name, callback) {
+ Events.listenToOnce = function(obj, name, callback) {
// Map the event into a `{event: once}` object.
- var events = eventsApi(onceMap, {}, name, callback, _.bind(this.stopListening, this, obj));
+ var events = eventsApi(onceMap, {}, name, callback, this.stopListening.bind(this, obj));
return this.listenTo(obj, events);
};
@@ -336,7 +302,7 @@
// passed the same arguments as `trigger` is, apart from the event name
// (unless you're listening on `"all"`, which will cause your callback to
// receive the true name of the event as the first argument).
- Events.trigger = function(name) {
+ Events.trigger = function(name) {
if (!this._events) return this;
var length = Math.max(0, arguments.length - 1);
@@ -348,7 +314,7 @@
};
// Handles triggering the appropriate event callbacks.
- var triggerApi = function(objEvents, name, cb, args) {
+ var triggerApi = function(objEvents, name, callback, args) {
if (objEvents) {
var events = objEvents[name];
var allEvents = objEvents.all;
@@ -373,6 +339,44 @@
}
};
+ // A listening class that tracks and cleans up memory bindings
+ // when all callbacks have been offed.
+ var Listening = function(listener, obj) {
+ this.id = listener._listenId;
+ this.listener = listener;
+ this.obj = obj;
+ this.interop = true;
+ this.count = 0;
+ this._events = void 0;
+ };
+
+ Listening.prototype.on = Events.on;
+
+ // Offs a callback (or several).
+ // Uses an optimized counter if the listenee uses Backbone.Events.
+ // Otherwise, falls back to manual tracking to support events
+ // library interop.
+ Listening.prototype.off = function(name, callback) {
+ var cleanup;
+ if (this.interop) {
+ this._events = eventsApi(offApi, this._events, name, callback, {
+ context: void 0,
+ listeners: void 0
+ });
+ cleanup = !this._events;
+ } else {
+ this.count--;
+ cleanup = this.count === 0;
+ }
+ if (cleanup) this.cleanup();
+ };
+
+ // Cleans up memory bindings between the listener and the listenee.
+ Listening.prototype.cleanup = function() {
+ delete this.listener._listeningTo[this.obj._listenId];
+ if (!this.interop) delete this.obj._listeners[this.id];
+ };
+
// Aliases for backwards compatibility.
Events.bind = Events.on;
Events.unbind = Events.off;
@@ -394,11 +398,13 @@
var Model = Backbone.Model = function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
+ this.preinitialize.apply(this, arguments);
this.cid = _.uniqueId(this.cidPrefix);
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
- attrs = _.defaults({}, attrs, _.result(this, 'defaults'));
+ var defaults = _.result(this, 'defaults');
+ attrs = _.defaults(_.extend({}, defaults, attrs), defaults);
this.set(attrs, options);
this.changed = {};
this.initialize.apply(this, arguments);
@@ -421,6 +427,10 @@
// You may want to override this if you're experiencing name clashes with model ids.
cidPrefix: 'c',
+ // preinitialize is an empty function by default. You can override it with a function
+ // or object. preinitialize will run before any instantiation logic is run in the Model.
+ preinitialize: function(){},
+
// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
@@ -506,7 +516,7 @@
}
// Update the `id`.
- this.id = this.get(this.idAttribute);
+ if (this.idAttribute in attrs) this.id = this.get(this.idAttribute);
// Trigger all relevant attribute changes.
if (!silent) {
@@ -561,12 +571,14 @@
if (!diff) return this.hasChanged() ? _.clone(this.changed) : false;
var old = this._changing ? this._previousAttributes : this.attributes;
var changed = {};
+ var hasChanged;
for (var attr in diff) {
var val = diff[attr];
if (_.isEqual(old[attr], val)) continue;
changed[attr] = val;
+ hasChanged = true;
}
- return _.size(changed) ? changed : false;
+ return hasChanged ? changed : false;
},
// Get the previous value of an attribute, recorded at the time the last
@@ -619,8 +631,8 @@
// the model will be valid when the attributes, if any, are set.
if (attrs && !wait) {
if (!this.set(attrs, options)) return false;
- } else {
- if (!this._validate(attrs, options)) return false;
+ } else if (!this._validate(attrs, options)) {
+ return false;
}
// After a successful server-side save, the client is (optionally)
@@ -642,7 +654,7 @@
// Set temporary attributes if `{wait: true}` to properly find new ids.
if (attrs && wait) this.attributes = _.extend({}, attributes, attrs);
- var method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update');
+ var method = this.isNew() ? 'create' : options.patch ? 'patch' : 'update';
if (method === 'patch' && !options.attrs) options.attrs = attrs;
var xhr = this.sync(method, this, options);
@@ -714,7 +726,7 @@
// Check if the model is currently in a valid state.
isValid: function(options) {
- return this._validate({}, _.defaults({validate: true}, options));
+ return this._validate({}, _.extend({}, options, {validate: true}));
},
// Run validation against the next complete set of model attributes,
@@ -730,14 +742,6 @@
});
- // Underscore methods that we want to implement on the Model, mapped to the
- // number of arguments they take.
- var modelMethods = { keys: 1, values: 1, pairs: 1, invert: 1, pick: 0,
- omit: 0, chain: 1, isEmpty: 1 };
-
- // Mix in each Underscore method as a proxy to `Model#attributes`.
- addUnderscoreMethods(Model, modelMethods, 'attributes');
-
// Backbone.Collection
// -------------------
@@ -753,6 +757,7 @@
// its models in sort order, as they're added and removed.
var Collection = Backbone.Collection = function(models, options) {
options || (options = {});
+ this.preinitialize.apply(this, arguments);
if (options.model) this.model = options.model;
if (options.comparator !== void 0) this.comparator = options.comparator;
this._reset();
@@ -769,7 +774,8 @@
at = Math.min(Math.max(at, 0), array.length);
var tail = Array(array.length - at);
var length = insert.length;
- for (var i = 0; i < tail.length; i++) tail[i] = array[i + at];
+ var i;
+ for (i = 0; i < tail.length; i++) tail[i] = array[i + at];
for (i = 0; i < length; i++) array[i + at] = insert[i];
for (i = 0; i < tail.length; i++) array[i + length + at] = tail[i];
};
@@ -781,6 +787,11 @@
// This should be overridden in most cases.
model: Model,
+
+ // preinitialize is an empty function by default. You can override it with a function
+ // or object. preinitialize will run before any instantiation logic is run in the Collection.
+ preinitialize: function(){},
+
// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
@@ -807,9 +818,12 @@
remove: function(models, options) {
options = _.extend({}, options);
var singular = !_.isArray(models);
- models = singular ? [models] : _.clone(models);
+ models = singular ? [models] : models.slice();
var removed = this._removeModels(models, options);
- if (!options.silent && removed) this.trigger('update', this, options);
+ if (!options.silent && removed.length) {
+ options.changes = {added: [], merged: [], removed: removed};
+ this.trigger('update', this, options);
+ }
return singular ? removed[0] : removed;
},
@@ -820,18 +834,22 @@
set: function(models, options) {
if (models == null) return;
- options = _.defaults({}, options, setOptions);
- if (options.parse && !this._isModel(models)) models = this.parse(models, options);
+ options = _.extend({}, setOptions, options);
+ if (options.parse && !this._isModel(models)) {
+ models = this.parse(models, options) || [];
+ }
var singular = !_.isArray(models);
models = singular ? [models] : models.slice();
var at = options.at;
if (at != null) at = +at;
+ if (at > this.length) at = this.length;
if (at < 0) at += this.length + 1;
var set = [];
var toAdd = [];
+ var toMerge = [];
var toRemove = [];
var modelMap = {};
@@ -840,13 +858,13 @@
var remove = options.remove;
var sort = false;
- var sortable = this.comparator && (at == null) && options.sort !== false;
+ var sortable = this.comparator && at == null && options.sort !== false;
var sortAttr = _.isString(this.comparator) ? this.comparator : null;
// Turn bare objects into model references, and prevent invalid models
// from being added.
- var model;
- for (var i = 0; i < models.length; i++) {
+ var model, i;
+ for (i = 0; i < models.length; i++) {
model = models[i];
// If a duplicate is found, prevent it from being added and
@@ -857,6 +875,7 @@
var attrs = this._isModel(model) ? model.attributes : model;
if (options.parse) attrs = existing.parse(attrs, options);
existing.set(attrs, options);
+ toMerge.push(existing);
if (sortable && !sort) sort = existing.hasChanged(sortAttr);
}
if (!modelMap[existing.cid]) {
@@ -890,8 +909,8 @@
var orderChanged = false;
var replace = !sortable && add && remove;
if (set.length && replace) {
- orderChanged = this.length != set.length || _.some(this.models, function(model, index) {
- return model !== set[index];
+ orderChanged = this.length !== set.length || _.some(this.models, function(m, index) {
+ return m !== set[index];
});
this.models.length = 0;
splice(this.models, set, 0);
@@ -905,7 +924,7 @@
// Silently sort the collection if appropriate.
if (sort) this.sort({silent: true});
- // Unless silenced, it's time to fire all appropriate add/sort events.
+ // Unless silenced, it's time to fire all appropriate add/sort/update events.
if (!options.silent) {
for (i = 0; i < toAdd.length; i++) {
if (at != null) options.index = at + i;
@@ -913,7 +932,14 @@
model.trigger('add', model, this, options);
}
if (sort || orderChanged) this.trigger('sort', this, options);
- if (toAdd.length || toRemove.length) this.trigger('update', this, options);
+ if (toAdd.length || toRemove.length || toMerge.length) {
+ options.changes = {
+ added: toAdd,
+ removed: toRemove,
+ merged: toMerge
+ };
+ this.trigger('update', this, options);
+ }
}
// Return the added (or merged) model (or models).
@@ -963,11 +989,18 @@
return slice.apply(this.models, arguments);
},
- // Get a model from the set by id.
+ // Get a model from the set by id, cid, model object with id or cid
+ // properties, or an attributes object that is transformed through modelId.
get: function(obj) {
if (obj == null) return void 0;
- var id = this.modelId(this._isModel(obj) ? obj.attributes : obj);
- return this._byId[obj] || this._byId[id] || this._byId[obj.cid];
+ return this._byId[obj] ||
+ this._byId[this.modelId(this._isModel(obj) ? obj.attributes : obj)] ||
+ obj.cid && this._byId[obj.cid];
+ },
+
+ // Returns `true` if the model is in the collection.
+ has: function(obj) {
+ return this.get(obj) != null;
},
// Get the model at the given index.
@@ -997,7 +1030,7 @@
options || (options = {});
var length = comparator.length;
- if (_.isFunction(comparator)) comparator = _.bind(comparator, this);
+ if (_.isFunction(comparator)) comparator = comparator.bind(this);
// Run sort based on type of `comparator`.
if (length === 1 || _.isString(comparator)) {
@@ -1011,7 +1044,7 @@
// Pluck an attribute from each model in the collection.
pluck: function(attr) {
- return _.invoke(this.models, 'get', attr);
+ return this.map(attr + '');
},
// Fetch the default set of models for this collection, resetting the
@@ -1042,9 +1075,9 @@
if (!wait) this.add(model, options);
var collection = this;
var success = options.success;
- options.success = function(model, resp, callbackOpts) {
- if (wait) collection.add(model, callbackOpts);
- if (success) success.call(callbackOpts.context, model, resp, callbackOpts);
+ options.success = function(m, resp, callbackOpts) {
+ if (wait) collection.add(m, callbackOpts);
+ if (success) success.call(callbackOpts.context, m, resp, callbackOpts);
};
model.save(null, options);
return model;
@@ -1065,10 +1098,25 @@
},
// Define how to uniquely identify models in the collection.
- modelId: function (attrs) {
+ modelId: function(attrs) {
return attrs[this.model.prototype.idAttribute || 'id'];
},
+ // Get an iterator of all models in this collection.
+ values: function() {
+ return new CollectionIterator(this, ITERATOR_VALUES);
+ },
+
+ // Get an iterator of all model IDs in this collection.
+ keys: function() {
+ return new CollectionIterator(this, ITERATOR_KEYS);
+ },
+
+ // Get an iterator of all [ID, model] tuples in this collection.
+ entries: function() {
+ return new CollectionIterator(this, ITERATOR_KEYSVALUES);
+ },
+
// Private method to reset all internal state. Called when the collection
// is first initialized or reset.
_reset: function() {
@@ -1103,6 +1151,12 @@
this.models.splice(index, 1);
this.length--;
+ // Remove references before triggering 'remove' event to prevent an
+ // infinite loop. #3693
+ delete this._byId[model.cid];
+ var id = this.modelId(model.attributes);
+ if (id != null) delete this._byId[id];
+
if (!options.silent) {
options.index = index;
model.trigger('remove', model, this, options);
@@ -1111,12 +1165,12 @@
removed.push(model);
this._removeReference(model, options);
}
- return removed.length ? removed : false;
+ return removed;
},
// Method for checking whether an object should be considered a model for
// the purposes of adding to the collection.
- _isModel: function (model) {
+ _isModel: function(model) {
return model instanceof Model;
},
@@ -1142,14 +1196,16 @@
// events simply proxy through. "add" and "remove" events that originate
// in other collections are ignored.
_onModelEvent: function(event, model, collection, options) {
- if ((event === 'add' || event === 'remove') && collection !== this) return;
- if (event === 'destroy') this.remove(model, options);
- if (event === 'change') {
- var prevId = this.modelId(model.previousAttributes());
- var id = this.modelId(model.attributes);
- if (prevId !== id) {
- if (prevId != null) delete this._byId[prevId];
- if (id != null) this._byId[id] = model;
+ if (model) {
+ if ((event === 'add' || event === 'remove') && collection !== this) return;
+ if (event === 'destroy') this.remove(model, options);
+ if (event === 'change') {
+ var prevId = this.modelId(model.previousAttributes());
+ var id = this.modelId(model.attributes);
+ if (prevId !== id) {
+ if (prevId != null) delete this._byId[prevId];
+ if (id != null) this._byId[id] = model;
+ }
}
}
this.trigger.apply(this, arguments);
@@ -1157,20 +1213,71 @@
});
- // Underscore methods that we want to implement on the Collection.
- // 90% of the core usefulness of Backbone Collections is actually implemented
- // right here:
- var collectionMethods = { forEach: 3, each: 3, map: 3, collect: 3, reduce: 4,
- foldl: 4, inject: 4, reduceRight: 4, foldr: 4, find: 3, detect: 3, filter: 3,
- select: 3, reject: 3, every: 3, all: 3, some: 3, any: 3, include: 3, includes: 3,
- contains: 3, invoke: 0, max: 3, min: 3, toArray: 1, size: 1, first: 3,
- head: 3, take: 3, initial: 3, rest: 3, tail: 3, drop: 3, last: 3,
- without: 0, difference: 0, indexOf: 3, shuffle: 1, lastIndexOf: 3,
- isEmpty: 1, chain: 1, sample: 3, partition: 3, groupBy: 3, countBy: 3,
- sortBy: 3, indexBy: 3};
+ // Defining an @@iterator method implements JavaScript's Iterable protocol.
+ // In modern ES2015 browsers, this value is found at Symbol.iterator.
+ /* global Symbol */
+ var $$iterator = typeof Symbol === 'function' && Symbol.iterator;
+ if ($$iterator) {
+ Collection.prototype[$$iterator] = Collection.prototype.values;
+ }
+
+ // CollectionIterator
+ // ------------------
+
+ // A CollectionIterator implements JavaScript's Iterator protocol, allowing the
+ // use of `for of` loops in modern browsers and interoperation between
+ // Backbone.Collection and other JavaScript functions and third-party libraries
+ // which can operate on Iterables.
+ var CollectionIterator = function(collection, kind) {
+ this._collection = collection;
+ this._kind = kind;
+ this._index = 0;
+ };
+
+ // This "enum" defines the three possible kinds of values which can be emitted
+ // by a CollectionIterator that correspond to the values(), keys() and entries()
+ // methods on Collection, respectively.
+ var ITERATOR_VALUES = 1;
+ var ITERATOR_KEYS = 2;
+ var ITERATOR_KEYSVALUES = 3;
- // Mix in each Underscore method as a proxy to `Collection#models`.
- addUnderscoreMethods(Collection, collectionMethods, 'models');
+ // All Iterators should themselves be Iterable.
+ if ($$iterator) {
+ CollectionIterator.prototype[$$iterator] = function() {
+ return this;
+ };
+ }
+
+ CollectionIterator.prototype.next = function() {
+ if (this._collection) {
+
+ // Only continue iterating if the iterated collection is long enough.
+ if (this._index < this._collection.length) {
+ var model = this._collection.at(this._index);
+ this._index++;
+
+ // Construct a value depending on what kind of values should be iterated.
+ var value;
+ if (this._kind === ITERATOR_VALUES) {
+ value = model;
+ } else {
+ var id = this._collection.modelId(model.attributes);
+ if (this._kind === ITERATOR_KEYS) {
+ value = id;
+ } else { // ITERATOR_KEYSVALUES
+ value = [id, model];
+ }
+ }
+ return {value: value, done: false};
+ }
+
+ // Once exhausted, remove the reference to the collection so future
+ // calls to the next method always return done.
+ this._collection = void 0;
+ }
+
+ return {value: void 0, done: true};
+ };
// Backbone.View
// -------------
@@ -1187,6 +1294,7 @@
// if an existing element is not provided...
var View = Backbone.View = function(options) {
this.cid = _.uniqueId('view');
+ this.preinitialize.apply(this, arguments);
_.extend(this, _.pick(options, viewOptions));
this._ensureElement();
this.initialize.apply(this, arguments);
@@ -1210,6 +1318,10 @@
return this.$el.find(selector);
},
+ // preinitialize is an empty function by default. You can override it with a function
+ // or object. preinitialize will run before any instantiation logic is run in the View
+ preinitialize: function(){},
+
// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
@@ -1277,7 +1389,7 @@
if (!_.isFunction(method)) method = this[method];
if (!method) continue;
var match = key.match(delegateEventSplitter);
- this.delegate(match[1], match[2], _.bind(method, this));
+ this.delegate(match[1], match[2], method.bind(this));
}
return this;
},
@@ -1335,6 +1447,94 @@
});
+ // Proxy Backbone class methods to Underscore functions, wrapping the model's
+ // `attributes` object or collection's `models` array behind the scenes.
+ //
+ // collection.filter(function(model) { return model.get('age') > 10 });
+ // collection.each(this.addView);
+ //
+ // `Function#apply` can be slow so we use the method's arg count, if we know it.
+ var addMethod = function(base, length, method, attribute) {
+ switch (length) {
+ case 1: return function() {
+ return base[method](this[attribute]);
+ };
+ case 2: return function(value) {
+ return base[method](this[attribute], value);
+ };
+ case 3: return function(iteratee, context) {
+ return base[method](this[attribute], cb(iteratee, this), context);
+ };
+ case 4: return function(iteratee, defaultVal, context) {
+ return base[method](this[attribute], cb(iteratee, this), defaultVal, context);
+ };
+ default: return function() {
+ var args = slice.call(arguments);
+ args.unshift(this[attribute]);
+ return base[method].apply(base, args);
+ };
+ }
+ };
+
+ var addUnderscoreMethods = function(Class, base, methods, attribute) {
+ _.each(methods, function(length, method) {
+ if (base[method]) Class.prototype[method] = addMethod(base, length, method, attribute);
+ });
+ };
+
+ // Support `collection.sortBy('attr')` and `collection.findWhere({id: 1})`.
+ var cb = function(iteratee, instance) {
+ if (_.isFunction(iteratee)) return iteratee;
+ if (_.isObject(iteratee) && !instance._isModel(iteratee)) return modelMatcher(iteratee);
+ if (_.isString(iteratee)) return function(model) { return model.get(iteratee); };
+ return iteratee;
+ };
+ var modelMatcher = function(attrs) {
+ var matcher = _.matches(attrs);
+ return function(model) {
+ return matcher(model.attributes);
+ };
+ };
+
+ // Underscore methods that we want to implement on the Collection.
+ // 90% of the core usefulness of Backbone Collections is actually implemented
+ // right here:
+ var collectionMethods = {forEach: 3, each: 3, map: 3, collect: 3, reduce: 0,
+ foldl: 0, inject: 0, reduceRight: 0, foldr: 0, find: 3, detect: 3, filter: 3,
+ select: 3, reject: 3, every: 3, all: 3, some: 3, any: 3, include: 3, includes: 3,
+ contains: 3, invoke: 0, max: 3, min: 3, toArray: 1, size: 1, first: 3,
+ head: 3, take: 3, initial: 3, rest: 3, tail: 3, drop: 3, last: 3,
+ without: 0, difference: 0, indexOf: 3, shuffle: 1, lastIndexOf: 3,
+ isEmpty: 1, chain: 1, sample: 3, partition: 3, groupBy: 3, countBy: 3,
+ sortBy: 3, indexBy: 3, findIndex: 3, findLastIndex: 3};
+
+
+ // Underscore methods that we want to implement on the Model, mapped to the
+ // number of arguments they take.
+ var modelMethods = {keys: 1, values: 1, pairs: 1, invert: 1, pick: 0,
+ omit: 0, chain: 1, isEmpty: 1};
+
+ // Mix in each Underscore method as a proxy to `Collection#models`.
+
+ _.each([
+ [Collection, collectionMethods, 'models'],
+ [Model, modelMethods, 'attributes']
+ ], function(config) {
+ var Base = config[0],
+ methods = config[1],
+ attribute = config[2];
+
+ Base.mixin = function(obj) {
+ var mappings = _.reduce(_.functions(obj), function(memo, name) {
+ memo[name] = 0;
+ return memo;
+ }, {});
+ addUnderscoreMethods(Base, obj, mappings, attribute);
+ };
+
+ addUnderscoreMethods(Base, _, methods, attribute);
+ });
+
// Backbone.sync
// -------------
@@ -1415,11 +1615,11 @@
// Map from CRUD to HTTP for our default `Backbone.sync` implementation.
var methodMap = {
- 'create': 'POST',
- 'update': 'PUT',
- 'patch': 'PATCH',
- 'delete': 'DELETE',
- 'read': 'GET'
+ create: 'POST',
+ update: 'PUT',
+ patch: 'PATCH',
+ delete: 'DELETE',
+ read: 'GET'
};
// Set the default implementation of `Backbone.ajax` to proxy through to `$`.
@@ -1435,6 +1635,7 @@
// matched. Creating a new one sets its `routes` hash, if not set statically.
var Router = Backbone.Router = function(options) {
options || (options = {});
+ this.preinitialize.apply(this, arguments);
if (options.routes) this.routes = options.routes;
this._bindRoutes();
this.initialize.apply(this, arguments);
@@ -1450,6 +1651,10 @@
// Set up all inheritable **Backbone.Router** properties and methods.
_.extend(Router.prototype, Events, {
+ // preinitialize is an empty function by default. You can override it with a function
+ // or object. preinitialize will run before any instantiation logic is run in the Router.
+ preinitialize: function(){},
+
// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
@@ -1507,11 +1712,11 @@
// against the current location hash.
_routeToRegExp: function(route) {
route = route.replace(escapeRegExp, '\\$&')
- .replace(optionalParam, '(?:$1)?')
- .replace(namedParam, function(match, optional) {
- return optional ? match : '([^/?]+)';
- })
- .replace(splatParam, '([^?]*?)');
+ .replace(optionalParam, '(?:$1)?')
+ .replace(namedParam, function(match, optional) {
+ return optional ? match : '([^/?]+)';
+ })
+ .replace(splatParam, '([^?]*?)');
return new RegExp('^' + route + '(?:\\?([\\s\\S]*))?$');
},
@@ -1539,7 +1744,7 @@
// falls back to polling.
var History = Backbone.History = function() {
this.handlers = [];
- this.checkUrl = _.bind(this.checkUrl, this);
+ this.checkUrl = this.checkUrl.bind(this);
// Ensure that `History` can be used outside of the browser.
if (typeof window !== 'undefined') {
@@ -1576,8 +1781,8 @@
// Does the pathname match the root?
matchRoot: function() {
var path = this.decodeFragment(this.location.pathname);
- var root = path.slice(0, this.root.length - 1) + '/';
- return root === this.root;
+ var rootPath = path.slice(0, this.root.length - 1) + '/';
+ return rootPath === this.root;
},
// Unicode characters in `location.pathname` are percent encoded so they're
@@ -1649,8 +1854,8 @@
// If we've started off with a route from a `pushState`-enabled
// browser, but we're currently in a browser that doesn't support it...
if (!this._hasPushState && !this.atRoot()) {
- var root = this.root.slice(0, -1) || '/';
- this.location.replace(root + '#' + this.getPath());
+ var rootPath = this.root.slice(0, -1) || '/';
+ this.location.replace(rootPath + '#' + this.getPath());
// Return immediately as browser will do redirect to new url
return true;
@@ -1679,7 +1884,7 @@
}
// Add a cross-platform `addEventListener` shim for older browsers.
- var addEventListener = window.addEventListener || function (eventName, listener) {
+ var addEventListener = window.addEventListener || function(eventName, listener) {
return attachEvent('on' + eventName, listener);
};
@@ -1700,7 +1905,7 @@
// but possibly useful for unit testing Routers.
stop: function() {
// Add a cross-platform `removeEventListener` shim for older browsers.
- var removeEventListener = window.removeEventListener || function (eventName, listener) {
+ var removeEventListener = window.removeEventListener || function(eventName, listener) {
return detachEvent('on' + eventName, listener);
};
@@ -1774,17 +1979,20 @@
fragment = this.getFragment(fragment || '');
// Don't include a trailing slash on the root.
- var root = this.root;
+ var rootPath = this.root;
if (fragment === '' || fragment.charAt(0) === '?') {
- root = root.slice(0, -1) || '/';
+ rootPath = rootPath.slice(0, -1) || '/';
}
- var url = root + fragment;
+ var url = rootPath + fragment;
- // Strip the hash and decode for matching.
- fragment = this.decodeFragment(fragment.replace(pathStripper, ''));
+ // Strip the fragment of the query and hash for matching.
+ fragment = fragment.replace(pathStripper, '');
- if (this.fragment === fragment) return;
- this.fragment = fragment;
+ // Decode for matching.
+ var decodedFragment = this.decodeFragment(fragment);
+
+ if (this.fragment === decodedFragment) return;
+ this.fragment = decodedFragment;
// If pushState is available, we use it to set the fragment as a real URL.
if (this._usePushState) {
@@ -1794,7 +2002,7 @@
// fragment to store history.
} else if (this._wantsHashChange) {
this._updateHash(this.location, fragment, options.replace);
- if (this.iframe && (fragment !== this.getHash(this.iframe.contentWindow))) {
+ if (this.iframe && fragment !== this.getHash(this.iframe.contentWindow)) {
var iWindow = this.iframe.contentWindow;
// Opening and closing the iframe tricks IE7 and earlier to push a
@@ -1856,14 +2064,9 @@
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
- // `parent` constructor function.
- var Surrogate = function(){ this.constructor = child; };
- Surrogate.prototype = parent.prototype;
- child.prototype = new Surrogate;
-
- // Add prototype properties (instance properties) to the subclass,
- // if supplied.
- if (protoProps) _.extend(child.prototype, protoProps);
+ // `parent`'s constructor function and add the prototype properties.
+ child.prototype = _.create(parent.prototype, protoProps);
+ child.prototype.constructor = child;
// Set a convenience property in case the parent's prototype is needed
// later.
@@ -1890,5 +2093,4 @@
};
return Backbone;
-
-}));
+});