【问题标题】:Removing a Marionette EventAggregator: really necessary to prevent leaks?移除 Marionette EventAggregator:真的有必要防止泄漏吗?
【发布时间】:2013-10-23 03:11:45
【问题描述】:

我正在阅读Backbone.Wreqr 中的EventAggregator 文档。它可以像下面这样使用。

MyApp.vent.trigger("some:namespaced:event");

MyApp.vent.on("some:namespaced:event", function(){
  // ...
});

我不明白,一旦在视图中设置了一个处理程序,我是否需要删除它,或者 Marionette 会在 onClose 方法或其他地方为我做这件事?如果我需要删除它,我可以遵循的最佳方法是什么?是否只需要off 那个事件?使用removeHandlerCommands 是否同样适用?

你能指出我正确的方向吗?

【问题讨论】:

    标签: javascript backbone.js command marionette eventaggregator


    【解决方案1】:

    这取决于你的情况。 如果您不想监听某些事件,当模块不工作时,您可以订阅模块“停止”事件并执行所有必要的事件。

    module.on("stop", function(){
      App.vent.off(someMethods);
    });
    

    像往常一样,视图不使用全局事件聚合。 但是如果你使用,你应该使用

    this.listenTo( App.vent, "someEvent", function(){...} )
    

    然后 Marionette 删除此处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-02
      • 2010-10-03
      • 2010-09-21
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      相关资源
      最近更新 更多