Skip to content

Commit

Permalink
Simplify RailsFayeSubscriber js implementation (previously coffee com…
Browse files Browse the repository at this point in the history
…piled)
  • Loading branch information
Jason Morrison committed Sep 21, 2011
1 parent 2275775 commit 520ae34
Showing 1 changed file with 46 additions and 79 deletions.
@@ -1,80 +1,47 @@
// Original CoffeeScript: this.BackboneSync = this.BackboneSync || {};
//
// window.BackboneSync = {}
//
// class BackboneSync.RailsFayeSubscriber
// constructor: (collection, options) ->
// @collection = collection
// @client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye')
// @channel = options.channel
//
// @subscribe()
//
// subscribe: =>
// @client.subscribe "/sync/#{@channel}", @receive
//
// receive: (message) =>
// $.each message, (event, eventArguments) =>
// @[event](eventArguments)
//
// update: (params) =>
// $.each params, (id, attributes) =>
// model = @collection.get(id)
// model.set(attributes)
//
// create: (params) =>
// $.each params, (id, attributes) =>
// model = new @collection.model(attributes)
// @collection.add(model)
//
// destroy: (params) =>
// $.each params, (id, attributes) =>
// model = @collection.get(id)
// @collection.remove(model)


(function() { BackboneSync.RailsFayeSubscriber = (function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; function RailsFayeSubscriber(collection, options) {
window.BackboneSync = {}; this.collection = collection;
BackboneSync.RailsFayeSubscriber = (function() { this.client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye');
function RailsFayeSubscriber(collection, options) { this.channel = options.channel;
this.destroy = __bind(this.destroy, this); this.subscribe();
this.create = __bind(this.create, this); }
this.update = __bind(this.update, this);
this.receive = __bind(this.receive, this); RailsFayeSubscriber.prototype.subscribe = function() {
this.subscribe = __bind(this.subscribe, this); this.collection = collection; return this.client.subscribe("/sync/" + this.channel, _.bind(this.receive, this));
this.client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye'); };
this.channel = options.channel;
this.subscribe(); RailsFayeSubscriber.prototype.receive = function(message) {
} var self = this;
RailsFayeSubscriber.prototype.subscribe = function() { return $.each(message, function(event, eventArguments) {
return this.client.subscribe("/sync/" + this.channel, this.receive); return self[event](eventArguments);
}; });
RailsFayeSubscriber.prototype.receive = function(message) { };
return $.each(message, __bind(function(event, eventArguments) {
return this[event](eventArguments); RailsFayeSubscriber.prototype.update = function(params) {
}, this)); var self = this;
}; return $.each(params, function(id, attributes) {
RailsFayeSubscriber.prototype.update = function(params) { var model = self.collection.get(id);
return $.each(params, __bind(function(id, attributes) { return model.set(attributes);
var model; });
model = this.collection.get(id); };
return model.set(attributes);
}, this)); RailsFayeSubscriber.prototype.create = function(params) {
}; var self = this;
RailsFayeSubscriber.prototype.create = function(params) { return $.each(params, function(id, attributes) {
return $.each(params, __bind(function(id, attributes) { var model = new self.collection.model(attributes);
var model; return self.collection.add(model);
model = new this.collection.model(attributes); });
return this.collection.add(model); };
}, this));
}; RailsFayeSubscriber.prototype.destroy = function(params) {
RailsFayeSubscriber.prototype.destroy = function(params) { var self = this;
return $.each(params, __bind(function(id, attributes) { return $.each(params, function(id, attributes) {
var model; var model = self.collection.get(id);
model = this.collection.get(id); return self.collection.remove(model);
return this.collection.remove(model); });
}, this)); };
};
return RailsFayeSubscriber; return RailsFayeSubscriber;
})(); })();
}).call(this);

0 comments on commit 520ae34

Please sign in to comment.