backbone_sync-rails 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -59,18 +59,22 @@ This assumes you already have a Backbone.js + Rails app.
59
59
  end
60
60
  ```
61
61
 
62
- 6. Instantiate a new `BackboneSync.RailsFayeSynchronizer` for *each instance*
62
+ 7. Instantiate a new `BackboneSync.RailsFayeSynchronizer` for *each instance*
63
63
  of a Backbone collection you instantiate. You could do this in the
64
64
  collection's constructor, or do it by hand:
65
65
 
66
66
  ```javascript
67
67
  // For simplicitly, here it is in a router, or app bootstrap
68
68
  this.users = new MyApp.Collections.UsersCollection();
69
- new BackboneSync.RailsFayeSubscriber(this.users, channel: 'users');
69
+ var fayeClient = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye');
70
+ new BackboneSync.RailsFayeSubscriber(this.users, {
71
+ channel: 'users', // Set to Rails model.class.table_name, or override Model#faye_channel
72
+ client: fayeClient
73
+ });
70
74
  this.wizards.reset(options.users);
71
75
  ```
72
76
 
73
- 7. Check it out! Open two browsers, do some stuff in one, and see your changes
77
+ 8. Check it out! Open two browsers, do some stuff in one, and see your changes
74
78
  cascade to the other. Your Backbone views will need to observe events on
75
79
  the collection like `change`, `add`, and `remove`.
76
80
 
@@ -1,4 +1,4 @@
1
- require 'backbone_sync-rails/faye/message'
1
+ require 'net/http'
2
2
 
3
3
  module BackboneSync
4
4
  module Rails
@@ -10,13 +10,22 @@ module BackboneSync
10
10
  end
11
11
 
12
12
  def broadcast
13
- Message.new(channel, data).send
13
+ Net::HTTP.post_form(uri, :message => message)
14
14
  end
15
15
 
16
16
  private
17
17
 
18
+ def uri
19
+ URI.parse("#{BackboneSync::Rails::Faye.root_address}/faye")
20
+ end
21
+
22
+ def message
23
+ { :channel => channel, :data => data }.to_json
24
+ end
25
+
18
26
  def channel
19
- "/sync/#{@model.class.table_name}"
27
+ subchannel = @model.try(:faye_channel) || @model.class.table_name
28
+ "/sync/#{subchannel}"
20
29
  end
21
30
 
22
31
  def data
@@ -4,6 +4,7 @@ module BackboneSync
4
4
  module Rails
5
5
  module Faye
6
6
  mattr_accessor :root_address
7
+ self.root_address = 'http://localhost:9292'
7
8
  end
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module BackboneSyncRails
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,80 +1,47 @@
1
- // Original CoffeeScript:
2
- //
3
- // window.BackboneSync = {}
4
- //
5
- // class BackboneSync.RailsFayeSubscriber
6
- // constructor: (collection, options) ->
7
- // @collection = collection
8
- // @client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye')
9
- // @channel = options.channel
10
- //
11
- // @subscribe()
12
- //
13
- // subscribe: =>
14
- // @client.subscribe "/sync/#{@channel}", @receive
15
- //
16
- // receive: (message) =>
17
- // $.each message, (event, eventArguments) =>
18
- // @[event](eventArguments)
19
- //
20
- // update: (params) =>
21
- // $.each params, (id, attributes) =>
22
- // model = @collection.get(id)
23
- // model.set(attributes)
24
- //
25
- // create: (params) =>
26
- // $.each params, (id, attributes) =>
27
- // model = new @collection.model(attributes)
28
- // @collection.add(model)
29
- //
30
- // destroy: (params) =>
31
- // $.each params, (id, attributes) =>
32
- // model = @collection.get(id)
33
- // @collection.remove(model)
1
+ this.BackboneSync = this.BackboneSync || {};
34
2
 
35
- (function() {
36
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
37
- window.BackboneSync = {};
38
- BackboneSync.RailsFayeSubscriber = (function() {
39
- function RailsFayeSubscriber(collection, options) {
40
- this.destroy = __bind(this.destroy, this);
41
- this.create = __bind(this.create, this);
42
- this.update = __bind(this.update, this);
43
- this.receive = __bind(this.receive, this);
44
- this.subscribe = __bind(this.subscribe, this); this.collection = collection;
45
- this.client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye');
46
- this.channel = options.channel;
47
- this.subscribe();
48
- }
49
- RailsFayeSubscriber.prototype.subscribe = function() {
50
- return this.client.subscribe("/sync/" + this.channel, this.receive);
51
- };
52
- RailsFayeSubscriber.prototype.receive = function(message) {
53
- return $.each(message, __bind(function(event, eventArguments) {
54
- return this[event](eventArguments);
55
- }, this));
56
- };
57
- RailsFayeSubscriber.prototype.update = function(params) {
58
- return $.each(params, __bind(function(id, attributes) {
59
- var model;
60
- model = this.collection.get(id);
61
- return model.set(attributes);
62
- }, this));
63
- };
64
- RailsFayeSubscriber.prototype.create = function(params) {
65
- return $.each(params, __bind(function(id, attributes) {
66
- var model;
67
- model = new this.collection.model(attributes);
68
- return this.collection.add(model);
69
- }, this));
70
- };
71
- RailsFayeSubscriber.prototype.destroy = function(params) {
72
- return $.each(params, __bind(function(id, attributes) {
73
- var model;
74
- model = this.collection.get(id);
75
- return this.collection.remove(model);
76
- }, this));
77
- };
78
- return RailsFayeSubscriber;
79
- })();
80
- }).call(this);
3
+ BackboneSync.RailsFayeSubscriber = (function() {
4
+ function RailsFayeSubscriber(collection, options) {
5
+ this.collection = collection;
6
+ this.client = options.client;
7
+ this.channel = options.channel;
8
+ this.subscribe();
9
+ }
10
+
11
+ RailsFayeSubscriber.prototype.subscribe = function() {
12
+ return this.client.subscribe("/sync/" + this.channel, _.bind(this.receive, this));
13
+ };
14
+
15
+ RailsFayeSubscriber.prototype.receive = function(message) {
16
+ var self = this;
17
+ return $.each(message, function(event, eventArguments) {
18
+ return self[event](eventArguments);
19
+ });
20
+ };
21
+
22
+ RailsFayeSubscriber.prototype.update = function(params) {
23
+ var self = this;
24
+ return $.each(params, function(id, attributes) {
25
+ var model = self.collection.get(id);
26
+ return model.set(attributes);
27
+ });
28
+ };
29
+
30
+ RailsFayeSubscriber.prototype.create = function(params) {
31
+ var self = this;
32
+ return $.each(params, function(id, attributes) {
33
+ var model = new self.collection.model(attributes);
34
+ return self.collection.add(model);
35
+ });
36
+ };
37
+
38
+ RailsFayeSubscriber.prototype.destroy = function(params) {
39
+ var self = this;
40
+ return $.each(params, function(id, attributes) {
41
+ var model = self.collection.get(id);
42
+ return self.collection.remove(model);
43
+ });
44
+ };
45
+
46
+ return RailsFayeSubscriber;
47
+ })();
@@ -2,7 +2,7 @@ Backbone.Collection.prototype._addWithIdCheck = function(model, options) {
2
2
  var idAttribute = model.idAttribute || this.model.prototype.idAttribute;
3
3
  var modelId = model[idAttribute];
4
4
 
5
- if (this.get(modelId) === undefined) {
5
+ if (modelId === undefined || this.get(modelId) === undefined) {
6
6
  this._addWithoutIdCheck(model, options);
7
7
  }
8
8
  };
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: backbone_sync-rails
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jason Morrison
@@ -10,12 +10,11 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-06 00:00:00 -04:00
13
+ date: 2011-09-21 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
18
- prerelease: false
19
18
  requirement: &id001 !ruby/object:Gem::Requirement
20
19
  none: false
21
20
  requirements:
@@ -23,10 +22,10 @@ dependencies:
23
22
  - !ruby/object:Gem::Version
24
23
  version: 3.0.0
25
24
  type: :runtime
25
+ prerelease: false
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
- prerelease: false
30
29
  requirement: &id002 !ruby/object:Gem::Requirement
31
30
  none: false
32
31
  requirements:
@@ -34,6 +33,7 @@ dependencies:
34
33
  - !ruby/object:Gem::Version
35
34
  version: "0"
36
35
  type: :development
36
+ prerelease: false
37
37
  version_requirements: *id002
38
38
  description: Broadcast changes from Rails models to client-side Backbone.js collections with WebSockets.
39
39
  email:
@@ -46,7 +46,6 @@ extra_rdoc_files: []
46
46
 
47
47
  files:
48
48
  - lib/backbone_sync-rails/faye/event.rb
49
- - lib/backbone_sync-rails/faye/message.rb
50
49
  - lib/backbone_sync-rails/faye/observer.rb
51
50
  - lib/backbone_sync-rails/faye.rb
52
51
  - lib/backbone_sync-rails/version.rb
@@ -100,12 +99,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
99
  requirements:
101
100
  - - ">="
102
101
  - !ruby/object:Gem::Version
102
+ hash: 2403046260054443418
103
+ segments:
104
+ - 0
103
105
  version: "0"
104
106
  required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  none: false
106
108
  requirements:
107
109
  - - ">="
108
110
  - !ruby/object:Gem::Version
111
+ hash: 2403046260054443418
112
+ segments:
113
+ - 0
109
114
  version: "0"
110
115
  requirements: []
111
116
 
@@ -1,31 +0,0 @@
1
- require 'net/http'
2
- require 'json'
3
-
4
- module BackboneSync
5
- module Rails
6
- module Faye
7
- # To publish from outside of an `EM.run {}` loop:
8
- # http://groups.google.com/group/faye-users/browse_thread/thread/ae6e2a1cc37b3b07
9
- class Message
10
- def initialize(channel, data)
11
- @channel = channel
12
- @data = data
13
- end
14
-
15
- def send
16
- Net::HTTP.post_form(uri, :message => payload)
17
- end
18
-
19
- private
20
-
21
- def uri
22
- URI.parse("#{BackboneSync::Rails::Faye.root_address}/faye")
23
- end
24
-
25
- def payload
26
- {:channel => @channel, :data => @data}.to_json
27
- end
28
- end
29
- end
30
- end
31
- end