text: !function(){var a;Backbone.PubNub=function(a,b){var c=this;return this.name=b,this.ref=a,this.uuid=this.ref.uuid(),this.channel="backbone-"+this.name,this.records=[],this.ref.subscribe({channel:this.channel,callback:function(a){if(a=JSON.parse(a),a.uuid!==c.uuid)switch(a.method){case"create":return c.create(a.model);case"update":return c.update(a.model);case"delete":return c.destroy(a.model)}}})},_.extend(Backbone.PubNub.prototype,{publish:function(a,b,c){var d;return d={method:a,model:b,options:c,uuid:this.uuid},d=JSON.stringify(d),this.ref.publish({channel:this.channel,message:d})},read:function(a){return null==a.id?this.find(a.id):this.findAll()},find:function(a){return _.find(this.records,function(b){return b.id===a})},findAll:function(){return this.records},create:function(a){return null==a.id&&(a.id=this.ref.uuid(),a.set(a.idAttribute,a.id)),this.records.push(a),this.publish("create",a),a},update:function(a){var b;return b=this.find(a.id),this.records[this.records.indexOf(b)]=a,this.publish("update",a),a},destroy:function(a){return a.isNew()?!1:(this.records=_.reject(this.records,function(b){return b.id===a.id}),this.publish("delete",a),a)}}),Backbone.PubNub.sync=function(a,b){var c,d,e,f,g;e=null!=(g=b.pubnub)?g:b.collection.pubnub;try{switch(a){case"read":return f=e.read(b);case"create":return f=e.create(b);case"update":return f=e.update(b);case"delete":return f=e.destroy(b)}}catch(h){return c=h,d=c.message,console.log("ERROR",c)}},a=Backbone.sync,Backbone.sync=function(b,c,d){var e;return e=a,(c.pubnub||c.collection&&c.collection.pubnub)&&(e=Backbone.PubNub.sync),e.apply(this,[b,c,d])},Backbone.PubNub.Collection=Backbone.Collection.extend({publish:function(a,b,c){var d;return d={method:a,model:b,options:c,uuid:this.uuid},d=JSON.stringify(d),this.pubnub.publish({channel:this.channel,message:d})},constructor:function(a,b){var c,d=this;return Backbone.Collection.apply(this,arguments),b&&b.pubnub&&(this.pubnub=b.pubnub),this.uuid=this.pubnub.uuid(),this.channel="backbone-collection-"+this.name,this.pubnub.subscribe({channel:this.channel,callback:function(a){if(a=JSON.parse(a),a.uuid!==d.uuid)switch(a.method){case"create":return d._onAdded(a.model,a.options);case"update":return d._onChanged(a.model,a.options);case"delete":return d._onRemoved(a.model,a.options)}}}),c=function(a){return this.publish("update",a)},this.listenTo(this,"change",c,this)},_onAdded:function(a,b){return Backbone.Collection.prototype.add.apply(this,[a,b])},_onChanged:function(a,b){var c,d;if(a.id){if(d=_.find(this.models,function(b){return b.id===a.id}),null==d)throw new Error("Could not find model with ID: "+a.id);return c=_.difference(_.keys(d.attributes),_.keys(a)),_.each(c,function(a){return d.unset(a)}),d.set(a,b)}},_onRemoved:function(a,b){return Backbone.Collection.prototype.remove.apply(this,[a,b])},add:function(a,b){var c,d,e;for(a=_.isArray(a)?a.slice():[a],d=0,e=a.length;e>d;d++)c=a[d],null==c.id&&(c.id=this.pubnub.uuid(),c.set(c.idAttribute,c.id)),this.publish("create",c,b);return Backbone.Collection.prototype.add.apply(this,arguments)},remove:function(a,b){var c,d,e;for(a=_.isArray(a)?a.slice():[a],d=0,e=a.length;e>d;d++)c=a[d],this.publish("delete",c,b);return Backbone.Collection.prototype.remove.apply(this,arguments)}}),Backbone.PubNub.Model=Backbone.Model.extend({publish:function(a,b,c){var d;return d={method:a,model:b,options:c,uuid:this.uuid},d=JSON.stringify(d),this.pubnub.publish({channel:this.channel,message:d})},constructor:function(a,b){var c=this;return Backbone.Model.apply(this,arguments),b&&b.pubnub&&b.name&&(this.pubnub=b.pubnub,this.name=b.name),this.uuid=this.pubnub.uuid(),this.channel="backbone-model-"+this.name,this.on("change",this._onChange,this),this.pubnub.subscribe({channel:this.channel,callback:function(a){if(a=JSON.parse(a),a.uuid!==c.uuid)switch(a.method){case"update":return c._onChanged(a.model,a.options);case"delete":return c._onRemoved(a.options)}}})},_onChange:function(a,b){return this.publish("update",a,b)},_onChanged:function(a){var b,c=this;return this.off("change",this._onChange,this),b=_.difference(_.keys(this.attributes),_.keys(a)),_.each(b,function(a){return c.unset(a)}),this.set(a),this.on("change",this._onChange,this)},_onRemoved:function(){return Backbone.Model.prototype.destroy.apply(this,arguments)},destroy:function(a){return this.publish("delete",null,a),Backbone.Model.prototype.destroy.apply(this,arguments)}})}.call(this);