Skip to content

Commit 0a8410f

Browse files
committed
more comments
1 parent 6a5dfc9 commit 0a8410f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lesson3.html

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ <h3>Chat Connection ({this.props.channel})</h3>
112112
<script type="text/jsx" id="playlistSource">
113113
/** @jsx React.DOM */
114114
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
115119
var songs = {"pf": {title: 'Another Brick in the Wall', album: 'The Wall', artist: 'Pink Floyd', _id:"pf"}};
116120

117121
function notifyDetail(song) {
@@ -122,6 +126,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
122126
channel.publish("list", _.values(songs));
123127
}
124128

129+
//actual operations
125130
function create(data, envelope) {
126131
data = data ? data : {};
127132
data._id = _.uniqueId();
@@ -156,7 +161,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
156161
channel.subscribe("patch.#", patch);
157162
channel.subscribe("delete.#", remove);
158163

159-
//response to data requests
164+
//respond to data requests
160165
channel.subscribe("list.get", function(data, envelope) {
161166
envelope.reply(_.values(songs));
162167
});
@@ -177,15 +182,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
177182
console.log(e.stack)
178183
}
179184
}
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);
189186
this.registerSubscription(sub)
190187
},
191188
registerSubscription: function(sub) {
@@ -196,6 +193,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
196193
_.each(this.subscriptions, function(sub) {
197194
sub.unsubscribe()
198195
});
196+
this.subscriptions = [];
199197
}
200198
};
201199

@@ -205,6 +203,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
205203
return {song: this.props.song}
206204
},
207205
componentWillMount: function() {
206+
//listen for updates on our song
208207
this.subscribe(this.props.songChannel, "instance."+this.state.song._id, this.updateSong)
209208
},
210209
updateSong: function(data, envelope) {
@@ -234,6 +233,7 @@ <h3>Chat Connection ({this.props.channel})</h3>
234233
});
235234

236235
var SongPlayer = React.createClass({
236+
//"plays" through our list of songs
237237
getInitialState: function() {
238238
return {
239239
current_song: null
@@ -243,13 +243,15 @@ <h3>Chat Connection ({this.props.channel})</h3>
243243
this.playNext();
244244
},
245245
playNext: function() {
246+
//find the next song to play and update our state
246247
var index = _.findIndex(this.props.songs, {_id: this.state.current_song});
247248
index += 1;
248249
if (index >= this.props.songs.length) index = 0;
249250
var song = this.props.songs[index]
250251
if (song) {
251252
this.setState({current_song: song._id});
252253
}
254+
//schedule this function when the song "ends"
253255
_.delay(this.playNext.bind(this, null), 3000);
254256
},
255257
render: function() {
@@ -270,11 +272,14 @@ <h3>Chat Connection ({this.props.channel})</h3>
270272
};
271273
},
272274
componentWillMount: function() {
275+
//request the list of songs
273276
this.props.songChannel.request({
274277
topic: "list.get",
275278
data: {},
276279
timeout: 3000
277280
}).then(this.updateSongList);
281+
282+
//listen for song list updates
278283
this.subscribe(this.props.songChannel, "list", this.updateSongList);
279284
},
280285
updateSongList: function(data, envelope) {
@@ -347,6 +352,7 @@ <h1>Models</h1>
347352
<li>Simple applications may use Ajax calls direct from the component</li>
348353
<li>Use <a href="https://www.npmjs.org/package/backbone-react-component">backbone-react-component</a> for backbone integration</li>
349354
<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>
350356
</ul>
351357
<div class="result" id="playlist"></div>
352358
</div>

0 commit comments

Comments
 (0)