javascript - Marionette View 的正确 View 生命周期是什么?

标签 javascript jquery backbone.js marionette eventtrigger

我目前使用的是 Marionette 2.4.1。

我有一个父布局 View (A),其中包含子布局 View (B) 将显示的区域。我在 View (B) 上有一个 onShow 方法,当显示 View (B) 时会正确调用该方法。但是,我在 View (B) 上有一个 onEmptyonBeforeEmpty ,希望它们在 View (B) 隐藏时被调用。这些是从 View (A) 调用的,其中我有一个函数可以清空 View (B) 填充的区域,但是我在空调用上传递 {preventDestroy: true}

我的问题是,我曾经/现在期望 View (B) 能够接收到 #empty 时调用的 before:emptyempty 触发器 是从 View (A) 调用的,但是 View (B) 不会获取这些触发器。甚至 View (B) 似乎也没有注意到这些触发器。

这里的预期情况是什么?运行 Marionette 源代码时,我可以看到 onBeforeEmpty 触发器根据我的源代码被调用:

Marionette._triggerMethod = (function() {
    // split the event name on the ":"
    var splitter = /(^|:)(\w)/gi;

    // take the event section ("section1:section2:section3")
    // and turn it in to uppercase name
    function getEventName(match, prefix, eventName) {
      return eventName.toUpperCase();
    }

    return function(context, event, args) {
      var noEventArg = arguments.length < 3;
      if (noEventArg) {
        args = event;
        event = args[0];
      }

      // get the method name from the event name
      var methodName = 'on' + event.replace(splitter, getEventName);
      var method = context[methodName];
      var result;

      // call the onMethodName if it exists
      if (_.isFunction(method)) {
        // pass all args, except the event name
        result = method.apply(context, noEventArg ? _.rest(args) : args);
      }

      // trigger the event, if a trigger method exists
      if (_.isFunction(context.trigger)) {
        if (noEventArg + args.length > 1) {
          context.trigger.apply(context, noEventArg ? args : [event].concat(_.drop(args, 0)));
        } else {
          context.trigger(event);
        }
      }

      return result;
    };
  })();

这是我怀疑它触发方法onBeforeEmpty的地方:

// get the method name from the event name
      var methodName = 'on' + event.replace(splitter, getEventName);
      var method = context[methodName];

看起来上下文是 View (A)。

Marionette 的区域#空:

// Destroy the current view, if there is one. If there is no
    // current view, it does nothing and returns immediately.
    empty: function(options) {
      var view = this.currentView;

      var preventDestroy = Marionette._getValue(options, 'preventDestroy', this);
      // If there is no view in the region
      // we should not remove anything
      if (!view) { return; }

      view.off('destroy', this.empty, this);
      this.triggerMethod('before:empty', view);
      if (!preventDestroy) {
        this._destroyView();
      }
      this.triggerMethod('empty', view);

      // Remove region pointer to the currentView
      delete this.currentView;

      if (preventDestroy) {
        this.$el.contents().detach();
      }

      return this;
    },

所以这里 this.triggerMethod('before:empty', view); 看起来 this 指的是 View (A) 和 view 指 View (B)。这是故意的吗?我认为该方法会在 View (B)上触发?但是,我似乎也看不到 View (A) 上触发的方法。

最佳答案

这个thisthis.triggerMethod('before:empty', view);指的是您的布局区域。您可以在此处查看更多区域生命周期事件/方法:http://marionettejs.com/docs/v2.4.3/marionette.region.html#events-raised-on-the-region-during-show

如果您正在寻找在您的 View 中使用的方法,onDestroyonBeforeDestroy可能是您正在寻找的。 View 被“破坏”,区域被“清空”。 http://marionettejs.com/docs/v2.4.3/marionette.view.html#view-onbeforedestroy

关于javascript - Marionette View 的正确 View 生命周期是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603475/

相关文章:

javascript - history.start() 函数在 backbone 中不起作用

javascript - 过渡不适用于元素

javascript - Node.js 从循环中查找集合返回数据

javascript - 如何在全局对象上使用绑定(bind)和事件

jquery - MVC 4 客户端验证不适用于使用 Ajax 加载的表单

jquery - 通过单击 div 附加事件

backbone.js - 主干转到另一个页面

javascript - 有没有人找到一种愉快地处理 WYSIHTML5 代码的方法?

javascript - 想要在选择选项中附加 Ajax 响应数据

events - Backbone.js 查看 img onload