transistor 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -159,11 +159,11 @@ if (window.Transistor === undefined) {
159
159
  }
160
160
 
161
161
  (function (transistor) {
162
- var Binder = (function () {
163
- return function (array) {
164
- var H, set, insert, update, remove;
162
+ var Binder, ArrayBinding;
165
163
 
166
- H = (function () {
164
+ ArrayBinding = {
165
+ bindTo: function (array) {
166
+ var H = (function () {
167
167
  return {
168
168
  clear: function () {
169
169
  for (;array.length !== 0;) {
@@ -179,21 +179,66 @@ if (window.Transistor === undefined) {
179
179
  }
180
180
 
181
181
  throw new Error("unknown id: " + id);
182
- },
182
+ }
183
+ };
184
+ }());
185
+
186
+ return {
187
+ set: function (collection) {
188
+ H.clear();
189
+
190
+ if (collection !== undefined) {
191
+ for (var i = 0 ; i < collection.length; i += 1) {
192
+ array[i] = collection[i];
193
+ }
194
+ }
195
+ },
196
+ insert: function (entry) {
197
+ if (entry !== undefined) {
198
+ array.push(entry);
199
+ }
200
+ },
201
+ update: function (id, entry) {
202
+ if (id === undefined) {
203
+ throw new Error("undefined id");
204
+ }
205
+
206
+ array[H.indexOfId(id)] = entry;
207
+ },
208
+ remove: function (id) {
209
+ array.splice(H.indexOfId(id), 1);
210
+ }
211
+ };
212
+ }
213
+ };
214
+
215
+
216
+ Binder = (function () {
217
+ return function (binding) {
218
+ var validate, target;
219
+
220
+ if (Object.prototype.toString.call( binding ) === '[object Array]') {
221
+ target = ArrayBinding.bindTo(binding);
222
+ } else {
223
+ target = binding;
224
+ }
183
225
 
184
- expectCollection: function (args) {
226
+
227
+ validate = (function () {
228
+ return {
229
+ collection: function (args) {
185
230
  if (args.collection === undefined) {
186
231
  throw new Error("undefined collection");
187
232
  }
188
233
  },
189
234
 
190
- expectEntry: function (args) {
235
+ entry: function (args) {
191
236
  if (args.entry === undefined) {
192
237
  throw new Error("undefined entry");
193
238
  }
194
239
  },
195
240
 
196
- expectId: function (args) {
241
+ id: function (args) {
197
242
  if (args.id === undefined) {
198
243
  throw new Error("undefined id");
199
244
  }
@@ -201,34 +246,6 @@ if (window.Transistor === undefined) {
201
246
  };
202
247
  }());
203
248
 
204
- set = function (collection) {
205
- H.clear();
206
-
207
- if (collection !== undefined) {
208
- for (var i = 0 ; i < collection.length; i += 1) {
209
- array[i] = collection[i];
210
- }
211
- }
212
- };
213
-
214
- insert = function (entry) {
215
- if (entry !== undefined) {
216
- array.push(entry);
217
- }
218
- };
219
-
220
- update = function (id, entry) {
221
- if (id === undefined) {
222
- throw new Error("undefined id");
223
- }
224
-
225
- array[H.indexOfId(id)] = entry;
226
- };
227
-
228
- remove = function (id) {
229
- array.splice(H.indexOfId(id), 1);
230
- };
231
-
232
249
  return function (event, args) {
233
250
  if (args === undefined) {
234
251
  throw new Error("undefined arguments");
@@ -236,24 +253,24 @@ if (window.Transistor === undefined) {
236
253
 
237
254
  switch (event) {
238
255
  case 'init':
239
- set(args);
256
+ target.set(args);
240
257
  break;
241
258
  case 'set':
242
- H.expectCollection(args);
243
- set(args.collection);
259
+ validate.collection(args);
260
+ target.set(args.collection);
244
261
  break;
245
262
  case 'insert':
246
- H.expectEntry(args);
247
- insert(args.entry);
263
+ validate.entry(args);
264
+ target.insert(args.entry);
248
265
  break;
249
266
  case 'update':
250
- H.expectId(args);
251
- H.expectEntry(args);
252
- update(args.id, args.entry);
267
+ validate.id(args);
268
+ validate.entry(args);
269
+ target.update(args.id, args.entry);
253
270
  break;
254
271
  case 'remove':
255
- H.expectId(args);
256
- remove(args.id);
272
+ validate.id(args);
273
+ target.remove(args.id);
257
274
  break;
258
275
  }
259
276
  };
data/index.html ADDED
@@ -0,0 +1,41 @@
1
+ <!DOCTYPE HTML>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
+ <title>radiotower</title>
6
+ <!-- <script type="text/javascript" src="js/vendor/faye-browser-min.js"></script> -->
7
+ <!-- <script type="text/javascript" src="js/vendor/jsonp.js"></script> -->
8
+ <!-- <script type="text/javascript" src="js/transistor/radio.js"></script> -->
9
+ <!-- <script type="text/javascript" src="js/transistor/control.js"></script> -->
10
+ <!-- <script type="text/javascript" src="js/transistor/binder.js"></script> -->
11
+ <script type="text/javascript" src="build/src/transistor.js"></script>
12
+ <script type="text/javascript">
13
+
14
+ var control = Transistor.Control(
15
+ 'http://localhost:9700', // broadcaster
16
+ 'e0745051ed75e95d1088986fedbde764', // station uid
17
+ '26d47e42b5ce83a934725f5e18e01d2b' // push token
18
+ )
19
+
20
+ var radio = Transistor.Radio(
21
+ 'http://localhost:9703', // station_finder
22
+ 'e0745051ed75e95d1088986fedbde764', // station uid
23
+ '4b9b62aae02f1f4fe079da7bcd0be00d' // test/live token
24
+ );
25
+
26
+ // explicit listener function
27
+ radio.tune("about", function(event, args) {
28
+ console.log('about', event, args);
29
+ });
30
+
31
+ // bind channel to a local array
32
+ var news = [];
33
+ radio.tune("news", Transistor.Binder(news));
34
+
35
+ radio.turnOn();
36
+ </script>
37
+ </head>
38
+ <body>
39
+
40
+ </body>
41
+ </html>
data/jake.yml CHANGED
@@ -25,6 +25,9 @@ packages:
25
25
  - vendor/jsonp
26
26
  - transistor/control
27
27
 
28
+ transistor-backbone:
29
+ - transistor/backbone-collection.js
30
+
28
31
  transistor:
29
32
  - vendor/faye-browser-min
30
33
  - vendor/jsonp
@@ -0,0 +1,100 @@
1
+ /*global Backbone, _ */
2
+
3
+ if (window.Transistor === undefined) {
4
+ window.Transistor = {};
5
+ }
6
+
7
+ (function (transistor) {
8
+ var BackboneCollection = (function () {
9
+ return function (radio, channel) {
10
+ var H, backbone_collection, collection_interface, binder;
11
+
12
+ H = (function () {
13
+ return {
14
+ };
15
+ }());
16
+
17
+ backbone_collection = new Backbone.Collection();
18
+
19
+ collection_interface = {
20
+ add: backbone_collection.add,
21
+ remove: backbone_collection.remove,
22
+ reset: backbone_collection.reset,
23
+ set: backbone_collection.set
24
+ };
25
+
26
+ (function (writer) {
27
+ var frozen = true,
28
+ delegate = function (func) {
29
+ writer[func] = (function (name) {
30
+ return function () {
31
+ if (frozen) {
32
+ throw "BackboneCollection is not mutable by user.";
33
+ } else {
34
+ return collection_interface[name].apply(backbone_collection, arguments);
35
+ }
36
+ };
37
+ }(func));
38
+ };
39
+
40
+ collection_interface.unfreeze = function () {
41
+ frozen = false;
42
+ };
43
+
44
+ collection_interface.freeze = function () {
45
+ frozen = true;
46
+ };
47
+
48
+ collection_interface.unfrozen = function (unfrozen_context) {
49
+ collection_interface.unfreeze();
50
+ unfrozen_context();
51
+ collection_interface.freeze();
52
+ };
53
+
54
+ delegate('add');
55
+ delegate('remove');
56
+ delegate('reset');
57
+ delegate('set');
58
+ }(backbone_collection));
59
+
60
+ binder = transistor.Binder({
61
+ set: function (collection) {
62
+ collection_interface.unfrozen(function () {
63
+ var models = _.map(collection, function (entry) {
64
+ return new Backbone.Model(entry);
65
+ });
66
+ backbone_collection.reset(models);
67
+ });
68
+ },
69
+ insert: function (entry) {
70
+ collection_interface.unfrozen(function () {
71
+ backbone_collection.add(new Backbone.Model(entry));
72
+ });
73
+ },
74
+ update: function (id, entry) {
75
+ collection_interface.unfrozen(function () {
76
+ var model = backbone_collection.get(id);
77
+ model.clear({silent: true});
78
+ model.set(entry);
79
+ });
80
+ },
81
+ remove: function (id) {
82
+ collection_interface.unfrozen(function () {
83
+ var model = backbone_collection.get(id);
84
+ backbone_collection.remove(model);
85
+ });
86
+ }
87
+ });
88
+
89
+ radio.tune(channel, binder);
90
+
91
+ return backbone_collection;
92
+ };
93
+ }());
94
+
95
+ if (transistor.Backbone === undefined) {
96
+ transistor.Backbone = {};
97
+ }
98
+ transistor.Backbone.Collection = BackboneCollection;
99
+
100
+ }(window.Transistor));
@@ -0,0 +1,126 @@
1
+ if (window.Transistor === undefined) {
2
+ window.Transistor = {};
3
+ }
4
+
5
+ (function (transistor) {
6
+ var Binder, ArrayBinding;
7
+
8
+ ArrayBinding = {
9
+ bindTo: function (array) {
10
+ var H = (function () {
11
+ return {
12
+ clear: function () {
13
+ for (;array.length !== 0;) {
14
+ array.pop();
15
+ }
16
+ },
17
+
18
+ indexOfId: function (id) {
19
+ for (var i = 0; i < array.length; i += 1) {
20
+ if (array[i].id === id) {
21
+ return i;
22
+ }
23
+ }
24
+
25
+ throw new Error("unknown id: " + id);
26
+ }
27
+ };
28
+ }());
29
+
30
+ return {
31
+ set: function (collection) {
32
+ H.clear();
33
+
34
+ if (collection !== undefined) {
35
+ for (var i = 0 ; i < collection.length; i += 1) {
36
+ array[i] = collection[i];
37
+ }
38
+ }
39
+ },
40
+ insert: function (entry) {
41
+ if (entry !== undefined) {
42
+ array.push(entry);
43
+ }
44
+ },
45
+ update: function (id, entry) {
46
+ if (id === undefined) {
47
+ throw new Error("undefined id");
48
+ }
49
+
50
+ array[H.indexOfId(id)] = entry;
51
+ },
52
+ remove: function (id) {
53
+ array.splice(H.indexOfId(id), 1);
54
+ }
55
+ };
56
+ }
57
+ };
58
+
59
+
60
+ Binder = (function () {
61
+ return function (binding) {
62
+ var validate, target;
63
+
64
+ if (Object.prototype.toString.call( binding ) === '[object Array]') {
65
+ target = ArrayBinding.bindTo(binding);
66
+ } else {
67
+ target = binding;
68
+ }
69
+
70
+
71
+ validate = (function () {
72
+ return {
73
+ collection: function (args) {
74
+ if (args.collection === undefined) {
75
+ throw new Error("undefined collection");
76
+ }
77
+ },
78
+
79
+ entry: function (args) {
80
+ if (args.entry === undefined) {
81
+ throw new Error("undefined entry");
82
+ }
83
+ },
84
+
85
+ id: function (args) {
86
+ if (args.id === undefined) {
87
+ throw new Error("undefined id");
88
+ }
89
+ }
90
+ };
91
+ }());
92
+
93
+ return function (event, args) {
94
+ if (args === undefined) {
95
+ throw new Error("undefined arguments");
96
+ }
97
+
98
+ switch (event) {
99
+ case 'init':
100
+ target.set(args);
101
+ break;
102
+ case 'set':
103
+ validate.collection(args);
104
+ target.set(args.collection);
105
+ break;
106
+ case 'insert':
107
+ validate.entry(args);
108
+ target.insert(args.entry);
109
+ break;
110
+ case 'update':
111
+ validate.id(args);
112
+ validate.entry(args);
113
+ target.update(args.id, args.entry);
114
+ break;
115
+ case 'remove':
116
+ validate.id(args);
117
+ target.remove(args.id);
118
+ break;
119
+ }
120
+ };
121
+ };
122
+ }());
123
+
124
+ transistor.Binder = Binder;
125
+
126
+ }(window.Transistor));
@@ -0,0 +1,64 @@
1
+ /*global JSONP */
2
+ if (window.Transistor === undefined) {
3
+ window.Transistor = {};
4
+ }
5
+
6
+ (function (transistor) {
7
+
8
+ var Control = (function () {
9
+
10
+ return function (broadcaster, station_uid, token) {
11
+ var jsonp, set, insert, update, remove, command_url, send;
12
+
13
+ jsonp = JSONP();
14
+
15
+ set = function (channel, collection, cb) {
16
+ send(command_url('set', channel), {collection: collection}, cb);
17
+ };
18
+
19
+ insert = function (channel, entry, cb) {
20
+ send(command_url('insert', channel), {entry: entry}, cb);
21
+ };
22
+
23
+ update = function (channel, id, entry, cb) {
24
+ send(command_url('update', channel), {id: id, entry: entry}, cb);
25
+ };
26
+
27
+ remove = function (channel, id, cb) {
28
+ send(command_url('remove', channel), {id: id}, cb);
29
+ };
30
+
31
+ command_url = function (cmd, channel) {
32
+ var base = broadcaster + '/command/' + cmd + '/' + station_uid + '/' + token;
33
+
34
+ if (channel === undefined) {
35
+ return base;
36
+ } else {
37
+ return base + '/' + channel;
38
+ }
39
+ };
40
+
41
+ send = function (command_url, data, cb) {
42
+ if (cb === undefined) {
43
+ cb = function (result) {};
44
+ }
45
+
46
+ if (data === undefined) {
47
+ jsonp.get(command_url, {}, cb);
48
+ } else {
49
+ jsonp.get(command_url, {data: JSON.stringify(data)}, cb);
50
+ }
51
+ };
52
+
53
+ return {
54
+ set: set,
55
+ insert: insert,
56
+ update: update,
57
+ remove: remove
58
+ };
59
+ };
60
+ }());
61
+
62
+ transistor.Control = Control;
63
+
64
+ }(window.Transistor));