diff --git a/published/understanding-backbone.md b/published/understanding-backbone.md index 8efb74c..f6c507b 100644 --- a/published/understanding-backbone.md +++ b/published/understanding-backbone.md @@ -1450,6 +1450,67 @@ engines, you might get this functionality out of the box. }); ``` +Split jQuery selectors into functions +--------------- + +``` diff +var Status = Backbone.Model.extend({ + url: '/status' +}); + +var Statuses = Backbone.Collection.extend({ + model: Status +}); + +var NewStatusView = Backbone.View.extend({ + events: { + 'submit form': 'addStatus' + }, + + initialize: function() { + this.collection.on('add', this.clearInput, this); + }, + + addStatus: function(e) { + e.preventDefault(); + +- this.collection.create({ text: this.$('textarea').val() }); ++ this.collection.create({ text: this.$textarea().val() }); + }, + + clearInput: function() { +- this.$('textarea').val(''); ++ this.$textarea().val(''); +- } ++ }, ++ ++ $textarea: function() { ++ return this.$('textarea'); ++ } +}); + +var StatusesView = Backbone.View.extend({ + initialize: function() { + this.collection.on('add', this.appendStatus, this); + }, + + appendStatus: function(status) { + this.$ul().append('