1
votes

I'm pretty new to this world so I need some clarifications on it. Maybe I could be wrong on the subject. So, feel free to correct me.

I'm studying how Marionette and Backbone work together. Oh yeah. Marionette gives us extension to Backbone. Really nice stuff.

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

Is there any rule of thumb?

Here, Where to use event aggregator in backbone marionette?, a similar discussion but there is no advice on how using that or the other.

2

2 Answers

5
votes

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

Basically, my opinion (others don't necessarily share it) is that Backbone's routing should be used to configure the application state when the user "enters" the application via a URL. In other words, it will parse parameters, and call proper controller actions.

But once that initial state is setup, routing code should no longer be triggered, even as the user navigates through the application.

Here's an example:

  1. The user enters arrives on the "contacts/2/edit". Backbone routing code will extract the 2 argument and call the edit controller action with that id parameter (which fetches that contact, displays the proper views, etc.). In other words, the initial application state is being configured.
  2. The user clicks on the "show all contacts" link leading to the "contacts" URL. Here, I believe this modification should be handled through Marionette events (i.e. indicating the user wants to see all contacts). After all, we know what the user wants to do, and which URL fragment should be displayed. In other words, there is no reason for the routing code to get involved.

Note that this is my opinion, and other developers simply pass trigger: true when the user clicks a link. But as I explain in the book extract linked above, this tends to lead developers to create "stateless applications in javascript" (e.g. passing lots of parameters in the URL, even though they should be stored in the application's state). Ater all there is a reason that by default, Backbone's navigate method has trigger: false.

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

0
votes

Event aggregator is more useful for notifying things. (think small bits of feedback)

  • Message from server (updated record)
  • Let other models know things have changed
  • Lock everything down while saving until saved
  • Single Moment in time things

Router is for things where you want the state to be save-able (think separate page in a MPA)

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

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