@@ -112,6 +112,10 @@ <h3>Chat Connection ({this.props.channel})</h3>
112
112
< script type ="text/jsx " id ="playlistSource ">
113
113
/** @jsx React.DOM */
114
114
var SongController = function ( channel ) {
115
+ //defines actions that can be done on our Song model/collection
116
+ //connects those actions to a postal.js channel
117
+
118
+ //initial data
115
119
var songs = { "pf" : { title : 'Another Brick in the Wall' , album : 'The Wall' , artist : 'Pink Floyd' , _id :"pf" } } ;
116
120
117
121
function notifyDetail ( song ) {
@@ -122,6 +126,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
122
126
channel . publish ( "list" , _ . values ( songs ) ) ;
123
127
}
124
128
129
+ //actual operations
125
130
function create ( data , envelope ) {
126
131
data = data ? data : { } ;
127
132
data . _id = _ . uniqueId ( ) ;
@@ -156,7 +161,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
156
161
channel . subscribe ( "patch.#" , patch ) ;
157
162
channel . subscribe ( "delete.#" , remove ) ;
158
163
159
- //response to data requests
164
+ //respond to data requests
160
165
channel . subscribe ( "list.get" , function ( data , envelope ) {
161
166
envelope . reply ( _ . values ( songs ) ) ;
162
167
} ) ;
@@ -177,15 +182,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
177
182
console . log ( e . stack )
178
183
}
179
184
}
180
- if ( _ . isString ( channel ) ) {
181
- sub = postal . subscribe ( {
182
- channel : channel ,
183
- topic : topic ,
184
- callback : callback
185
- } ) ;
186
- } else {
187
- sub = channel . subscribe ( topic , callback ) ;
188
- }
185
+ sub = channel . subscribe ( topic , callback ) ;
189
186
this . registerSubscription ( sub )
190
187
} ,
191
188
registerSubscription : function ( sub ) {
@@ -196,6 +193,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
196
193
_ . each ( this . subscriptions , function ( sub ) {
197
194
sub . unsubscribe ( )
198
195
} ) ;
196
+ this . subscriptions = [ ] ;
199
197
}
200
198
} ;
201
199
@@ -205,6 +203,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
205
203
return { song : this . props . song }
206
204
} ,
207
205
componentWillMount : function ( ) {
206
+ //listen for updates on our song
208
207
this . subscribe ( this . props . songChannel , "instance." + this . state . song . _id , this . updateSong )
209
208
} ,
210
209
updateSong : function ( data , envelope ) {
@@ -234,6 +233,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
234
233
} ) ;
235
234
236
235
var SongPlayer = React . createClass ( {
236
+ //"plays" through our list of songs
237
237
getInitialState : function ( ) {
238
238
return {
239
239
current_song : null
@@ -243,13 +243,15 @@ <h3>Chat Connection ({this.props.channel})</h3>
243
243
this . playNext ( ) ;
244
244
} ,
245
245
playNext : function ( ) {
246
+ //find the next song to play and update our state
246
247
var index = _ . findIndex ( this . props . songs , { _id : this . state . current_song } ) ;
247
248
index += 1 ;
248
249
if ( index >= this . props . songs . length ) index = 0 ;
249
250
var song = this . props . songs [ index ]
250
251
if ( song ) {
251
252
this . setState ( { current_song : song . _id } ) ;
252
253
}
254
+ //schedule this function when the song "ends"
253
255
_ . delay ( this . playNext . bind ( this , null ) , 3000 ) ;
254
256
} ,
255
257
render : function ( ) {
@@ -270,11 +272,14 @@ <h3>Chat Connection ({this.props.channel})</h3>
270
272
} ;
271
273
} ,
272
274
componentWillMount : function ( ) {
275
+ //request the list of songs
273
276
this . props . songChannel . request ( {
274
277
topic : "list.get" ,
275
278
data : { } ,
276
279
timeout : 3000
277
280
} ) . then ( this . updateSongList ) ;
281
+
282
+ //listen for song list updates
278
283
this . subscribe ( this . props . songChannel , "list" , this . updateSongList ) ;
279
284
} ,
280
285
updateSongList : function ( data , envelope ) {
@@ -347,6 +352,7 @@ <h1>Models</h1>
347
352
< li > Simple applications may use Ajax calls direct from the component</ li >
348
353
< li > Use < a href ="https://www.npmjs.org/package/backbone-react-component "> backbone-react-component</ a > for backbone integration</ li >
349
354
< li > Facebook has their own model layer for react called < a href ="https://facebook.github.io/react/docs/flux-todo-list.html "> flux</ a > </ li >
355
+ < li > Postal.js provides a < a href ="https://github.com/postaljs/postal.request-response "> Request-Response</ a > library for putting requests over a channel</ li >
350
356
</ ul >
351
357
< div class ="result " id ="playlist "> </ div >
352
358
</ div >
0 commit comments