骨干视图不侦听模型更改
Backbone view not listeningTo a model change
数据结构:
- Measure (collection)
- Measure (model)
- beats (c)
- beat (m)
- on/off (attribute)
- representations (c)
- representation (m)
- currentType (attribute)
- previousType (a)
表示模型通过转换函数被调用,我通过控制台打印输出注意到更改,但是,视图根本没有注册更改。 我有权访问点击事件,因此我知道视图的el
是正确的。 为什么侦听在视图中不起作用?
代表模型:
define([
'underscore',
'backbone'
], function(_, Backbone) {
var RepresentationModel = Backbone.Model.extend({
initialize: function(options){
this.representationType = options.representationType;
this.previousRepresentationType = undefined;
},
transition: function(newRep){
this.previousRepresentationType = this.representationType;
this.representationType = newRep;
console.error('model change : ' + this.previousRepresentationType + ' ' + this.representationType);
}
});
return RepresentationModel;
});
度量表示视图:
define([…], function(…){
return Backbone.View.extend({
initialize: function(options){
if (options) {
for (var key in options) {
this[key] = options[key];
}
}
//Dispatch listeners
…
//Binding
//this was the old way, so I changed to the new listenTo to take advantage of when the view is destroyed.
//this.model.bind('change', _.bind(this.transition, this));
this.listenTo(this.model, 'change', _.bind(this.transition, this));
this.render();
},
render: function(){
// compile the template for a representation
var measureRepTemplateParamaters = {…};
var compiledTemplate = _.template( MeasureRepTemplate, measureRepTemplateParamaters );
// put in the rendered template in the measure-rep-container of the measure
$(this.repContainerEl).append( compiledTemplate );
this.setElement($('#measure-rep-'+this.measureRepModel.cid));
// for each beat in this measure
_.each(this.parentMeasureModel.get('beats').models, function(beat, index) {
measurePassingToBeatViewParamaters = {…};
};
new BeatView(measurePassingToBeatViewParamaters);
}, this);
return this;
},
transition: function(){
console.warn('getting in here'); //NEVER GET HERE
console.log(this.model.get('previousRepresentationType') + '|' + this.model.get('representationType'));
}
});
});
更改事件仅在使用model.set
进行更改时触发。不能只分配新属性。Backbone 不使用 defineProperty 样式,它是一种更明确的样式。
this.set({
previousRepresentationType: this.representationType,
representationType: newRep
});
相关文章:
- 角度指令没有更新模型视图
- “渲染骨干模型视图”返回未定义的结果
- 轮询更新主干模型/视图的请求
- 节点.js中的模型-视图-控制器模式
- 图像数据中的更改未反映在模型视图中
- 自定义 ngModel 指令以支持 jquery 插件中的模型>视图绑定
- 优化显示简单项目列表的模型/视图
- 建议:在JavaScript中难以使用模型视图控制器
- 将三维世界矢量转换为模型视图矩阵
- 在主干模型/视图上处理更复杂的验证逻辑(必填字段等)的最佳方式
- AngularJS在POST http请求上更新模型/视图
- web应用程序模型视图中的Java脚本
- 我的第一个骨干模型/视图.我的思路对吗?
- Knockoutjs的日期时间字段更新不会刷新模型视图
- Backbone.js中的嵌套集合/模型视图管理
- Javascript“this"模型视图演示器设计中的问题
- 模型视图控制器-javascript mvc框架设计实践,用于编辑就地界面
- 模型视图控制器-任何使用javascript mvc的人
- 模型视图控制器-我们是否在使用JavaScriptMVC(MVVM)框架,如Backbone.js、Angular等
- 模型视图控制器-AngularJS中是否可以在经典的javascript函数中使用数据绑定