Marionette Event Aggregator vs. Trunk Router

I am new to this world, so I need some clarification. Maybe I'm wrong about this. Therefore, feel free to correct me.

I learn how Marionette and Backbone work. Oh yeah. Marionette gives us an extension to the Highway. Really nice stuff.

What is not obvious to me is when to use the routing mechanism provided by Backbone, and when to use the Marionette publisher / subscriber pattern.

Is there any rule of thumb?

Here Where to use the event aggregator in the base puppet? A similar discussion, but no advice on using this or the other.

+1
source share
2 answers

My route management is explained in a free preview of my book Marionette ( http://samples.leanpub.com/marionette-gentle-introduction-sample.pdf )

Basically, my opinion (others do not necessarily share it) is that the routing of the backbone should be used to configure the state of the application when the user "enters" the application through the URL. In other words, it will analyze the parameters and trigger the correct controller actions.

But once this initial state is configured, the routing code should no longer run even when the user navigates through the application.

Here is an example:

  • The user enters "contacts / 2 / editing". The main routing code retrieves argument 2 and invokes the action of the edit controller with this id parameter (which retrieves this contact, displays the corresponding views, etc.). In other words, the state of the source application is configured.
  • The user clicks the "show all contacts" link leading to the URL of the contacts. Here, I believe that this modification should be handled through Marionette events (i.e. indicate that the user wants to see all contacts). In the end, we know what the user wants to do, and which fragment of the URL should be displayed. In other words, there is no reason to include a routing code.

Please note that this is my opinion, and other developers simply pass trigger: true when the user clicks the link. But, as I explain in the excerpt from the book above, this leads to the fact that developers create "stateless applications in javascript" (for example, they pass a lot of parameters to the URL, although they must be stored in the state of the application). For all reasons, there is a reason that the Backbone navigate method has trigger: false .

Derick Bailey (creator of Marionette) also discussed this issue here: http://lostechies.com/derickbailey/2011/08/03/stop-using-backbone-as-if-it-were-a-stateless-web-server/

+5
source

An event aggregator is more useful for notification. (think of small feedback bits)

  • Message from server (updated record)
  • Let other models know that things have changed.
  • Lock everything on save until save
  • Single moment in time.

The router is for things where you want the state to be economical (think of a separate page in the MPA)

  • Model Edit Page
  • Model View Page
  • Something that remains until another event or activity changes it

If you are not sure if something is an event or a page, think about it and ask a separate question.

0
source

Source: https://habr.com/ru/post/1481891/


All Articles