最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a rule of thumb to decide when to use trigger or triggerMethod in Backbone.Marionette? - Stack Overflow

programmeradmin0浏览0评论

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?

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

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?

What about trigger?

Thanks you in advance.

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?

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

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?

What about trigger?

Thanks you in advance.

Share Improve this question edited Jan 11, 2014 at 11:57 Lorenzo B asked Jan 10, 2014 at 18:45 Lorenzo BLorenzo B 33.4k24 gold badges118 silver badges195 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

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

  • trigger will trigger an event
  • triggerMethod will trigger an event AND call a corresponding method according to naming convention (see https://marionettejs.com/docs/v2.1.0/marionette.functions.html#marionettetriggermethod)

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.

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.

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);

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
  }
}

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):

  • test that "form:data:invalid" event is triggered when a user enters incorrect info
  • test that when "form:data:invalid" event is triggered, error gets logged
  • etc.

trigger(name)

  • Part of Backbone.js
  • Triggers event using the name passed in

triggerMethod(name)

  • Part of Marionnete.js

  • Does everything trigger(name) does

  • Also calls methods using a predefined naming convention.

    eg. triggerMethod('foo') will call onFoo()

    eg. triggerMethod('foo:bar') will call onFooBar()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论