Created
July 13, 2012 20:20
-
-
Save kimjoar/3107188 to your computer and use it in GitHub Desktop.
Step 12 -> 13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var events = _.clone(Backbone.Events); | |
| var Statuses = function() { | |
| }; | |
| Statuses.prototype.add = function(text) { | |
| $.ajax({ | |
| url: '/status', | |
| type: 'POST', | |
| dataType: 'json', | |
| data: { text: text }, | |
| success: function(data) { | |
| events.trigger('status:add', data.text); | |
| } | |
| }); | |
| }; | |
| var NewStatusView = function(options) { | |
| this.statuses = options.statuses; | |
| - this.el = $('#new-status'); | |
| + this.el = options.el; | |
| events.on('status:add', this.clearInput, this); | |
| var add = $.proxy(this.addStatus, this); | |
| this.el.find('form').submit(add); | |
| }; | |
| NewStatusView.prototype.addStatus = function(e) { | |
| e.preventDefault(); | |
| this.statuses.add(this.el.find('textarea').val()); | |
| }; | |
| NewStatusView.prototype.clearInput = function() { | |
| this.el.find('textarea').val(''); | |
| }; | |
| -var StatusesView = function() { | |
| - this.el = $('#statuses'); | |
| +var StatusesView = function(options) { | |
| + this.el = options.el; | |
| events.on('status:add', this.appendStatus, this); | |
| }; | |
| StatusesView.prototype.appendStatus = function(text) { | |
| this.el.find('ul').append('<li>' + text + '</li>'); | |
| }; | |
| $(document).ready(function() { | |
| var statuses = new Statuses(); | |
| - new NewStatusView({ statuses: statuses }); | |
| - new StatusesView(); | |
| + new NewStatusView({ el: $('#new-status'), statuses: statuses }); | |
| + new StatusesView({ el: $('#statuses') }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment