Skip to content

Instantly share code, notes, and snippets.

@rafaeljesus
Created March 21, 2014 18:24
Show Gist options
  • Select an option

  • Save rafaeljesus/9692573 to your computer and use it in GitHub Desktop.

Select an option

Save rafaeljesus/9692573 to your computer and use it in GitHub Desktop.
Backbone parse with Faye
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