A View normally expects an object with these attributes before it can render:
{ el: '#someelement', model: someModel }
A View also allows us to bind the model’s events to functions in the view:
initialize: function() {
this.model.bind('change', this.renderFromModel, this);
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
renderFromModel: function() {
var t = _.template($('#some-template').html());
$('item-' + this.cid).html(t(this.toJSON()));
return this;
},
The problem is that the first time we instantiate a View for rendering, it is expecting an object with a Model in it; and the second time we render the view when it is called from within the Model, it is not. Because of this, I end up creating two render() functions.
Is there a better way of achieving single item render that can also respond to model.change() events?
i think you need to ensure your render method is always bound the view by calling underscore.js’ bindAll method.