我在我的 Backbone 视图中看到了一些奇怪的行为。当我 console.log "this.model" 我在我的渲染方法和我的一个自定义方法中得到不同的结果。
这就是我设置路线的方式:
app_router.on('route:showTemplates', function(id) {
var listtemplate = new ListTemplateModel.Model({
身份证:身份证
});
listtemplate.fetch().then(function() {
var detailsview = new ListDetailsView.View({
模型:列表模板
});
});
});
如果我 console.log 在我的视图的渲染方法中,我得到了我所期望的:
定义(['use!backbone','helpers/templateHelper'],函数(B,TemplateHelper){
var View = B.View.extend({
el: '#page',
模板:TemplateHelper('taskDetailTemplate'),
事件:{
'keypress #new-step':'addOnEnter'
},
初始化:函数(){
this.render();
},
渲染:函数(){
this.$el.html(this.template(this.model.toJSON()));
},
addOnEnter:函数(e){
this.input = this.$('#new-step');
if (e.which !== 13) {// || !this.input.val().trim()) {
返回;
}
console.log(this.model.toJSON());
}
});
返回 {
查看:查看
};
});
但是我的“addOnEnter”方法中的console.log 返回了一个包含通过会话创建的所有模型实例的数组。就像所有先前创建的模型也触发了该事件。
