0

有人对为什么在模型上执行 .set 时这个该死的视图不会更新有任何想法吗?我认为如果可能与我放入的渲染功能有关......

骨干视图:

    App.Views.PanelOne = Backbone.View.extend({


    initialize: function() {
        console.log("View PanelOne Initialized");

        panelonesetting = this.model.get('panel_one');
        console.log( panelonesetting );

        _.bindAll(this, 'render');
        this.model.bind('change', this.render);

    },

    render: function(){
        if ( panelonesetting == "poolprice" ){
            this.$el.html(JST['dashboard/poolprice']({}));
        return this;
        };
        if ( panelonesetting == "none" ){
            this.$el.html(JST['dashboard/none']({}));
        return this;
        };
        if ( panelonesetting == "noconnection" ){
            this.$el.html(JST['dashboard/noconnection']({}));
        return this;
        };
    }


});

骨干路由器:

App.Routers.Dashboard = Backbone.Router.extend({

    routes: {
        ""          : "index"       
        },

    initialize: function(options) {
    console.log("Router initialized");
      preflist = new App.Models.PrefList({});

    preflist.fetch({
        success: function() {
            console.log( preflist );
          paneloneview = new App.Views.PanelOne({ model: preflist });
          $('#panelone').html(paneloneview.render().$el);             
        }  
    });
  },

    index: function() {

    }


});
4

1 回答 1

1

你有一些意外的全局变量。特别是,您的视图中有这个initialize

panelonesetting = this.model.get('panel_one');

然后render看看panelonesetting。没有varpanelonesetting所以它是一个全局变量。

initialize方法在您构建视图时被调用,并且不再被调用;这意味着panelonesetting将被分配初始panel_one值,然后永远不会更新。因此,如果模型发生更改,您render将被调用,但它会查看原始panel_one设置。结果是似乎什么都没有改变:你可以render用你想要的任何模型更改来调用你想要的任意多次,而且你永远不会看到任何事情发生。

您应该检查方法panel_one内部的值render

initialize: function() {
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
},

render: function(){
    var panelonesetting = this.model.get('panel_one');
    if(panelonesetting == "poolprice")
        this.$el.html(JST['dashboard/poolprice']({}));
    if(panelonesetting == "none")
        this.$el.html(JST['dashboard/none']({}));
    if(panelonesetting == "noconnection")
        this.$el.html(JST['dashboard/noconnection']({}));
    return this;
}

当您preflist and的路由器中的 paneloneview 变量几乎肯定是本地的时,它们也是全局的。

于 2012-12-16T03:00:56.950 回答