基本上我需要做的是这样的事情
App.CommentView = Backbone.View.extend({
className: function() {
if (this.model.get('parent_id')) {
return 'comment comment-reply';
} else {
return 'comment';
}
},
问题是,在函数传递给className
是在视图模板的html上下文中执行的,所以我无法调用this.model
.
有什么方法可以在渲染过程中访问模型吗?或者我是否需要稍后设置课程,例如在render
功能?
这听起来像是模型绑定的工作。
App.CommentView = Backbone.View.extend({
initialize: function () {
// anytime the model's name attribute changes
this.listenTo(this.model, 'change:name', function (name) {
if (name === 'hi') {
this.$el.addClass('hi');
} else if......
});
},
render: function () {
// do initial dynamic class set here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)