forked from jasonm/backbone_sync-rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify RailsFayeSubscriber js implementation (previously coffee com…
…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.
There are no files selected for viewing
125 changes: 46 additions & 79 deletions
125
vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |