Skip to content

Instantly share code, notes, and snippets.

@aredridel
Last active December 16, 2015 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aredridel/5481195 to your computer and use it in GitHub Desktop.
Save aredridel/5481195 to your computer and use it in GitHub Desktop.
Tiny backbone.js hints
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