Last active
December 16, 2015 18:58
-
-
Save aredridel/5481195 to your computer and use it in GitHub Desktop.
Tiny backbone.js hints
This file contains 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 RowView = Backbone.View.extend({ | |
initialize: function () { | |
this.model.on('change', this.handleChange, this); | |
}, | |
handleChange: function () { | |
this.model.save(); | |
}, | |
render: function () { | |
this.setElement($('<tr>')); | |
this.$el.append("<td>" + this.model.get('id') + "</td><td>" + this.model.get('field1') + "</td>"); | |
return this; | |
}, | |
events: { | |
"change [name]": function (ev) { | |
this.model.set($(ev.target).attr('name'), $(ev.target).val()); | |
} | |
} | |
}); | |
var PageView = Backbone.View.extend({ | |
initialize: function() { | |
this.collection.on('reset', this.render, this); | |
}, | |
render: function () { | |
this.$el.append('<table>'); | |
_.each(this.collection, function (model){ | |
var row = new RowView({model: model}); | |
this.$('table').append(row.render().$el); | |
}); | |
} | |
}); | |
var Orders = Backbone.Collection.extend({ | |
url: "/orders" | |
}); | |
var orders = new Orders(); | |
var page = new PageView({collection: orders}); | |
orders.fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment