backbone_sync-rails 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
@@ -7,6 +7,12 @@ system is Faye http://faye.jcoglan.com/.
|
|
7
7
|
|
8
8
|
This assumes you already have a Backbone.js + Rails app.
|
9
9
|
|
10
|
+
0. Install the gem, say in your `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'backbone_sync-rails', '~> 0.0.1'
|
14
|
+
```
|
15
|
+
|
10
16
|
1. Run a Faye server. It's pretty straightforward, check out `example_faye/run.sh` in this repo.
|
11
17
|
|
12
18
|
2. Tell your app where the faye server is. This may differ per Rails.env.
|
@@ -31,7 +37,7 @@ This assumes you already have a Backbone.js + Rails app.
|
|
31
37
|
<script type="text/javascript" src="<%= BackboneSync::Rails::Faye.root_address %>/faye.js"></script>
|
32
38
|
```
|
33
39
|
|
34
|
-
5. Observe model changes in Rails, and broadcast them.
|
40
|
+
5. Observe model changes in Rails, and broadcast them. The gem provides the guts of
|
35
41
|
an observer for you, so add a file like `app/models/user_observer.rb`:
|
36
42
|
|
37
43
|
```ruby
|
@@ -61,9 +67,9 @@ This assumes you already have a Backbone.js + Rails app.
|
|
61
67
|
|
62
68
|
```javascript
|
63
69
|
// For simplicitly, here it is in a router, or app bootstrap
|
64
|
-
this.users = new MyApp.Collections.UsersCollection()
|
65
|
-
new BackboneSync.RailsFayeSubscriber(this.users, channel: 'users')
|
66
|
-
this.wizards.reset
|
70
|
+
this.users = new MyApp.Collections.UsersCollection();
|
71
|
+
new BackboneSync.RailsFayeSubscriber(this.users, channel: 'users');
|
72
|
+
this.wizards.reset(options.users);
|
67
73
|
```
|
68
74
|
|
69
75
|
7. Check it out! Open two browsers, do some stuff in one, and see your changes
|
@@ -76,6 +82,13 @@ If you're on a version of Rails < 3.1, you'll probably have to copy some files
|
|
76
82
|
into your app by hand, like the `vendor/assets` files. You'll probably have to
|
77
83
|
require the `lib/backbone_sync-rails/faye.rb` file yourself, too.
|
78
84
|
|
85
|
+
## Example app
|
86
|
+
|
87
|
+
I wrote an untested example application that uses CoffeeScript and the
|
88
|
+
backbone-rails generators:
|
89
|
+
|
90
|
+
https://github.com/jasonm/wizards
|
91
|
+
|
79
92
|
## Caveats
|
80
93
|
|
81
94
|
In short, I augment the `Backbone.Collection.prototype._add` function so
|
@@ -0,0 +1,80 @@
|
|
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)
|
34
|
+
|
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);
|
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
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jason Morrison
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-09-06 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -52,7 +52,7 @@ files:
|
|
52
52
|
- lib/backbone_sync-rails/version.rb
|
53
53
|
- lib/backbone_sync-rails.rb
|
54
54
|
- lib/tasks/backbone_sync-rails_tasks.rake
|
55
|
-
- vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.
|
55
|
+
- vendor/assets/javascripts/backbone_sync-rails/rails_faye_subscriber.js.erb
|
56
56
|
- vendor/assets/javascripts/extensions/backbone.collection.idempotent.js
|
57
57
|
- MIT-LICENSE
|
58
58
|
- Rakefile
|
@@ -1,31 +0,0 @@
|
|
1
|
-
window.BackboneSync = {}
|
2
|
-
|
3
|
-
class BackboneSync.RailsFayeSubscriber
|
4
|
-
constructor: (collection, options) ->
|
5
|
-
@collection = collection
|
6
|
-
@client = new Faye.Client('<%= BackboneSync::Rails::Faye.root_address %>/faye')
|
7
|
-
@channel = options.channel
|
8
|
-
|
9
|
-
@subscribe()
|
10
|
-
|
11
|
-
subscribe: =>
|
12
|
-
@client.subscribe "/sync/#{@channel}", @receive
|
13
|
-
|
14
|
-
receive: (message) =>
|
15
|
-
$.each message, (event, eventArguments) =>
|
16
|
-
@[event](eventArguments)
|
17
|
-
|
18
|
-
update: (params) =>
|
19
|
-
$.each params, (id, attributes) =>
|
20
|
-
model = @collection.get(id)
|
21
|
-
model.set(attributes)
|
22
|
-
|
23
|
-
create: (params) =>
|
24
|
-
$.each params, (id, attributes) =>
|
25
|
-
model = new @collection.model(attributes)
|
26
|
-
@collection.add(model)
|
27
|
-
|
28
|
-
destroy: (params) =>
|
29
|
-
$.each params, (id, attributes) =>
|
30
|
-
model = @collection.get(id)
|
31
|
-
@collection.remove(model)
|