Skip to content

Commit

Permalink
Added Booter and Environment classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Spencer committed Aug 20, 2009
1 parent 6544112 commit 7293d94
Show file tree
Hide file tree
Showing 8 changed files with 744 additions and 460 deletions.
2 changes: 1 addition & 1 deletion App.js
Expand Up @@ -26,7 +26,7 @@ ExtMVC.App = Ext.extend(Ext.util.Observable, {

/**
* @private
* Called when Ext.onReadt fires
* Called when Ext.onReady fires
*/
onReady: function() {
if (this.fireEvent('before-launch', this)) {
Expand Down
228 changes: 0 additions & 228 deletions MVC.js
Expand Up @@ -46,234 +46,6 @@ ExtMVC = Ext.extend(Ext.util.Observable, {
this.name = this.app.name;
},

/**
* @property bootParams
* @type Object
* An object which contains all boot parameters. These are used during the boot phase,
* and can be set using GET params after the '?' in the url
*/
bootParams: {
environment: 'production'
},

/**
* @property globalEnvironmentSettings
* @type Object
* All default environment settings that will be Ext.applyIf'd to the current environment.
* These are things that don't tend to change between applications, but you can override them if you need to
*/
globalEnvironmentSettings: {
pluginsDir : '../vendor/plugins',
libDir : '../lib',
configDir : '../config',
overridesDir: '../config/overrides',
appDir : '../app',
vendor : ['mvc'],
mvcFilename : 'ext-mvc-all-min',
config : ['initialize', 'database', 'routes']
},

/**
* Boots up the application.
* @param {Object} config Optional config object.
*/
boot: function(config) {
Ext.apply(this.bootParams, config || {}, {
callback: Ext.emptyFn
});

var args = window.location.href.split("?")[1];

/**
* Read config data from url parameters
*/
if (args != undefined) {
Ext.each(args.split("&"), function(arg) {
var splits = arg.split("="),
key = splits[0],
value = splits[1];

this.bootParams[key] = value;
}, this);
}

//load up the environment
Ext.Ajax.request({
url: '../config/environment.json',
scope : this,
success: function(response, options) {
var envName = this.bootParams.environment;

this.addEnvironmentSettings(envName, this.globalEnvironmentSettings);
this.addSettingsFromEnvironmentFile(response);
this.setCurrentEnvironment(envName);

Ext.Ajax.request({
url : String.format("../config/environments/{0}.json", envName),
success: function(response, options) {
this.addSettingsFromEnvironmentFile(response);

this.onEnvironmentLoaded(this.getCurrentEnvironmentSettings());
},
scope : this
});
},
failure: function() {
Ext.Msg.alert(
'Could not load environment',
'The ' + config.environment + ' environment could not be found'
);
}
});
},

/**
* Creates and returns a script tag, but does not place it into the document. If a callback function
* is passed, this is called when the script has been loaded
* @param {String} filename The name of the file to create a script tag for
* @param {Function} callback Optional callback, which is called when the script has been loaded
* @return {Element} The new script ta
*/
buildScriptTag: function(filename, callback) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = filename;

//IE has a different way of handling <script> loads, so we need to check for it here
if (script.readyState) {
script.onreadystatechange = function(){
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = callback;
}

return script;
},

/**
* Loads a given set of application .js files. Calls the callback function when all files have been loaded
* Set preserveOrder to true to ensure non-parallel loading of files, if load ordering is important
* @param {Array} fileList Array of all files to load
* @param {Boolean} preserveOrder True to make files load in serial, one after the other (defaults to false)
* @param {Function} callback Callback to call after all files have been loaded
* @param {Object} scope The scope to call the callback in
*/
loadFiles: function(fileList, preserveOrder, callback, scope) {
var scope = scope || this,
head = document.getElementsByTagName("head")[0],
fragment = document.createDocumentFragment(),
numFiles = fileList.length,
loadedFiles = 0;

/**
* Loads a particular file from the fileList by index. This is used when preserving order
*/
var loadFileIndex = function(index) {
head.appendChild(
ExtMVC.buildScriptTag(fileList[index], onFileLoaded, scope)
);
};

/**
* Callback function which is called after each file has been loaded. This calls the callback
* passed to loadFiles once the final file in the fileList has been loaded
*/
var onFileLoaded = function() {
loadedFiles ++;

//if this was the last file, call the callback, otherwise load the next file
if (numFiles == loadedFiles && Ext.isFunction(callback)) {
callback.call(scope);
} else {
if (preserveOrder === true) loadFileIndex(loadedFiles);
}
};

if (preserveOrder === true) {
loadFileIndex(0);
} else {
//load each file (most browsers will do this in parallel)
Ext.each(fileList, function(file, index) {
fragment.appendChild(
ExtMVC.buildScriptTag(file, onFileLoaded)
);
}, this);

head.appendChild(fragment);
}
},

/**
* Called when the environment files have been loaded and application load can begin
* @param {Object} environment The current environment object
*/
onEnvironmentLoaded: function(env) {
var order = ['overrides', 'config', 'plugins', 'models', 'controllers', 'views'],
baseFiles = [],
pluginFiles = [],
modelFiles = [],
controllerFiles = [],
viewFiles = [];

// var groups = {
// 'base': {preserveOrder: false, }
// };

Ext.each(env.overrides, function(file) {
baseFiles.push(String.format("{0}/{1}.js", env.overridesDir, file));
}, this);

Ext.each(env.config, function(file) {
baseFiles.push(String.format("{0}/{1}.js", env.configDir, file));
}, this);

Ext.each(env.plugins, function(file) {
pluginFiles.push(String.format("{0}/{1}/{2}-all.js", env.pluginsDir, file, file));
}, this);

Ext.each(env.models, function(file) {
modelFiles.push(String.format("{0}/models/{1}.js", env.appDir, file));
}, this);

Ext.each(env.controllers, function(file) {
controllerFiles.push(String.format("{0}/controllers/{1}Controller.js", env.appDir, file));
}, this);

Ext.iterate(env.views, function(dir, fileList) {
Ext.each(fileList, function(file) {
viewFiles.push(String.format("{0}/views/{1}/{2}.js", env.appDir, dir, file));
}, this);
}, this);

this.loadFiles(baseFiles, false, function() {
this.loadFiles(pluginFiles, false, function() {
this.loadFiles(modelFiles, false, function() {
this.loadFiles(controllerFiles, true, function() {
this.loadFiles(viewFiles, true, function() {
ExtMVC.app.onReady();

this.bootParams.callback();
});
});
});
});
});
},


/**
* @private
* Takes the response of an AJAX request, encodes it into a JSON object and adds to the current environment
*/
addSettingsFromEnvironmentFile: function(response) {
var envJSON = Ext.decode(response.responseText);
this.addEnvironmentSettings(this.bootParams.environment, envJSON);
},

/**
* @property controllers
* When this.registerController('application', MyApp.ApplicationController) is called,
Expand Down
2 changes: 1 addition & 1 deletion ext-mvc-all-min.js

Large diffs are not rendered by default.

0 comments on commit 7293d94

Please sign in to comment.