Skip to content

Commit

Permalink
Added experimental methods to load an MVC app via an environment, plu…
Browse files Browse the repository at this point in the history
…s the plugin builder script
  • Loading branch information
Ed Spencer committed Aug 19, 2009
1 parent 61ae23c commit 0f1df74
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 103 deletions.
54 changes: 30 additions & 24 deletions App.js
Expand Up @@ -21,34 +21,40 @@ ExtMVC.App = Ext.extend(Ext.util.Observable, {

this.initializeNamespaces();

Ext.onReady(function() {
if (this.fireEvent('before-launch', this)) {
this.initializeRouter();
// this.initializeViewport();
this.initializeEvents();
Ext.onReady(this.onReady, this);
},

/**
* @private
* Called when Ext.onReadt fires
*/
onReady: function() {
if (this.fireEvent('before-launch', this)) {
this.initializeRouter();
// this.initializeViewport();
this.initializeEvents();

if (this.usesHistory === true) this.initializeHistory();
if (this.usesHistory === true) this.initializeHistory();

this.launch();
this.fireEvent('launched', this);

/**
* TODO: This used to reside in initializeHistory but this.launch() needs to be
* called before this dispatches so it is temporarily here... ugly though
*/
if (this.usesHistory) {
if (this.dispatchHistoryOnLoad === true) {
Ext.History.init(function(history) {
var hash = document.location.hash.replace("#", "");
var params = this.router.recognise(hash);
if (params) {this.dispatch(params);}
}, this);
} else {
Ext.History.init();
}
this.launch();
this.fireEvent('launched', this);

/**
* TODO: This used to reside in initializeHistory but this.launch() needs to be
* called before this dispatches so it is temporarily here... ugly though
*/
if (this.usesHistory) {
if (this.dispatchHistoryOnLoad === true) {
Ext.History.init(function(history) {
var hash = document.location.hash.replace("#", "");
var params = this.router.recognise(hash);
if (params) {this.dispatch(params);}
}, this);
} else {
Ext.History.init();
}
}
}, this);
}
},

/**
Expand Down
145 changes: 145 additions & 0 deletions MVC.js
Expand Up @@ -46,6 +46,151 @@ 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.
* TODO: When it works, document this :)
*/
boot: function() {
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'
);
}
});
},

/**
* 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'],
files = [];

// Ext.each(order, function(fileList) {
// var dir = env[fileList + "Dir"] || String.format("../app/{0}", fileList);
//
// Ext.each(env[fileList], function(file) {
// files.push(String.format("{0}/{1}.js", dir, file));
// }, this);
// }, this);

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

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

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

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

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

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

var fragment = "";
for (var i=0; i < files.length; i++) {
fragment += String.format("<script type\"text/javascript\" src=\"{0}\"></script>", files[i]);
};

Ext.getBody().createChild(fragment);

// Ext.onReady(function() {
// console.log('hmm');
// console.log('test');
// console.log(this);
// this.app.onReady();
// }, this);
},


/**
* @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
13 changes: 12 additions & 1 deletion controller/CrudController.js
Expand Up @@ -154,12 +154,16 @@ ExtMVC.controller.CrudController = Ext.extend(ExtMVC.controller.Controller, {
* Renders the custom New view if present, otherwise falls back to the default scaffold New form
*/
build: function() {
return this.render('New', {
var buildView = this.render('New', {
model : this.model,
controller : this,
listeners : this.getBuildViewListeners(),
viewsPackage: this.viewsPackage
});

this.onBuild(buildView);

return buildView;
},

/**
Expand Down Expand Up @@ -326,6 +330,13 @@ ExtMVC.controller.CrudController = Ext.extend(ExtMVC.controller.Controller, {
this.fireEvent('delete-failed', instance);
},

/**
* Called whenever the 'New' form has been rendered for a given instance. This is an empty function by default,
* which you can override to provide your own logic if needed
* @param {Ext.Component} form The rendered 'New' form
*/
onBuild: function(form) {},

/**
* Called whenever the Edit form has been rendered for a given instance. This is an empty function by default,
* which you can override to provide your own logic if needed
Expand Down
2 changes: 1 addition & 1 deletion ext-mvc-all-min.js

Large diffs are not rendered by default.

0 comments on commit 0f1df74

Please sign in to comment.