Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / app / Application.js
1 /**
2  * @class Ext.app.Application
3  * @extend Ext.app.Controller
4  * 
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:
7  * 
8  *     Ext.application({
9  *         name: 'MyApp',
10  *         launch: function() {
11  *             Ext.create('Ext.container.Viewport', {
12  *                 items: {
13  *                     html: 'My App'
14  *                 }
15  *             });
16  *         }
17  *     });
18  * 
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.
22  * 
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
25  * the example above.
26  * 
27  * <u>Telling Application about the rest of the app</u>
28  * 
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:
33  * 
34  *     Ext.application({
35  *         name: 'Blog',
36  *         models: ['Post', 'Comment'],
37  *         controllers: ['Posts', 'Comments'],
38  *     
39  *         launch: function() {
40  *             ...
41  *         }
42  *     });
43  * 
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:
50  * 
51  *     Ext.define('MyApp.controller.Posts', {
52  *         extend: 'Ext.app.Controller',
53  *         views: ['posts.List', 'posts.Edit'],
54  *     
55  *         //the rest of the Controller here
56  *     });
57  * 
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.
62  * 
63  * For more information about writing Ext JS 4 applications, please see the <a href="../guide/application_architecture">
64  * application architecture guide</a>.
65  * 
66  * @docauthor Ed Spencer
67  * @constructor
68  */
69 Ext.define('Ext.app.Application', {
70     extend: 'Ext.app.Controller',
71
72     requires: [
73         'Ext.ModelManager',
74         'Ext.data.Model',
75         'Ext.data.StoreManager',
76         'Ext.tip.QuickTipManager',
77         'Ext.ComponentManager',
78         'Ext.app.EventBus'
79     ],
80
81     /**
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.
84      */
85
86     /**
87      * @cfg {Object} scope The scope to execute the {@link #launch} function in. Defaults to the Application
88      * instance.
89      */
90     scope: undefined,
91
92     /**
93      * @cfg {Boolean} enableQuickTips True to automatically set up Ext.tip.QuickTip support (defaults to true)
94      */
95     enableQuickTips: true,
96
97     /**
98      * @cfg {String} defaultUrl When the app is first loaded, this url will be redirected to. Defaults to undefined
99      */
100
101     /**
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.
104      * Defaults to 'app'
105      */
106     appFolder: 'app',
107
108     /**
109      * @cfg {Boolean} autoCreateViewport True to automatically load and instantiate AppName.view.Viewport
110      * before firing the launch function (defaults to false).
111      */
112     autoCreateViewport: false,
113
114     constructor: function(config) {
115         config = config || {};
116         Ext.apply(this, config);
117
118         var requires = config.requires || [];
119
120         Ext.Loader.setPath(this.name, this.appFolder);
121
122         if (this.paths) {
123             Ext.Object.each(this.paths, function(key, value) {
124                 Ext.Loader.setPath(key, value);
125             });
126         }
127
128         this.callParent(arguments);
129
130         this.eventbus = Ext.create('Ext.app.EventBus');
131
132         var controllers = Ext.Array.from(this.controllers),
133             ln = controllers && controllers.length,
134             i, controller;
135
136         this.controllers = Ext.create('Ext.util.MixedCollection');
137
138         if (this.autoCreateViewport) {
139             requires.push(this.getModuleClassName('Viewport', 'view'));
140         }
141
142         for (i = 0; i < ln; i++) {
143             requires.push(this.getModuleClassName(controllers[i], 'controller'));
144         }
145
146         Ext.require(requires);
147
148         Ext.onReady(function() {
149             for (i = 0; i < ln; i++) {
150                 controller = this.getController(controllers[i]);
151                 controller.init(this);
152             }
153
154             this.onBeforeLaunch.call(this);
155         }, this);
156     },
157
158     control: function(selectors, listeners, controller) {
159         this.eventbus.control(selectors, listeners, controller);
160     },
161
162     /**
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
165      * @property launch
166      * @type Function
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.
170      */
171     launch: Ext.emptyFn,
172
173     /**
174      * @private
175      */
176     onBeforeLaunch: function() {
177         if (this.enableQuickTips) {
178             Ext.tip.QuickTipManager.init();
179         }
180
181         if (this.autoCreateViewport) {
182             this.getView('Viewport').create();
183         }
184
185         this.launch.call(this.scope || this);
186         this.launched = true;
187         this.fireEvent('launch', this);
188
189         this.controllers.each(function(controller) {
190             controller.onLaunch(this);
191         }, this);
192     },
193
194     getModuleClassName: function(name, type) {
195         var namespace = Ext.Loader.getPrefix(name);
196
197         if (namespace.length > 0 && namespace !== name) {
198             return name;
199         }
200
201         return this.name + '.' + type + '.' + name;
202     },
203
204     getController: function(name) {
205         var controller = this.controllers.get(name);
206
207         if (!controller) {
208             controller = Ext.create(this.getModuleClassName(name, 'controller'), {
209                 application: this,
210                 id: name
211             });
212
213             this.controllers.add(controller);
214         }
215
216         return controller;
217     },
218
219     getStore: function(name) {
220         var store = Ext.StoreManager.get(name);
221
222         if (!store) {
223             store = Ext.create(this.getModuleClassName(name, 'store'), {
224                 storeId: name
225             });
226         }
227
228         return store;
229     },
230
231     getModel: function(model) {
232         model = this.getModuleClassName(model, 'model');
233
234         return Ext.ModelManager.getModel(model);
235     },
236
237     getView: function(view) {
238         view = this.getModuleClassName(view, 'view');
239
240         return Ext.ClassManager.get(view);
241     }
242 });