4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-app-Application'>/**
19 </span> * Represents an Ext JS 4 application, which is typically a single page app using a {@link Ext.container.Viewport Viewport}.
20 * A typical Ext.app.Application might look like this:
24 * launch: function() {
25 * Ext.create('Ext.container.Viewport', {
33 * This does several things. First it creates a global variable called 'MyApp' - all of your Application's classes (such
34 * as its Models, Views and Controllers) will reside under this single namespace, which drastically lowers the chances
35 * of colliding global variables.
37 * When the page is ready and all of your JavaScript has loaded, your Application's {@link #launch} function is called,
38 * at which time you can run the code that starts your app. Usually this consists of creating a Viewport, as we do in
41 * # Telling Application about the rest of the app
43 * Because an Ext.app.Application represents an entire app, we should tell it about the other parts of the app - namely
44 * the Models, Views and Controllers that are bundled with the application. Let's say we have a blog management app; we
45 * might have Models and Controllers for Posts and Comments, and Views for listing, adding and editing Posts and Comments.
46 * Here's how we'd tell our Application about all these things:
50 * models: ['Post', 'Comment'],
51 * controllers: ['Posts', 'Comments'],
53 * launch: function() {
58 * Note that we didn't actually list the Views directly in the Application itself. This is because Views are managed by
59 * Controllers, so it makes sense to keep those dependencies there. The Application will load each of the specified
60 * Controllers using the pathing conventions laid out in the [application architecture guide][mvc] -
61 * in this case expecting the controllers to reside in `app/controller/Posts.js` and
62 * `app/controller/Comments.js`. In turn, each Controller simply needs to list the Views it uses and they will be
63 * automatically loaded. Here's how our Posts controller like be defined:
65 * Ext.define('MyApp.controller.Posts', {
66 * extend: 'Ext.app.Controller',
67 * views: ['posts.List', 'posts.Edit'],
69 * //the rest of the Controller here
72 * Because we told our Application about our Models and Controllers, and our Controllers about their Views, Ext JS will
73 * automatically load all of our app files for us. This means we don't have to manually add script tags into our html
74 * files whenever we add a new class, but more importantly it enables us to create a minimized build of our entire
75 * application using the Ext JS 4 SDK Tools.
77 * For more information about writing Ext JS 4 applications, please see the
78 * [application architecture guide][mvc].
80 * [mvc]: #!/guide/application_architecture
82 * @docauthor Ed Spencer
84 Ext.define('Ext.app.Application', {
85 extend: 'Ext.app.Controller',
90 'Ext.data.StoreManager',
91 'Ext.tip.QuickTipManager',
92 'Ext.ComponentManager',
96 <span id='Ext-app-Application-cfg-name'> /**
97 </span> * @cfg {String} name The name of your application. This will also be the namespace for your views, controllers
98 * models and stores. Don't use spaces or special characters in the name.
101 <span id='Ext-app-Application-cfg-scope'> /**
102 </span> * @cfg {Object} scope The scope to execute the {@link #launch} function in. Defaults to the Application
107 <span id='Ext-app-Application-cfg-enableQuickTips'> /**
108 </span> * @cfg {Boolean} enableQuickTips True to automatically set up Ext.tip.QuickTip support.
110 enableQuickTips: true,
112 <span id='Ext-app-Application-cfg-defaultUrl'> /**
113 </span> * @cfg {String} defaultUrl When the app is first loaded, this url will be redirected to.
116 <span id='Ext-app-Application-cfg-appFolder'> /**
117 </span> * @cfg {String} appFolder The path to the directory which contains all application's classes.
118 * This path will be registered via {@link Ext.Loader#setPath} for the namespace specified in the {@link #name name} config.
122 <span id='Ext-app-Application-cfg-autoCreateViewport'> /**
123 </span> * @cfg {Boolean} autoCreateViewport True to automatically load and instantiate AppName.view.Viewport
124 * before firing the launch function.
126 autoCreateViewport: false,
128 <span id='Ext-app-Application-method-constructor'> /**
129 </span> * Creates new Application.
130 * @param {Object} [config] Config object.
132 constructor: function(config) {
133 config = config || {};
134 Ext.apply(this, config);
136 var requires = config.requires || [];
138 Ext.Loader.setPath(this.name, this.appFolder);
141 Ext.Object.each(this.paths, function(key, value) {
142 Ext.Loader.setPath(key, value);
146 this.callParent(arguments);
148 this.eventbus = Ext.create('Ext.app.EventBus');
150 var controllers = Ext.Array.from(this.controllers),
151 ln = controllers && controllers.length,
154 this.controllers = Ext.create('Ext.util.MixedCollection');
156 if (this.autoCreateViewport) {
157 requires.push(this.getModuleClassName('Viewport', 'view'));
160 for (i = 0; i < ln; i++) {
161 requires.push(this.getModuleClassName(controllers[i], 'controller'));
164 Ext.require(requires);
166 Ext.onReady(function() {
167 for (i = 0; i < ln; i++) {
168 controller = this.getController(controllers[i]);
169 controller.init(this);
172 this.onBeforeLaunch.call(this);
176 control: function(selectors, listeners, controller) {
177 this.eventbus.control(selectors, listeners, controller);
180 <span id='Ext-app-Application-property-launch'> /**
181 </span> * Called automatically when the page has completely loaded. This is an empty function that should be
182 * overridden by each application that needs to take action on page load
185 * @param {String} profile The detected {@link #profiles application profile}
186 * @return {Boolean} By default, the Application will dispatch to the configured startup controller and
187 * action immediately after running the launch function. Return false to prevent this behavior.
191 <span id='Ext-app-Application-method-onBeforeLaunch'> /**
194 onBeforeLaunch: function() {
195 if (this.enableQuickTips) {
196 Ext.tip.QuickTipManager.init();
199 if (this.autoCreateViewport) {
200 this.getView('Viewport').create();
203 this.launch.call(this.scope || this);
204 this.launched = true;
205 this.fireEvent('launch', this);
207 this.controllers.each(function(controller) {
208 controller.onLaunch(this);
212 getModuleClassName: function(name, type) {
213 var namespace = Ext.Loader.getPrefix(name);
215 if (namespace.length > 0 && namespace !== name) {
219 return this.name + '.' + type + '.' + name;
222 getController: function(name) {
223 var controller = this.controllers.get(name);
226 controller = Ext.create(this.getModuleClassName(name, 'controller'), {
231 this.controllers.add(controller);
237 getStore: function(name) {
238 var store = Ext.StoreManager.get(name);
241 store = Ext.create(this.getModuleClassName(name, 'store'), {
249 getModel: function(model) {
250 model = this.getModuleClassName(model, 'model');
252 return Ext.ModelManager.getModel(model);
255 getView: function(view) {
256 view = this.getModuleClassName(view, 'view');
258 return Ext.ClassManager.get(view);