gpt4 book ai didi

javascript - 刷新 Backbone 类名和属性

转载 作者:行者123 更新时间:2023-11-29 19:43:00 25 4
gpt4 key购买 nike

问题

如何在模型更改时重新渲染时更新 Backbone el?


我目前有一个事件监听器,像这样监听模型更改。

this.listenTo(this.model, 'change', this.render);

这将正确地重新呈现 View ,但是类名和属性不会更新以反射(reflect)模型更改,它们看起来像这样。

className: function()
{
var childClassName = ( null === this.model.get('parent_id') ? '' : ' list-item-child' ),
parentClassName = ( false == this.model.get('parent') ? '' : ' list-item-parent');

return 'list-item' + childClassName + parentClassName;
},

attributes: function()
{
return {
'data-id': this.model.get('id'),
'data-parent': this.model.get('parent_id')
}
},

最佳答案

你必须更新自己,例如:

onRender: function(){

this.$el.removeClass().addClass(this.className());

}

关于javascript - 刷新 Backbone 类名和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com