Is there a Sencha-recommended "best practise" location for global functions in an Architect project? I'm currently defining a global singleton object which I populate by loading a store before the main viewport loads, so that I can refer to App.global.user_guid (etc) throughout the application.
I also refer to a simple showMessage() function (among others) in a common.js file which is included via app.json. Is this the best place or way to refer to global functions? I know it pollutes the global namespace but I haven't had a problem to date. It's almost certainly not best practise, but I'm not 100% sure what is in an app created with Sencha Architect anyway.
How does everybody else deal with getting and using common information throughout their apps, and where do you store your common/utility functions? I have a little time up my sleeve and I'd like to tidy up my application template before I get stuck into the next project, and if I can do things better, then I'm open to suggestions. Thanks.
This is how I start my apps in essence:
Code:
// app.js:
Ext.application({
requires: [
'Ext.Loader'
],
models: [
'AppGlobal'
],
stores: [
'AppGlobal'
],
views: [
'MainViewport'
],
name: 'App',
launch: function() {
// Global object to access things throughout the application
Ext.define('App.global', {
singleton: true,
user_guid: null,
is_admin: false,
something: 'else'
});
initialiseAppGlobal(function() {
// Remove a simple HTML table in the index.html which has a spinning.gif in it
var element = document.getElementById('loadertable');
if (element) element.parentNode.removeChild(element);
if (App.global.user_guid) {
showMainViewport();
} else {
showMessage('Error', 'Your identity could not be determined', 'e');
}
});
}
});
// common.js (included via the "js" array in app.json)
function initialiseAppGlobal(callback) {
var store = Ext.create('App.store.AppGlobal', {
autoLoad: true,
listeners: {
'load': function (store, records, success, operation, eOpts) {
if (this.getTotalCount() === 0) {
showMessage('Critical Error', 'AppGlobal store did not return a record. The application is unable to load.', 'e');
} else {
var record = this.first();
if (record) {
App.global.user_guid = record.get('user_guid');
App.global.is_admin = record.get('is_admin');
App.global.something = record.get('something');
}
if (callback) callback();
}
}
}
});
}
function showMainViewport() {
var vp = Ext.create('App.view.MainViewport', {
renderTo: Ext.getBody()
});
vp.show();
}
function showMessage(title, message, icon) {
var i = Ext.Msg.INFO; // assume INFO by default
if (icon !== undefined) {
if (icon === 'w') i = Ext.Msg.WARNING;
if (icon === 'e') i = Ext.Msg.ERROR;
}
Ext.Msg.show({
title: title,
msg: message,
buttons: Ext.Msg.OK,
icon: i
});
}
One unfortunate downside to using my App.global singleton is that I can't refer to it in any ViewModel formulae; it causes Architect to throw up errors in the Log, but I can refer to them in the ViewModel data property before the corresponding component loads, though, and then use them via get('whatever').