绑定到外部模型的变化

时间:2012-05-04 08:23:38

标签: javascript backbone.js

我有一个带模型的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);
},

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

App.Storage.account.on("change", this.render) 
相关问题