a minimal, slightly opinionated plugin to simplify view rendering and composing complex views
Container
Table
Row
Row
Row
render: function() {
if (this.template) {
this.$el.html( this.template( this ) );
}
return this;
},
Default render provided to avoid
Backbone.View.extend({
template: JST['templates/index.ejs'],
...
});
// view specific
Backbone.View.extend({
template: JST['templates/index.ejs'],
attachToTemplate: true,
...
});
// globally set
Backbone.View.prototype.attachToTemplate = true;
Avoid dreaded <div> wrapper
Backbone.View.extend({
serializeData: function() {
var result = this.model.toJSON();
result.fullName = result.firstName + ' ' + result.lastName;
return result;
}
});
#template
<table>
<tr>
<td><%= fullName %></td>
</tr>
</table>
View Manager will use results as input to template rendering
Backbone.View.extend({
...
onRender: function() {
this.$('#state').select2();
}
});
fires rendered event and calls onRendered
showItems: function() {
this.collection.forEach(function(model){
var view = new ItemView({model: model});
this.addSubView({
view: view,
selector: 'prepend'
});
}, this);
}
addSubView adds a view to a parent view's DOM and manages it
var container = new Backbone.View({el: '.container'});
var view = new UserView({model: this.model});
container.setView(view);
Use setView to swap the view of a container
bower install backbone.composer
bower