4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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-method-constructor'><span id='Ext-app-Application'>/**
19 </span></span> * @class Ext.app.Application
20 * @extend Ext.app.Controller
22 * Represents an Ext JS 4 application, which is typically a single page app using a {@link Ext.container.Viewport Viewport}.
23 * A typical Ext.app.Application might look like this:
27 * launch: function() {
28 * Ext.create('Ext.container.Viewport', {
36 * This does several things. First it creates a global variable called 'MyApp' - all of your Application's classes (such
37 * as its Models, Views and Controllers) will reside under this single namespace, which drastically lowers the chances
38 * of colliding global variables.
40 * When the page is ready and all of your JavaScript has loaded, your Application's {@link #launch} function is called,
41 * at which time you can run the code that starts your app. Usually this consists of creating a Viewport, as we do in
44 * <u>Telling Application about the rest of the app</u>
46 * Because an Ext.app.Application represents an entire app, we should tell it about the other parts of the app - namely
47 * the Models, Views and Controllers that are bundled with the application. Let's say we have a blog management app; we
48 * might have Models and Controllers for Posts and Comments, and Views for listing, adding and editing Posts and Comments.
49 * Here's how we'd tell our Application about all these things:
53 * models: ['Post', 'Comment'],
54 * controllers: ['Posts', 'Comments'],
56 * launch: function() {
61 * Note that we didn't actually list the Views directly in the Application itself. This is because Views are managed by
62 * Controllers, so it makes sense to keep those dependencies there. The Application will load each of the specified
63 * Controllers using the pathing conventions laid out in the <a href="../guide/application_architecture">application
64 * architecture guide</a> - in this case expecting the controllers to reside in app/controller/Posts.js and
65 * app/controller/Comments.js. In turn, each Controller simply needs to list the Views it uses and they will be
66 * automatically loaded. Here's how our Posts controller like be defined:
68 * Ext.define('MyApp.controller.Posts', {
69 * extend: 'Ext.app.Controller',
70 * views: ['posts.List', 'posts.Edit'],
72 * //the rest of the Controller here
75 * Because we told our Application about our Models and Controllers, and our Controllers about their Views, Ext JS will
76 * automatically load all of our app files for us. This means we don't have to manually add script tags into our html
77 * files whenever we add a new class, but more importantly it enables us to create a minimized build of our entire
78 * application using the Ext JS 4 SDK Tools.
80 * For more information about writing Ext JS 4 applications, please see the <a href="../guide/application_architecture">
81 * application architecture guide</a>.
83 * @docauthor Ed Spencer
86 Ext.define('Ext.app.Application', {
87 extend: 'Ext.app.Controller',
92 'Ext.data.StoreManager',
93 'Ext.tip.QuickTipManager',
94 'Ext.ComponentManager',
98 <span id='Ext-app-Application-cfg-name'> /**
99 </span> * @cfg {String} name The name of your application. This will also be the namespace for your views, controllers
100 * models and stores. Don't use spaces or special characters in the name.
103 <span id='Ext-app-Application-cfg-scope'> /**
104 </span> * @cfg {Object} scope The scope to execute the {@link #launch} function in. Defaults to the Application
109 <span id='Ext-app-Application-cfg-enableQuickTips'> /**
110 </span> * @cfg {Boolean} enableQuickTips True to automatically set up Ext.tip.QuickTip support (defaults to true)
112 enableQuickTips: true,
114 <span id='Ext-app-Application-cfg-defaultUrl'> /**
115 </span> * @cfg {String} defaultUrl When the app is first loaded, this url will be redirected to. Defaults to undefined
118 <span id='Ext-app-Application-cfg-appFolder'> /**
119 </span> * @cfg {String} appFolder The path to the directory which contains all application's classes.
120 * This path will be registered via {@link Ext.Loader#setPath} for the namespace specified in the {@link #name name} config.
125 <span id='Ext-app-Application-cfg-autoCreateViewport'> /**
126 </span> * @cfg {Boolean} autoCreateViewport True to automatically load and instantiate AppName.view.Viewport
127 * before firing the launch function (defaults to false).
129 autoCreateViewport: false,
131 constructor: function(config) {
132 config = config || {};
133 Ext.apply(this, config);
135 var requires = config.requires || [];
137 Ext.Loader.setPath(this.name, this.appFolder);
140 Ext.Object.each(this.paths, function(key, value) {
141 Ext.Loader.setPath(key, value);
145 this.callParent(arguments);
147 this.eventbus = Ext.create('Ext.app.EventBus');
149 var controllers = Ext.Array.from(this.controllers),
150 ln = controllers && controllers.length,
153 this.controllers = Ext.create('Ext.util.MixedCollection');
155 if (this.autoCreateViewport) {
156 requires.push(this.getModuleClassName('Viewport', 'view'));
159 for (i = 0; i < ln; i++) {
160 requires.push(this.getModuleClassName(controllers[i], 'controller'));
163 Ext.require(requires);
165 Ext.onReady(function() {
166 for (i = 0; i < ln; i++) {
167 controller = this.getController(controllers[i]);
168 controller.init(this);
171 this.onBeforeLaunch.call(this);
175 control: function(selectors, listeners, controller) {
176 this.eventbus.control(selectors, listeners, controller);
179 <span id='Ext-app-Application-property-launch'> /**
180 </span> * Called automatically when the page has completely loaded. This is an empty function that should be
181 * overridden by each application that needs to take action on page load
184 * @param {String} profile The detected {@link #profiles application profile}
185 * @return {Boolean} By default, the Application will dispatch to the configured startup controller and
186 * action immediately after running the launch function. Return false to prevent this behavior.
190 <span id='Ext-app-Application-method-onBeforeLaunch'> /**
193 onBeforeLaunch: function() {
194 if (this.enableQuickTips) {
195 Ext.tip.QuickTipManager.init();
198 if (this.autoCreateViewport) {
199 this.getView('Viewport').create();
202 this.launch.call(this.scope || this);
203 this.launched = true;
204 this.fireEvent('launch', this);
206 this.controllers.each(function(controller) {
207 controller.onLaunch(this);
211 getModuleClassName: function(name, type) {
212 var namespace = Ext.Loader.getPrefix(name);
214 if (namespace.length > 0 && namespace !== name) {
218 return this.name + '.' + type + '.' + name;
221 getController: function(name) {
222 var controller = this.controllers.get(name);
225 controller = Ext.create(this.getModuleClassName(name, 'controller'), {
230 this.controllers.add(controller);
236 getStore: function(name) {
237 var store = Ext.StoreManager.get(name);
240 store = Ext.create(this.getModuleClassName(name, 'store'), {
248 getModel: function(model) {
249 model = this.getModuleClassName(model, 'model');
251 return Ext.ModelManager.getModel(model);
254 getView: function(view) {
255 view = this.getModuleClassName(view, 'view');
257 return Ext.ClassManager.get(view);