2 * @class Ext.app.Application
3 * @extend Ext.app.Controller
5 * Represents an Ext JS 4 application, which is typically a single page app using a {@link Ext.container.Viewport Viewport}.
6 * A typical Ext.app.Application might look like this:
10 * launch: function() {
11 * Ext.create('Ext.container.Viewport', {
19 * This does several things. First it creates a global variable called 'MyApp' - all of your Application's classes (such
20 * as its Models, Views and Controllers) will reside under this single namespace, which drastically lowers the chances
21 * of colliding global variables.
23 * When the page is ready and all of your JavaScript has loaded, your Application's {@link #launch} function is called,
24 * at which time you can run the code that starts your app. Usually this consists of creating a Viewport, as we do in
27 * <u>Telling Application about the rest of the app</u>
29 * Because an Ext.app.Application represents an entire app, we should tell it about the other parts of the app - namely
30 * the Models, Views and Controllers that are bundled with the application. Let's say we have a blog management app; we
31 * might have Models and Controllers for Posts and Comments, and Views for listing, adding and editing Posts and Comments.
32 * Here's how we'd tell our Application about all these things:
36 * models: ['Post', 'Comment'],
37 * controllers: ['Posts', 'Comments'],
39 * launch: function() {
44 * Note that we didn't actually list the Views directly in the Application itself. This is because Views are managed by
45 * Controllers, so it makes sense to keep those dependencies there. The Application will load each of the specified
46 * Controllers using the pathing conventions laid out in the <a href="../guide/application_architecture">application
47 * architecture guide</a> - in this case expecting the controllers to reside in app/controller/Posts.js and
48 * app/controller/Comments.js. In turn, each Controller simply needs to list the Views it uses and they will be
49 * automatically loaded. Here's how our Posts controller like be defined:
51 * Ext.define('MyApp.controller.Posts', {
52 * extend: 'Ext.app.Controller',
53 * views: ['posts.List', 'posts.Edit'],
55 * //the rest of the Controller here
58 * Because we told our Application about our Models and Controllers, and our Controllers about their Views, Ext JS will
59 * automatically load all of our app files for us. This means we don't have to manually add script tags into our html
60 * files whenever we add a new class, but more importantly it enables us to create a minimized build of our entire
61 * application using the Ext JS 4 SDK Tools.
63 * For more information about writing Ext JS 4 applications, please see the <a href="../guide/application_architecture">
64 * application architecture guide</a>.
66 * @docauthor Ed Spencer
69 Ext.define('Ext.app.Application', {
70 extend: 'Ext.app.Controller',
75 'Ext.data.StoreManager',
76 'Ext.tip.QuickTipManager',
77 'Ext.ComponentManager',
82 * @cfg {String} name The name of your application. This will also be the namespace for your views, controllers
83 * models and stores. Don't use spaces or special characters in the name.
87 * @cfg {Object} scope The scope to execute the {@link #launch} function in. Defaults to the Application
93 * @cfg {Boolean} enableQuickTips True to automatically set up Ext.tip.QuickTip support (defaults to true)
95 enableQuickTips: true,
98 * @cfg {String} defaultUrl When the app is first loaded, this url will be redirected to. Defaults to undefined
102 * @cfg {String} appFolder The path to the directory which contains all application's classes.
103 * This path will be registered via {@link Ext.Loader#setPath} for the namespace specified in the {@link #name name} config.
109 * @cfg {Boolean} autoCreateViewport True to automatically load and instantiate AppName.view.Viewport
110 * before firing the launch function (defaults to false).
112 autoCreateViewport: false,
114 constructor: function(config) {
115 config = config || {};
116 Ext.apply(this, config);
118 var requires = config.requires || [];
120 Ext.Loader.setPath(this.name, this.appFolder);
123 Ext.Object.each(this.paths, function(key, value) {
124 Ext.Loader.setPath(key, value);
128 this.callParent(arguments);
130 this.eventbus = Ext.create('Ext.app.EventBus');
132 var controllers = Ext.Array.from(this.controllers),
133 ln = controllers && controllers.length,
136 this.controllers = Ext.create('Ext.util.MixedCollection');
138 if (this.autoCreateViewport) {
139 requires.push(this.getModuleClassName('Viewport', 'view'));
142 for (i = 0; i < ln; i++) {
143 requires.push(this.getModuleClassName(controllers[i], 'controller'));
146 Ext.require(requires);
148 Ext.onReady(function() {
149 for (i = 0; i < ln; i++) {
150 controller = this.getController(controllers[i]);
151 controller.init(this);
154 this.onBeforeLaunch.call(this);
158 control: function(selectors, listeners, controller) {
159 this.eventbus.control(selectors, listeners, controller);
163 * Called automatically when the page has completely loaded. This is an empty function that should be
164 * overridden by each application that needs to take action on page load
167 * @param {String} profile The detected {@link #profiles application profile}
168 * @return {Boolean} By default, the Application will dispatch to the configured startup controller and
169 * action immediately after running the launch function. Return false to prevent this behavior.
176 onBeforeLaunch: function() {
177 if (this.enableQuickTips) {
178 Ext.tip.QuickTipManager.init();
181 if (this.autoCreateViewport) {
182 this.getView('Viewport').create();
185 this.launch.call(this.scope || this);
186 this.launched = true;
187 this.fireEvent('launch', this);
189 this.controllers.each(function(controller) {
190 controller.onLaunch(this);
194 getModuleClassName: function(name, type) {
195 var namespace = Ext.Loader.getPrefix(name);
197 if (namespace.length > 0 && namespace !== name) {
201 return this.name + '.' + type + '.' + name;
204 getController: function(name) {
205 var controller = this.controllers.get(name);
208 controller = Ext.create(this.getModuleClassName(name, 'controller'), {
213 this.controllers.add(controller);
219 getStore: function(name) {
220 var store = Ext.StoreManager.get(name);
223 store = Ext.create(this.getModuleClassName(name, 'store'), {
231 getModel: function(model) {
232 model = this.getModuleClassName(model, 'model');
234 return Ext.ModelManager.getModel(model);
237 getView: function(view) {
238 view = this.getModuleClassName(view, 'view');
240 return Ext.ClassManager.get(view);