1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-app.Controller-method-constructor'><span id='Ext-app.Controller'>/**
2 </span></span> * @class Ext.app.Controller
5 * Controllers are the glue that binds an application together. All they really do is listen for events (usually from
6 * views) and take some action. Here's how we might create a Controller to manage Users:
8 * Ext.define('MyApp.controller.Users', {
9 * extend: 'Ext.app.Controller',
12 * console.log('Initialized Users! This happens before the Application launch function is called');
16 * The init function is a special method that is called when your application boots. It is called before the
17 * {@link Ext.app.Application Application}'s launch function is executed so gives a hook point to run any code before
18 * your Viewport is created.
20 * The init function is a great place to set up how your controller interacts with the view, and is usually used in
21 * conjunction with another Controller function - {@link Ext.app.Controller#control control}. The control function
22 * makes it easy to listen to events on your view classes and take some action with a handler function. Let's update
23 * our Users controller to tell us when the panel is rendered:
25 * Ext.define('MyApp.controller.Users', {
26 * extend: 'Ext.app.Controller',
30 * 'viewport > panel': {
31 * render: this.onPanelRendered
36 * onPanelRendered: function() {
37 * console.log('The panel was rendered');
41 * We've updated the init function to use this.control to set up listeners on views in our application. The control
42 * function uses the new ComponentQuery engine to quickly and easily get references to components on the page. If you
43 * are not familiar with ComponentQuery yet, be sure to check out THIS GUIDE for a full explanation. In brief though,
44 * it allows us to pass a CSS-like selector that will find every matching component on the page.
46 * In our init function above we supplied 'viewport > panel', which translates to "find me every Panel that is a direct
47 * child of a Viewport". We then supplied an object that maps event names (just 'render' in this case) to handler
48 * functions. The overall effect is that whenever any component that matches our selector fires a 'render' event, our
49 * onPanelRendered function is called.
51 * <u>Using refs</u>
53 * One of the most useful parts of Controllers is the new ref system. These use the new {@link Ext.ComponentQuery} to
54 * make it really easy to get references to Views on your page. Let's look at an example of this now:
56 * Ext.define('MyApp.controller.Users', {
57 extend: 'Ext.app.Controller',
69 click: this.refreshGrid
74 refreshGrid: function() {
75 this.getList().store.load();
79 * This example assumes the existence of a {@link Ext.grid.Panel Grid} on the page, which contains a single button to
80 * refresh the Grid when clicked. In our refs array, we set up a reference to the grid. There are two parts to this -
81 * the 'selector', which is a {@link Ext.ComponentQuery ComponentQuery} selector which finds any grid on the page and
82 * assigns it to the reference 'list'.
84 * By giving the reference a name, we get a number of things for free. The first is the getList function that we use in
85 * the refreshGrid method above. This is generated automatically by the Controller based on the name of our ref, which
86 * was capitalized and prepended with get to go from 'list' to 'getList'.
88 * The way this works is that the first time getList is called by your code, the ComponentQuery selector is run and the
89 * first component that matches the selector ('grid' in this case) will be returned. All future calls to getList will
90 * use a cached reference to that grid. Usually it is advised to use a specific ComponentQuery selector that will only
91 * match a single View in your application (in the case above our selector will match any grid on the page).
93 * Bringing it all together, our init function is called when the application boots, at which time we call this.control
94 * to listen to any click on a {@link Ext.button.Button button} and call our refreshGrid function (again, this will
95 * match any button on the page so we advise a more specific selector than just 'button', but have left it this way for
96 * simplicity). When the button is clicked we use out getList function to refresh the grid.
98 * You can create any number of refs and control any number of components this way, simply adding more functions to
99 * your Controller as you go. For an example of real-world usage of Controllers see the Feed Viewer example in the
100 * examples/app/feed-viewer folder in the SDK download.
102 * <u>Generated getter methods</u>
104 * Refs aren't the only thing that generate convenient getter methods. Controllers often have to deal with Models and
105 * Stores so the framework offers a couple of easy ways to get access to those too. Let's look at another example:
107 * Ext.define('MyApp.controller.Users', {
108 extend: 'Ext.app.Controller',
111 stores: ['AllUsers', 'AdminUsers'],
114 var User = this.getUserModel(),
115 allUsers = this.getAllUsersStore();
117 var ed = new User({name: 'Ed'});
122 * By specifying Models and Stores that the Controller cares about, it again dynamically loads them from the appropriate
123 * locations (app/model/User.js, app/store/AllUsers.js and app/store/AdminUsers.js in this case) and creates getter
124 * functions for them all. The example above will create a new User model instance and add it to the AllUsers Store.
125 * Of course, you could do anything in this function but in this case we just did something simple to demonstrate the
128 * <u>Further Reading</u>
130 * For more information about writing Ext JS 4 applications, please see the <a href="../guide/application_architecture">
131 * application architecture guide</a>. Also see the {@link Ext.app.Application} documentation.
134 * @docauthor Ed Spencer
136 Ext.define('Ext.app.Controller', {
137 <span id='Ext-app.Controller-cfg-id'> /**
138 </span> * @cfg {Object} id The id of this controller. You can use this id when dispatching.
142 observable: 'Ext.util.Observable'
145 onClassExtended: function(cls, data) {
146 var className = Ext.getClassName(cls),
147 match = className.match(/^(.*)\.controller\./);
149 if (match !== null) {
150 var namespace = Ext.Loader.getPrefix(className) || match[1],
151 onBeforeClassCreated = data.onBeforeClassCreated,
153 modules = ['model', 'view', 'store'],
156 data.onBeforeClassCreated = function(cls, data) {
158 items, j, subLn, item;
160 for (i = 0,ln = modules.length; i < ln; i++) {
163 items = Ext.Array.from(data[module + 's']);
165 for (j = 0,subLn = items.length; j < subLn; j++) {
168 prefix = Ext.Loader.getPrefix(item);
170 if (prefix === '' || prefix === item) {
171 requires.push(namespace + '.' + module + '.' + item);
179 Ext.require(requires, Ext.Function.pass(onBeforeClassCreated, arguments, this));
184 constructor: function(config) {
185 this.mixins.observable.constructor.call(this, config);
187 Ext.apply(this, config || {});
189 this.createGetters('model', this.models);
190 this.createGetters('store', this.stores);
191 this.createGetters('view', this.views);
199 init: function(application) {},
201 onLaunch: function(application) {},
203 createGetters: function(type, refs) {
204 type = Ext.String.capitalize(type);
205 Ext.Array.each(refs, function(ref) {
207 parts = ref.split('.');
209 // Handle namespaced class names. E.g. feed.Add becomes getFeedAddView etc.
210 Ext.Array.each(parts, function(part) {
211 fn += Ext.String.capitalize(part);
216 this[fn] = Ext.Function.pass(this['get' + type], [ref], this);
218 // Execute it right away
224 ref: function(refs) {
226 refs = Ext.Array.from(refs);
227 Ext.Array.each(refs, function(info) {
229 fn = 'get' + Ext.String.capitalize(ref);
231 me[fn] = Ext.Function.pass(me.getRef, [ref, info], me);
236 getRef: function(ref, info, config) {
237 this.refCache = this.refCache || {};
239 config = config || {};
241 Ext.apply(info, config);
243 if (info.forceCreate) {
244 return Ext.ComponentManager.create(info, 'component');
248 selector = info.selector,
249 cached = me.refCache[ref];
252 me.refCache[ref] = cached = Ext.ComponentQuery.query(info.selector)[0];
253 if (!cached && info.autoCreate) {
254 me.refCache[ref] = cached = Ext.ComponentManager.create(info, 'component');
257 cached.on('beforedestroy', function() {
258 me.refCache[ref] = null;
266 control: function(selectors, listeners) {
267 this.application.control(selectors, listeners, this);
270 getController: function(name) {
271 return this.application.getController(name);
274 getStore: function(name) {
275 return this.application.getStore(name);
278 getModel: function(model) {
279 return this.application.getModel(model);
282 getView: function(view) {
283 return this.application.getView(view);
286 </pre></pre></body></html>