5

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?

2 Answers 2

2

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.

1

Marionette apps have the initializer so that you can write code that runs after App.start()

Several uses of this :

  • Placing code in the .js file that will execute after the inline scripts are added

    <script type='text/javascript'>
    $(document).ready(function () { 
             App.start(); 
    } );
    </script>
    
  • Performing the initial fetch of a collection once you know the collection is defined

  • Setting up menu code

Fun Notes :

  1. If the app is already started they run immediately
  2. Keeps you clear of surprises with JavaScript function availability.
2
  • Your first example is a case where I do see how it could be helpful, where you are attaching the start() to a callback that you have no idea when it will be triggered. the other two examples not so much. Jan 8, 2014 at 4:25
  • like for example "Performing the initial fetch of a collection once you know the collection is defined" - assuming you mean you would fetch the collection in the initializer - you still have to know the collection is defined before the initializer runs, so you would still have to define the collection either before you run start(), or in a before:start callback. So at that point why dont you just make sure you define your collection before you fetch it, without all these extra callbacks? Either way you have to keep track of the order of things. Jan 8, 2014 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.