【问题标题】:Binding to change of external model绑定到外部模型的变化
【发布时间】:2012-05-04 08:23:38
【问题描述】:

我有一个带有模型的 Backbone 视图。

此外,我还有一个全局模型,其中包含一些特定于应用程序的东西。

现在我将此模型的更改事件绑定到我的视图的渲染方法,但这似乎不起作用。

model: new Preferences.Item(),

render: function() {

    $(that.el).html(template(that.model.toJSON()));                 
},

initialize : function() {
        this.render = _.bind(this.render, this);
        // global account model holder
            App.Storage.account.bind("change", this.render);
},

我必须做一些特定的绑定来附加到外部模型的事件吗?

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    您应该使用 Backbone 的内联绑定来绑定 render 方法。另外,您在render 方法中使用了that,这将是一个错误。

    var ModelView = Backbone.View.extend({
        model: new Preferences.Item(),
        template: _.template('<div><%= variable %></div>');
        render: function () {
            this.$el.html(this.template(this.model.toJSON()))
        },
        initialize: function () {
            App.Storage.account.on('change', this.render, this);
        }
    });
    

    【讨论】:

      【解决方案2】:

      找到解决方案...您必须致电:

      App.Storage.account.on("change", this.render) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多