【问题标题】:Refresh Backbone ClassName & Attributes刷新主干类名和属性
【发布时间】:2014-02-18 23:47:32
【问题描述】:

问题

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


我目前有一个事件监听器,监听这样的模型变化。

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

这将正确地重新渲染视图,但是类名和属性不会更新以反映模型更改,它们看起来像这样。

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')
    }
},

【问题讨论】:

标签: javascript jquery backbone.js underscore.js


【解决方案1】:

你必须更新自己,例如:

onRender: function(){

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

}

【讨论】:

    【解决方案2】:

    className 是在视图创建时设置的,当您重新渲染视图时它不会被更新。

    如果您没有其他解决方案,也许这个解决方案可以帮助您:

    this.listenTo(this.model, 'change', function() {
        this._ensureElement();
        this.render();
        this.delegateEvents();
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多