From 520ae34a4fe4bfce34bb154248eb60d1ce4d7908 Mon Sep 17 00:00:00 2001 From: Jason Morrison Date: Fri, 9 Sep 2011 14:43:37 -0400 Subject: [PATCH] Simplify RailsFayeSubscriber js implementation (previously coffee compiled) --- .../rails_faye_subscriber.js.erb | 125 +++++++----------- 1 file changed, 46 insertions(+), 79 deletions(-) diff --git a/vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb b/vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb index 01db98c..cef6d68 100644 --- a/vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb +++ b/vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb @@ -1,80 +1,47 @@ -// Original CoffeeScript: -// -// 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) +this.BackboneSync = this.BackboneSync || {}; -(function() { - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - window.BackboneSync = {}; - BackboneSync.RailsFayeSubscriber = (function() { - function RailsFayeSubscriber(collection, options) { - this.destroy = __bind(this.destroy, this); - this.create = __bind(this.create, this); - this.update = __bind(this.update, this); - this.receive = __bind(this.receive, this); - this.subscribe = __bind(this.subscribe, this); 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, this.receive); - }; - RailsFayeSubscriber.prototype.receive = function(message) { - return $.each(message, __bind(function(event, eventArguments) { - return this[event](eventArguments); - }, this)); - }; - RailsFayeSubscriber.prototype.update = function(params) { - return $.each(params, __bind(function(id, attributes) { - var model; - model = this.collection.get(id); - return model.set(attributes); - }, this)); - }; - RailsFayeSubscriber.prototype.create = function(params) { - return $.each(params, __bind(function(id, attributes) { - var model; - model = new this.collection.model(attributes); - return this.collection.add(model); - }, this)); - }; - RailsFayeSubscriber.prototype.destroy = function(params) { - return $.each(params, __bind(function(id, attributes) { - var model; - model = this.collection.get(id); - return this.collection.remove(model); - }, this)); - }; - return RailsFayeSubscriber; - })(); -}).call(this); +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; +})();