本文介绍了有没有决定何时在Backbone.Marionette使用触发器或triggerMethod一个经验法则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打了一下与 Backbone.js的 Backbone.Marionette ,我想知道什么是之间的差异触发 triggerMethod

I'm playing a bit with Backbone.js and Backbone.Marionette and I would like to know what's the difference between trigger and triggerMethod.

在特定的,有没有使用前者还是后者,当决定拇指任何规则?

In particular, is there any rule of thumb to decide when use the former or the latter?

在我的脑海里的事件,例如,是一个DOM元素及其视图之间进行通信非常有用。

In my mind events, for example, are useful to communicate between a DOM element and its view.

triggerMethod 木偶中使用串联不同的组件,例如更新布局调用显示法的儿童(儿童昂秀回应)。所以,对我来说它与调用它的直接方法。这是真的吗?

triggerMethod is used in Marionette to update in cascade different components, e.g. a layout calls the show method to its children (children respond to onShow). So, for me its the same as calling a direct method on it. Is this true?

什么触发

感谢您提前。

推荐答案

有没有一个巨大的差异,这只是取决于你想要做什么......

There isn't a huge difference, and it simply depends on what you want to do...


  • 触发将触发一个事件

  • triggerMethod 将触发一个事件的的调用根据命名约定相应的方法(见https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.functions.md#marionettetriggermethod)

  • trigger will trigger an event
  • triggerMethod will trigger an event AND call a corresponding method according to naming convention (see https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.functions.md#marionettetriggermethod)

显然,如果你只想触发一个事件,你会使用触发。但是,使用触发你还可以创建一个自制triggerMethod实施触发事件,然后有一个监听器,将叫你想要的功能。

Obviously, if you only want to trigger an event, you'd use trigger. But using trigger you also create a "home made" triggerMethod implementation: trigger the event, then have a listener that will call the function you want.

那么,关于 triggerMethod ?如上所述,这将触发一个事件,并调用的方法。所以,如果你唯一的目标是要调用的方法摆在首位,但不一定需要使用 triggerMethod

So what about triggerMethod ? As mentioned above, it will trigger an event and call a method. So if your only objective is to call the method in the first place, there isn't necessarily a need for using triggerMethod.

那么为什么有人使用 triggerMethod 呢?因为它给你的钩子与事件侦听器添加功能。在木偶我在窗体上显示错误消息。同样可以通过简单地调用来实现。

So why would one use triggerMethod at all? Because it gives you "hooks" to add functionality with event listeners. In my book on Marionette, for example, there is a triggerMethod call in https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/edit/edit_controller.js#L24 to display error messages on a form. The same could be achieved by simply calling

view.onFormDataInvalid(contact.validationError);

但正如上面提到的, triggerMethod 给我们一个钩子以备后用。例如,假设我要添加用户错误的记录:我可以简单地将侦听器添加到我的观点:

But as mentioned above, triggerMethod give us a "hook" for later use. For example, let's say I want to add logging of user errors: I can simply add a listener to my view:

initialize: function(){
  this.on("form:data:invalid", function(validationError){
    // log error here
  }
}

可以在不影响code的剩余部分添加此功能产生额外的,因为我们使用的直接方法调用的 triggerMethod 来代替。此外,它会更容易在以后测试(小的考验,具有单点故障):

This additonal functionality can be added without impacting the rest of the code, because we've used triggerMethod instead of a direct method call. In addition, it will be easier to test later (small tests, with single point of failure):


  • 测试的形式:数据:无效的,当用户输入不正确的信息是触发事件

  • 测试时的形式:数据:无效触发的事件,错误会记录


这篇关于有没有决定何时在Backbone.Marionette使用触发器或triggerMethod一个经验法则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:14