我正在使用Handlebars模板引擎。 所以,我有模特:
Backbone.Model.extend({
urlRoot: Config.urls.getClient,
defaults: {
contract:"",
contractDate:"",
companyTitle:"",
contacts:[],
tariff: new Tariff(),
tariffs: [],
remain:0,
licenses:0,
edo:""
},
initialize:function(){
this.fetch();
}
});
然后木偶项目查看:
Marionette.ItemView.extend({
template : templates.client,
initialize: function () {
this.model.on('change', this.render, this);
},
onRender: function () {
console.log(this.model.toJSON());
}
});
然后我称之为:
new View({
model : new Model({id:id})
})
而且,它会立即为我呈现一个视图,这很酷。但是在模型获取数据之后它会触发“更改”,所以我在控制台序列化模型中看到两次,我第一次看到空模型,然后填充一个。
但是,视图未更新。
我怎么解决它?
附:我明白,我可以在fetch done回调上调用render方法。但是当用户改变模型时,我还需要它进行进一步的操作。
在视图中,您可以使用以下代码
modelEvents: {
'change': 'render'
}
代替
initialize: function () {
this.model.on('change', this.render, this);
},
onRender: function () {
console.log(this.model.toJSON());
}
实际上,Backbone和Marionette足够聪明。 问题出在模板和数据中,因为我发现它是another question。所以,我重新检查了一切并得到了结果。