Created
March 21, 2014 18:24
-
-
Save rafaeljesus/9692573 to your computer and use it in GitHub Desktop.
Backbone parse with Faye
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| this.BackboneSync = this.BackboneSync || {}; | |
| BackboneSync.RailsFayeSubscriber = (function() { | |
| function RailsFayeSubscriber(collection, options) { | |
| this.collection = collection; | |
| this.client = new Faye.Client(’<%= BackboneSync::Rails::Faye.root_address %>/faye’); | |
| this.channel = options.channel; | |
| this.subscribe(); | |
| } | |
| RailsFayeSubscriber.prototype.subscribe = function() { | |
| return this.client.subscribe(”/sync/” + this.channel, _.bind(this.receive, this)); | |
| }; | |
| RailsFayeSubscriber.prototype.receive = function(message) { | |
| var self = this; | |
| return $.each(message, function(event, eventArguments) { | |
| return self[event](eventArguments); | |
| }); | |
| }; | |
| RailsFayeSubscriber.prototype.update = function(params) { | |
| var self = this; | |
| return $.each(params, function(id, attributes) { | |
| var model = self.collection.get(id); | |
| return model.set(attributes); | |
| }); | |
| }; | |
| RailsFayeSubscriber.prototype.create = function(params) { | |
| var self = this; | |
| return $.each(params, function(id, attributes) { | |
| var model = new self.collection.model(attributes); | |
| return self.collection.add(model); | |
| }); | |
| }; | |
| RailsFayeSubscriber.prototype.destroy = function(params) { | |
| var self = this; | |
| return $.each(params, function(id, attributes) { | |
| var model = self.collection.get(id); | |
| return self.collection.remove(model); | |
| }); | |
| }; | |
| return RailsFayeSubscriber; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment