Created
July 13, 2012 20:37
-
-
Save kimjoar/3107317 to your computer and use it in GitHub Desktop.
Done!
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 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.add({ text: this.$('textarea').val() }); | |
| }, | |
| clearInput: function() { | |
| this.$('textarea').val(''); | |
| } | |
| }); | |
| var StatusesView = Backbone.View.extend({ | |
| initialize: function() { | |
| 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'), collection: 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