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'>/**
19 </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
81 * [application architecture guide](#/guide/application_architecture).
83 * @docauthor Ed Spencer
85 Ext.define('Ext.app.Application', {
86 extend: 'Ext.app.Controller',
91 'Ext.data.StoreManager',
92 'Ext.tip.QuickTipManager',
93 'Ext.ComponentManager',
97 <span id='Ext-app-Application-cfg-name'> /**
98 </span> * @cfg {String} name The name of your application. This will also be the namespace for your views, controllers
99 * models and stores. Don't use spaces or special characters in the name.
102 <span id='Ext-app-Application-cfg-scope'> /**
103 </span> * @cfg {Object} scope The scope to execute the {@link #launch} function in. Defaults to the Application
108 <span id='Ext-app-Application-cfg-enableQuickTips'> /**
109 </span> * @cfg {Boolean} enableQuickTips True to automatically set up Ext.tip.QuickTip support (defaults to true)
111 enableQuickTips: true,
113 <span id='Ext-app-Application-cfg-defaultUrl'> /**
114 </span> * @cfg {String} defaultUrl When the app is first loaded, this url will be redirected to. Defaults to undefined
117 <span id='Ext-app-Application-cfg-appFolder'> /**
118 </span> * @cfg {String} appFolder The path to the directory which contains all application's classes.
119 * This path will be registered via {@link Ext.Loader#setPath} for the namespace specified in the {@link #name name} config.
124 <span id='Ext-app-Application-cfg-autoCreateViewport'> /**
125 </span> * @cfg {Boolean} autoCreateViewport True to automatically load and instantiate AppName.view.Viewport
126 * before firing the launch function (defaults to false).
128 autoCreateViewport: false,
130 <span id='Ext-app-Application-method-constructor'> /**
131 </span> * Creates new Application.
132 * @param {Object} config (optional) Config object.
134 constructor: function(config) {
135 config = config || {};
136 Ext.apply(this, config);
138 var requires = config.requires || [];
140 Ext.Loader.setPath(this.name, this.appFolder);
143 Ext.Object.each(this.paths, function(key, value) {
144 Ext.Loader.setPath(key, value);
148 this.callParent(arguments);
150 this.eventbus = Ext.create('Ext.app.EventBus');
152 var controllers = Ext.Array.from(this.controllers),
153 ln = controllers && controllers.length,
156 this.controllers = Ext.create('Ext.util.MixedCollection');
158 if (this.autoCreateViewport) {
159 requires.push(this.getModuleClassName('Viewport', 'view'));
162 for (i = 0; i < ln; i++) {
163 requires.push(this.getModuleClassName(controllers[i], 'controller'));
166 Ext.require(requires);
168 Ext.onReady(function() {
169 for (i = 0; i < ln; i++) {
170 controller = this.getController(controllers[i]);
171 controller.init(this);
174 this.onBeforeLaunch.call(this);
178 control: function(selectors, listeners, controller) {
179 this.eventbus.control(selectors, listeners, controller);
182 <span id='Ext-app-Application-property-launch'> /**
183 </span> * Called automatically when the page has completely loaded. This is an empty function that should be
184 * overridden by each application that needs to take action on page load
187 * @param {String} profile The detected {@link #profiles application profile}
188 * @return {Boolean} By default, the Application will dispatch to the configured startup controller and
189 * action immediately after running the launch function. Return false to prevent this behavior.
193 <span id='Ext-app-Application-method-onBeforeLaunch'> /**
196 onBeforeLaunch: function() {
197 if (this.enableQuickTips) {
198 Ext.tip.QuickTipManager.init();
201 if (this.autoCreateViewport) {
202 this.getView('Viewport').create();
205 this.launch.call(this.scope || this);
206 this.launched = true;
207 this.fireEvent('launch', this);
209 this.controllers.each(function(controller) {
210 controller.onLaunch(this);
214 getModuleClassName: function(name, type) {
215 var namespace = Ext.Loader.getPrefix(name);
217 if (namespace.length > 0 && namespace !== name) {
221 return this.name + '.' + type + '.' + name;
224 getController: function(name) {
225 var controller = this.controllers.get(name);
228 controller = Ext.create(this.getModuleClassName(name, 'controller'), {
233 this.controllers.add(controller);
239 getStore: function(name) {
240 var store = Ext.StoreManager.get(name);
243 store = Ext.create(this.getModuleClassName(name, 'store'), {
251 getModel: function(model) {
252 model = this.getModuleClassName(model, 'model');
254 return Ext.ModelManager.getModel(model);
257 getView: function(view) {
258 view = this.getModuleClassName(view, 'view');
260 return Ext.ClassManager.get(view);