Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created July 13, 2012 20:21
Show Gist options
  • Select an option

  • Save kimjoar/3107198 to your computer and use it in GitHub Desktop.

Select an option

Save kimjoar/3107198 to your computer and use it in GitHub Desktop.
Step 19 -> 20
var Status = Backbone.Model.extend({
url: '/status'
});
var Statuses = Backbone.Collection.extend({
model: Status
});
var NewStatusView = Backbone.View.extend({
- initialize: function(options) {
+ initialize: function() {
- this.statuses = options.statuses;
-
- this.statuses.on('add', this.clearInput, this);
+ this.collection.on('add', this.clearInput, this);
var add = $.proxy(this.addStatus, this);
this.$('form').submit(add);
},
addStatus: function(e) {
e.preventDefault();
- this.statuses.add({ text: this.$('textarea').val() });
+ this.collection.add({ text: this.$('textarea').val() });
},
clearInput: function() {
this.$('textarea').val('');
}
});
var StatusesView = Backbone.View.extend({
- initialize: function(options) {
+ initialize: function() {
- this.statuses = options.statuses;
-
- this.statuses.on('add', this.appendStatus, this);
+ this.collection.on('add', this.appendStatus, this);
},
appendStatus: function(status) {
this.$('ul').append('<li>' + status.get('text') + '</li>');
}
});
$(document).ready(function() {
var statuses = new Statuses();
- new NewStatusView({ el: $('#new-status'), statuses: statuses });
+ new NewStatusView({ el: $('#new-status'), collection: statuses });
- new StatusesView({ el: $('#statuses'), statuses: statuses });
+ new StatusesView({ el: $('#statuses'), collection: statuses });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment