绑定到外部模型的更改

Binding to change of external model

本文关键字:模型 外部 绑定      更新时间:2023-09-26

我有一个带有模型的骨干视图。

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

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

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

我是否必须执行一些特定的绑定以附加到外部模型的事件?

您应该使用 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);
    }
});

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

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