a bit confused obout listening to changes on a model in Backbone
So I'm thinking of moving a jQuery app to backbone. I have a View which I
pass a collection that creates a list of Views which are each passed
models. This view outputs the information into an input. Is there a way, I
can propogate automatically from the view to the associated models or is
this done for me autamtically.
Say for example I have a view that looks like this:
I have the following and when I update this.update_value gets called. But
how would I know which model has changed? Can I get a reference to the
specific model causing the change event? Sorry if this sounds basic.
var HomepagePositionView = Backbone.View.extend({
className:'row',
initialize:function(){
_.bindAll(this, "render");
this.listenTo(this.model, 'change', this.update_value);
},
render:function(){
var html="<div class='span2'>" + this.model.get('name') +
"</div><div class='span1'><input type=text class='hp_score
input-mini pull-right' value='" + this.model.get('hp_score') + "'
/></div>";
$(this.el).html(html);
this.$('.hp_score').bind('change', this.update_model);
return this;
},
update_model:function(){
console.log('this model changed' + this.model.id); // this.model.id
is not working
}
});
and
var HomepagePositionsView = Backbone.View.extend({
render:function(){
// iterate through collection and for each model create a
HomepagePositionView
}
and I want the update button to be a blank if the underlying models have
not changed. Is there a way in Backbone to tell which models have changed
if I Church and State goes from 90 -> 95?
thx
No comments:
Post a Comment