0

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

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

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

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

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

4

2 回答 2

1

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

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);
    }
});
于 2012-05-04T12:40:55.783 回答
0

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

App.Storage.account.on("change", this.render) 
于 2012-05-04T08:35:54.917 回答