PDA

View Full Version : Sharing server constants on the JS side, and a/sync calls



Stifu
16 Sep 2013, 5:11 AM
Hello,

I'm using ExtJS MVC, and wanted to share my server-side constants with the client side to avoid code repetition, and making sure things stay in sync. I got something working, but I'm not 100% satisfied with my solution.

At first, I loaded the constants through an AJAX call, but my problem was that my constants could be called before they were done loading, due to the asynchronous nature of the loading. As a quick fix, I set the async property to false on the Ext.Ajax.request, and it works fine. That said, I know this is a bad practice, and as I understand it, the async property is undocumented and we shouldn't rely on it, as it may disappear in the future.

I tried using the callback of the constant loading to starting loading my application, like this:


Ext.Ajax.request({
url: 'Api/Constant/Get',
success: function (response) {
MyApp.consts = Ext.decode(response.responseText);
Ext.application({ /* app launch code, including all the controller / model require calls */ });
}
});


Although it looks to me like it should work, Sencha Cmd doesn't seem to support something like that, and includes scripts in the wrong order (it includes the content of my models first, which rely on the constants, before including the code above that loads the constants). I wanted to avoid having to add an extra script in my HTML page head to load the constants, as I wanted to keep everything in the Ext JS side, but I will end up doing that if I have to.

Is there a better soluton to this problem?

Thanks.

ettavolt
17 Sep 2013, 7:32 AM
Try wrapping you constants in something like Ext.define('App.Cfg',{singleton:true}); and require App.Cfg in your app's class.
To work with Cmd you should set a different path for Loader, because this file must not be included into build but rather required at load time.