Backbone.View.remove 在 Marionette 申请中

Backbone.View.remove in a Marionette application

要销毁 Backbone 视图,您可以调用它的 remove 方法,但在 Marionette 应用程序中,视图提供了一个新的 destroy 方法来执行同样,但也负责删除子视图。这是否意味着 remove 方法不应该在 Marionette 视图上使用?

查看source,我们可以看到Marionette视图destroy()方法内部调用了Backboneremove()方法。调用 remove() 将删除 dom 元素并取消绑定事件,但是 Marionette destroy() 更强大并且做更多的管道,比如破坏行为等。所以在 Marionette 你应该只使用的应用程序 destroy().

  destroy: function() {
    if (this.isDestroyed) { return this; }

    var args = _.toArray(arguments);

    this.triggerMethod.apply(this, ['before:destroy'].concat(args));

    // mark as destroyed before doing the actual destroy, to
    // prevent infinite loops within "destroy" event handlers
    // that are trying to destroy other views
    this.isDestroyed = true;
    this.triggerMethod.apply(this, ['destroy'].concat(args));

    // unbind UI elements
    this.unbindUIElements();

    this.isRendered = false;

    // remove the view from the DOM
    this.remove();

    // Call destroy on each behavior after
    // destroying the view.
    // This unbinds event listeners
    // that behaviors have registered for.
    _.invoke(this._behaviors, 'destroy', args);

    return this;
  },