Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of marionette App initializers?

Tags:

marionette

I dont really understand how they are useful. In the original article that introduced initializers this was the code sample:

App = new Backbone.Marionette.Application();

App.addInitializer(function(){
  // add some app initialization code, here
});

App.addInitializer(function(){
  // more initialization stuff 
  // for a different part of the app
});

// run all the initializers and start the app
App.start();

however, as far as I can understand, there is no difference between that^, and this:

App = new Backbone.Marionette.Application();

// add some app initialization code, here

// more initialization stuff 
// for a different part of the app

The benefit of the latter code being that you can actually control the order of initialization code, whereas initializers are run in random order. So, what is the advantage of addInitializer?

like image 875
Owen Masback Avatar asked Oct 21 '22 17:10

Owen Masback


1 Answers

I think the main wins are semantics - it's a very descriptive method name - and grouping of related functionality. I write my initializers with a named function, which helps with debugging and descriptiveness:

App.addInitializer(function startSomePartOfTheApp () {

});

Another useful feature is that the function is bound to the Application instance. This gives you the option of mixing in initializers, which is useful in larger apps.

But ultimately, you can achieve the same functionality in the way you've suggested.

like image 124
Tom Avatar answered Jan 02 '23 19:01

Tom