I am trying to build a basic application using a puppet plugin, and I am having problems initializing to work the way I expected. I have the following code:
var MyApp = new Backbone.Marionette.Application(); MyApp.addRegions({ region1 : '#div1', region2 : '#div2' }); MyApp.Resources = { }; MyApp.bind('initialize:before', function (options) { // display a modal dialog for app initialization options.initMessageId = noty({ text : 'Initializing MyApp (this should only take a second or two)', layout : 'center', speed : 1, timeout : false, modal : true, closeOnSelfClick : false }); }); MyApp.addInitializer(function (options) { $.ajax({ url: options.apiUrl + '/my-app-api-module', type: 'GET', contentType: 'application/json; charset=utf-8', success: function (results) { MyApp.Resources.urls = results; console.log(MyApp.Resources.urls); // <- THIS returns an object } }); }); MyApp.bind('initialize:after', function (options) { // initialization is done...close the modal dialog if (options.initMessageId) { $.noty.close(options.initMessageId); } if (Backbone.history) { Backbone.history.start(); } console.log(MyApp.Resources.urls); // <- THIS returns 'undefined' BEFORE the console.log in the initializer above });
Note that in the above code, I have two calls to console.log
, one in the initializer and one in the initialize:after
handler. Both register the same property of the object. As you can see, what I'm experiencing is that the console.log
call in the initialize:after
handler is called before it is executed in the success
handler of the initializer. I understand that this is because the initializer has an asynchronous call in it ... I need to know how I can make sure all the async code in my initializer is complete before doing anything else in the application? Is there a good sample for this? I did not find anything in the docs indicating how to handle this correctly.
Thanks.
source share