My app contains two views with templates, router, model and collection.
root
js
collections
-collection.js
models
-model.js
routers
-router.js
templates
-add_item.html
-list.html
views
-add_item.js
-list.js
index.html
In router.js
, I'm trying to navigate to a child view.
define([
'jquery',
'backbone',
'views/item'
], function($, Backbone, ItemView) {
'use strict';
return Backbone.Router.extend({
routes: {
'': 'list',
'list/add': 'addItem',
'list/edit/:id': 'editItem'
},
addItem: function() {
new ItemView();
}
});
});
By looking at the call stack, I see that my router triggers. But the template of my child view doesn't initializes.
item.js
code:
return Backbone.View.extend({
template: _.template(itemTemplate),
events: {
'click #save': 'saveItem',
'click #cancel': 'cancel'
},
initialize: function() {
this.render();
},
render: function() {
this.$el.html(this.template);
return this;
}
});
Underscore's
_.template
This line is fine, but the following one puts the returned function as the HTML content:
You need to call the pre-compiled template function to get the string:
Backbone view rendering
Now that the view's default root element (
el
) is correctly filled with the template content.But once the template is rendered, the view's element is still not in the page. To make it part of the page, you could:
put the view's
el
manually into another element in the routerI put
view.render()
here because rendering in theinitialize
is not my preferred pattern as it's limiting the reuse of the view. But it really depends on what's the view for.pass a selector string or element to the
el
constructor option of the viewcreate the view with a hard-coded
el
property (this can be overridden by theel
option above)Default route
If you want to default to the
list
route, you could make theroutes
object like this:Additional information:
$el
andel
?_.template
explanation and example