Created
July 13, 2012 20:19
-
-
Save kimjoar/3107179 to your computer and use it in GitHub Desktop.
Step 8 -> 9
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(options) { | |
| $.ajax({ | |
| url: '/status', | |
| type: 'POST', | |
| dataType: 'json', | |
| data: { text: options.text }, | |
| success: options.success | |
| }); | |
| }; | |
| var NewStatusView = function(options) { | |
| this.statuses = options.statuses; | |
| + events.on('status:add', this.appendStatus, this); | |
| + events.on('status:add', this.clearInput, this); | |
| + | |
| var add = $.proxy(this.addStatus, this); | |
| $('#new-status form').submit(add); | |
| }; | |
| NewStatusView.prototype.addStatus = function(e) { | |
| e.preventDefault(); | |
| - var that = this; | |
| - | |
| this.statuses.add({ | |
| text: $('#new-status textarea').val(), | |
| success: function(data) { | |
| - that.appendStatus(data.text); | |
| - that.clearInput(); | |
| + events.trigger('status:add', data.text); | |
| } | |
| }); | |
| }; | |
| NewStatusView.prototype.appendStatus = function(text) { | |
| $('#statuses ul').append('<li>' + text + '</li>'); | |
| }; | |
| NewStatusView.prototype.clearInput = function() { | |
| $('#new-status textarea').val(''); | |
| }; | |
| $(document).ready(function() { | |
| var statuses = new Statuses(); | |
| new NewStatusView({ statuses: statuses }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment