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
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With