我想,当一个模型改变为一个类添加到ItemView控件的外围标签如下
View.Option = Marionette.ItemView.extend({
tagName: "li",
className: "option-item clearfix",
template: optionsItemTpl,
modelEvents: {
"change": "modelChanged"
},
modelChanged: function() {
console.log(this.$el);
this.$el.addClass('success');
}
});
后续输出元素我试图将该类添加到
console.log(this.$el);
但该类没有添加,我只是不明白为什么这会是。
我复制你的代码的jsfiddle。 好像它如预期运行。 的console.log(这一点。$ EL)实际上并不会告诉你,起初该值已经改变,因为它输出[li.option项],但你可以看到它并不显示其clearfix都不是。 如果您在更深的挖掘到$ EL对象,你会看到成功类实际上追加。 您也可以通过回this.el这是没有jQuery的包装纯DOM元素验证这一点。
检查出的jsfiddle稍微修改代码
和代码itsself:
HTML:
<div id="conatainer"> </div>
JavaScript的:
var View = {};
View.Option = Marionette.ItemView.extend({
tagName: "li",
className: "option-item clearfix",
modelEvents: {
"change": "modelChanged"
},
modelChanged: function() {
console.log("Model Changed..");
this.$el.addClass('success');
$("#conatainer").append(this.$el);
console.log("Updated Views $el:")
console.log(this.$el);
console.log("Updated Views el:")
console.log(this.el);
}
});
var model = Backbone.Model.extend();
var model_instance = new model({test: 1});
console.log("Model Instance:");
console.log(model_instance)
var view = new View.Option({model: model_instance});
console.log("View: ")
console.log(view);
model_instance.set({test: 2});
输出:
Model Instance:
Object {cid="c1", attributes=Object, _changing=false, ...}
View:
Object {options=Object, cid="view2", model=Object, ...}
Model Changed..
Updated Views $el:
[li.option-item]
Updated Views el:
<li class="option-item clearfix success">