Here i want to make a view in backbone.js
// The DOM element for a User item...
var UserView = Backbone.View.extend({
//... is a list tag.
tagName: "li",
// Cache the template function for a single item.
template: _.template($('#tmpl_occupant').html()),
// a one-to-one correspondence between a **User** and a **UserView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render);
this.model.view = this;
},
// Re-render the contents of the User item.
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
This view code i put in the user.js and when index.html load it is called and it gives error
str is null
http://myserver/rahul/js/underscore-1.1.3.js
Line 675
i think it is due to because error not come when i remove this line
template: _.template($('#tmpl_occupant').html()),
<script type="text/html" id="tmpl_occupant">
<%=user.username%> is in <%=gib.name%> (<%=channel%>)
</script>
i think it is because index.html is not completely loaded while this line is executed .so it don’t fine tmpl_occupant , what can i do to solve this problem.
Done it by passing directly string instead of jquery reference.
or
http://documentcloud.github.com/underscore/#template
and better solution is to compile the template and pass the template to the instance of view.