视图和模型对象的Backbone.js的处置 [英] Disposing of view and model objects in Backbone.js

查看:84
本文介绍了视图和模型对象的Backbone.js的处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是处置模型/视图实例不需要时最有效的方法是什么?

Which is the most efficient way to dispose model/view instances when not needed?

通常情况下,我把所有的逻辑控制器/路由器。这是一个决定,有什么意见应该被创建,什么型号应提供给他们。通常情况下,有几个处理函数,分别对应不同的用户动作或路线,我在那里创建新的视图实例每次当一个处理被执行的时间。当然,这应消除一切我已经pviously存储在视图实例$ P $。然而,也有一些情况下,当一些看法保持DOM事件处理程序自己,他们没有得到正确unbinded,这会导致这些情况下被保留活着。我想,如果有破坏视图实例以适当方式,例如当他们EL(DOM重新presentation)被分离,或抛出的DOM

Usually, I put all the logic in the controller/router. It is the one that decides, what views should be created, and what models should be supplied to them. Usually, there are a few handler functions, corresponding to different user actions or routes, where I create new view instances every time when a handler gets executed. Of course, that should eliminate whatever I've previously stored in the view instance. However, there are some situations when some views keep DOM event handlers themselves, and they don't get unbinded properly, which results in those instances being kept alive. I wish if there were a proper way to destroy view instances, when for example their el (DOM representation) gets detached, or thrown out of the DOM

推荐答案

你在正确的道路上。你应该有一个控制你的视图生命周期的对象。我不喜欢把它放在我的看法。我喜欢创造这个单独的对象。

you're on the right path. you should have an object that controls the lifecycle of your views. i don't like to put this in my view. i like to create a separate object for this.

您需要做的事情,是在必要时解除绑定的事件。要做到这一点,这是建立在所有的意见亲密的方法,并用它控制一切的生命周期的对象总是调用close方法是个好主意。

the thing you need to do, is unbind the events when necessary. to do this, it's a good idea to create a "close" method on all of your views, and use the object that controls the lifecycle of everything to always call the close method.

例如:


  function AppController(){
    this.showView = function (view){
      if (this.currentView){
        this.currentView.close();
      }
      this.currentView = view;
      this.currentView.render();
      $("#someElement").html(this.currentView.el);
    }
  }

在这一点上,你可以设置你的code只有具备的AppController的一个实例,你总是叫 appController.showView(...)从您的路由器或其他任何code,需要在显示屏幕的 #someElement 部分的视图。

at this point, you would set up your code to only have one instance of the AppController, and you would always call appController.showView(...) from your router or any other code that needs to show a view in the #someElement portion of your screen.

(我有一个使用APPVIEW(骨干运行应用程序的主要部分视图一个非常简单的应用骨干的另一个例子),此处的http://jsfiddle.net/derickbailey/dHrXv/9/

(i have another example of a very simple backbone app that uses an "AppView" (a backbone view that runs the main portion of the app), here: http://jsfiddle.net/derickbailey/dHrXv/9/ )

关闭方法默认情况下不上的看法存在,所以你需要自己创建一个,为您的每个意见。有两件事情应该始终处于关闭方法: this.unbind() this.remove() 。除了这些,如果你绑定您的视图以任何模型或收集事件,你应该解除他们在close方法。

the close method does not exist on views by default, so you need to create one yourself, for each of your views. There are two things that should always be in the close method: this.unbind() and this.remove(). in addition to these, if you are binding your view to any model or collection events, you should unbind them in the close method.

例如:


  MyView = Backbone.View.extend({
    initialize: function(){
      this.model.bind("change", this.modelChanged, this);
    },

    modelChanged: function(){
      // ... do stuff here
    },

    close: function(){
      this.remove();
      this.unbind();
      this.model.unbind("change", this.modelChanged);
    }
  });

这将正确地清理所有从DOM事件(通过 this.remove()),所有的观点本身可能引起的事件(通过 this.unbind())和事件(通过 this.model.unbind(...)从模型绑定的视图)。

this will properly clean up all of the events from the DOM (via this.remove()), all of the events that the view itself may raise (via this.unbind()) and the event that the view bound from the model (via this.model.unbind(...)).

这篇关于视图和模型对象的Backbone.js的处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆