0

所以我在我的主干应用程序中遇到了同样著名的僵尸视图问题。我试过这个成为超级英雄:P

var Router=Backbone.Router.extend({
             routes:{
                  "":"loadDashboard",
                  "home":"loadDashboard",
                  'post-leads':"loadPostLeads"
                },
                initialize:function(){
                window.currentView=null;
                },

            loadPostLeads:function(){
            require(['views/post-leads'],function(leads){
            if(window.currentView!=null)
            {window.currentView.remove();}
            window.currentView=new leads();
            window.currentView.render();
            })

        },

              loadDashboard: function(){
        require(['views/dashboard'],function(dashboard){
            if(window.currentView!=null)
            {window.currentView.remove();}
            window.currentView=new dashboard();
            window.currentView.render();
            })
        }
          });

这行不通。我想要一些简单的东西,不想为此使用木偶或类似的东西。上面怎么了?这是一个明智的做法吗?

4

1 回答 1

3

原则上你应该做什么,但有些事情 Backbone 无法清理,因为它不知道它们。

首先,您应该确保您使用的是最新版本的 Backbone(0.9.9或更新版本)。对事件绑定代码进行了一些改进,这使得该View.remove方法更容易进行所有必要的清理。

常见的陷阱是:

监听模型事件

//don't use other.on (Backbone doesn't know how to clean up)
this.model.on('event', this.method);

//use this.listenTo (Backbone cleans up events when View.remove is called)
//requires Backbone 0.9.9
this.listenTo(this.model, 'event', this.method);

监听视图范围之外的 DOM 事件

//if you listen to events for nodes that are outside View.el
$(document).on('event', this.method);

//you have to clean them up. A good way is to override the View.remove method
remove: function() {
  $(document).off('event', this.method);
  Backbone.View.prototype.remove.call(this);
}

直接参考

//you may hold a direct reference to the view:
this.childView = otherView;

//or one of its methods
this.callback = otherView.render;

//or as a captured function scope variable:
this.on('event', function() {
  otherView.render();
});

闭包

//if you create a closure over your view, or any method of your view, 
//someone else may still hold a reference to your view:
method: function(arg) {
  var self = this;
  return function() {
    self.something(x);
  }
}

避免以下陷阱应该可以帮助您正确清理视图。

根据评论编辑

啊,你没有在你的问题中提到完整的问题。据我所知,您的方法的问题在于您试图将两个视图呈现到同一个元素中:

var View1 = Backbone.View.extend({el:"#container" });
var View2 = Backbone.View.extend({el:"#container" });

并且当您删除时View1View2不会正确呈现。

el您应该将视图呈现元素,而不是指定视图。在您的页面上,您应该有一个#container元素,并将视图的元素附加到容器中。

loadPostLeads: function () {
  var self = this;
  require(['views/post-leads'], function (leads) {
    self.renderView(new leads());
  })
},

loadDashboard: function () {
  var self = this;
  require(['views/dashboard'], function (dashboard) {
    self.renderView(new dashboard());
  })
},

renderView: function(view) {
  if(window.currentView) {
    window.currentView.remove();
  }

  //the view itself does not specify el, so you need to append the view into the DOM
  view.render();
  $("#container").html(view.el);

  window.currentView = view;
}
于 2013-03-14T08:07:57.857 回答