-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackbone.sharedcollection.min.js
1 lines (1 loc) · 6.47 KB
/
backbone.sharedcollection.min.js
1
((function(){var a,b,c=Array.prototype.slice,d=function(a,b){return function(){return a.apply(b,arguments)}},e=Object.prototype.hasOwnProperty,f=function(a,b){function d(){this.constructor=a}for(var c in b)e.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a};localStorage.debugSharedCollection&&(typeof console!="undefined"&&console!==null?console.log:void 0)?b=function(){var a;return a=1<=arguments.length?c.call(arguments,0):[],a.unshift("SharedCollection:"),console.log.apply(console,a)}:b=function(){},a=function(){return((1+Math.random())*65536|0).toString(16).substring(1)},Backbone.sync=function(a,b,c){if(a==="delete")return c.success()},Backbone.SharedCollection=function(c){function e(a,b){b==null&&(b={}),this.captureError=d(this.captureError,this),this.classMap={},b.modelClasses&&this.mapTypes(b.modelClasses),this._addingQueue=[],b.sharejsDoc&&this._setShareJSDoc(b.sharejsDoc);if(!(this.collectionId=b.collectionId))throw new Error("SharedCollection needs a collectionId in options!");e.__super__.constructor.apply(this,arguments)}return f(e,c),e.version="0.1.0",e.prototype.model=Backbone.Model,e.generateGUID=function(){return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()},e.prototype.mapTypes=function(a){var b,c,d,e,f;f=[];for(d=0,e=a.length;d<e;d++){b=a[d];if(!b.prototype.type)throw new Error("Model class "+b.prototype.constructor.name+" is missing `type` attribute.");if(c=this.classMap[b.prototype.type])if(c!==b)throw new Error("Type id collision. "+b.prototype.constructor.name+" and "+c.prototype.constructor.name+" have the same type-property!");f.push(this.classMap[b.prototype.type]=b)}return f},e.prototype.captureError=function(a,c){var d=this;return function(d){if(d)return b("Sync error! "+c+":",d),a.trigger("syncerror",a,c,d)}},e.prototype.create=function(a,b){var c,d;b==null&&(b={});if(!(a instanceof Backbone.Model)){d=a,c=this.classMap[d.__type]||this.model,a=new c(d,{collection:this});if(a.validate&&!a._performValidation(d,b))return!1}return a.collection||(a.collection=this),this.add(a,b),a},e.prototype._setShareJSDoc=function(a){var b=this;return this._syncDoc=a,a.connection.on("disconnect",function(){return b.trigger("disconnect",b,a)})},e.prototype.fetch=function(a){var b,c=this;a==null&&(a={});if(this.fetched)return cb();this.fetched=!0,b=function(b){return b?a!=null?typeof a.error=="function"?a.error(b):void 0:void 0:(c.trigger("syncload"),c.connected=!0,c._flushAddingQueue(),c.trigger("connect",c),a!=null?typeof a.success=="function"?a.success():void 0:void 0)},a.sharejsDoc&&this._setShareJSDoc(a.sharejsDoc);if(this._syncDoc.type.name!=="json")throw new Error("The ShareJS document type must be 'json', not '"+this._syncDoc.type.name+"'");return this._bindSendOperations(),this._initSyncDoc(function(a){return a?b(a):c._initCollection(function(a){return a?b(a):c._loadModelsFromSyncDoc(b)})}),this._syncDoc.on("remoteop",function(a){var b,d,e,f;f=[];for(d=0,e=a.length;d<e;d++)b=a[d],b.p[0]===c.collectionId?f.push(c.parse(b)):f.push(void 0);return f})},e.prototype._flushAddingQueue=function(){var a,b;b=[];while(a=this._addingQueue.shift())b.push(this.add(a));return b},e.prototype._initSyncDoc=function(a){return this._syncDoc.created?this._syncDoc.snapshot?a():(b("Creating new sync doc"),this._syncDoc.submitOp([{p:[],oi:{}}],a)):a()},e.prototype._initCollection=function(a){return this._syncDoc.snapshot[this.collectionId]?a():(b("Adding new collection to syncdoc: "+this.collectionId),this._syncDoc.submitOp([{p:[this.collectionId],oi:{}}],a))},e.prototype._loadModelsFromSyncDoc=function(a){var c,d,e;a==null&&(a=function(){});if(e=this._syncDoc.snapshot[this.collectionId]){for(c in e)d=e[c],this.create(d);return a()}return b("Creating collection "+this.collectionId),this._syncDoc.submitOp([{p:[this.collectionId],oi:{}}],a)},e.prototype._bindSendOperations=function(){var a=this;return this.bind("change",function(b,c){if(c!=null?!c.local:!void 0)return a._sendModelChange(b)}),this.bind("add",function(b,c,d){if(d!=null?!d.local:!void 0)return a._sendModelAdd(b)}),this.bind("destroy",function(b,c,d){if(d!=null?!d.local:!void 0)return a._sendModelDestroy(b)})},e.prototype._sendModelChange=function(a){var c,d,e;d=function(){var d,f;d=a.changedAttributes(),f=[];for(c in d)e=d[c],b("SEND CHANGE: "+a.id+": "+c+": "+e),f.push({p:[this.collectionId,a.id,c],oi:e});return f}.call(this);if(!this._syncDoc.snapshot[this.collectionId][a.id])throw new Error("ERROR: snapshot has no model with id "+a.id);if(d.length!==0)return this._syncDoc.submitOp(d,this.captureError(a,"change"))},e.prototype._sendModelAdd=function(a,c){var d,f;a.id||a.set({id:e.generateGUID()});if((f=this._syncDoc.snapshot[this.collectionId])!=null?f[a.id]:void 0)return;b("SEND ADD "+a.id+": "+JSON.stringify(a.toJSON())),d=a.toJSON();if(a.type){if(!this.classMap[a.type])throw new Error("Cannot serialize model. Unkown type: '"+a.type+"'. You must add this model class to `modelClasses` options when creating this collection");d.__type=a.type}return this._syncDoc.submitOp([{p:[this.collectionId,a.id],oi:d}],this.captureError(a,"add"))},e.prototype._sendModelDestroy=function(a){return b("SEND REMOVE "+a.id),this._syncDoc.submitOp([{p:[this.collectionId,a.id],od:!0}],this.captureError(a,"destroy"))},e.prototype.parse=function(a){if(a.p.length===2){if(a.oi)return this._receiveModelAdd(a);if(a.od)return this._receiveModelDestroy(a)}return a.p[2]?this._receiveModelChange(a):b("Unkown model operation "+JSON.stringify(a))},e.prototype._receiveModelAdd=function(a){return b("RECEIVE ADD "+a.oi.id+": "+JSON.stringify(a.oi)),this.create(a.oi,{local:!0,remote:!0})},e.prototype._receiveModelDestroy=function(a){var c,d;d=a.p[1],c=this.get(d);if(!c)throw new Error("Remote asked to remove non existing model "+d);b("RECEIVE REMOVE "+c.id+": "+JSON.stringify(d)),c.destroy({local:!0,remote:!0});if(this._syncDoc.snapshot[this.collectionId][d])return b("ERROR: Model exists after deletion! "+d)},e.prototype._receiveModelChange=function(a){var c,d,e,f,g;f=a.p[1],c=a.p[2],d=a.oi,e=this.get(f);if(!e)throw new Error("Remote asked to update non existing model: "+e.id+" "+f);return b("RECEIVE CHANGE "+e.id+": "+c+": "+d),g={},g[c]=d,e.set(g,{local:!0,remote:!0})},e.prototype.add=function(a,b){var c,d,f,g;if(!a||a.length===0)return this;_.isArray(a)||(a=[a]);if(!this.fetched){while(d=a.shift())console.log("Adding "+d.get("name")+" to queue"),this._addingQueue.push(d);return this}for(f=0,g=a.length;f<g;f++)c=a[f],this._sendModelAdd(c,b);return e.__super__.add.apply(this,arguments),this},e}(Backbone.Collection)})).call(this);