Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created July 22, 2012 13:54
Show Gist options
  • Select an option

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

Select an option

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