Clear Up
SharpKit Reference

Ext.app Namespace

Download SDK: SharpKit.ExtJs.zip

Classes

Name Description
Application

Represents an Ext JS 4 application, which is typically a single page app using a Viewport. A typical Ext.app.Application might look like this:

  
    Ext.application({
            name: 'MyApp',
            launch: function() {
            Ext.create('Ext.container.Viewport', {
            items: {
            html: 'My App'
            }
            });
            }
            });
            

This does several things. First it creates a global variable called 'MyApp' - all of your Application's classes (such as its Models, Views and Controllers) will reside under this single namespace, which drastically lowers the chances of colliding global variables.

When the page is ready and all of your JavaScript has loaded, your Application's launch function is called, at which time you can run the code that starts your app. Usually this consists of creating a Viewport, as we do in the example above.

Telling Application about the rest of the app

Because an Ext.app.Application represents an entire app, we should tell it about the other parts of the app - namely the Models, Views and Controllers that are bundled with the application. Let's say we have a blog management app; we might have Models and Controllers for Posts and Comments, and Views for listing, adding and editing Posts and Comments. Here's how we'd tell our Application about all these things:

  
    Ext.application({
            name: 'Blog',
            models: ['Post', 'Comment'],
            controllers: ['Posts', 'Comments'],
            launch: function() {
            ...
            }
            });
            

Note that we didn't actually list the Views directly in the Application itself. This is because Views are managed by Controllers, so it makes sense to keep those dependencies there. The Application will load each of the specified Controllers using the pathing conventions laid out in the application architecture guide - in this case expecting the controllers to reside in app/controller/Posts.js and app/controller/Comments.js. In turn, each Controller simply needs to list the Views it uses and they will be automatically loaded. Here's how our Posts controller like be defined:

  
    Ext.define('MyApp.controller.Posts', {
            extend: 'Ext.app.Controller',
            views: ['posts.List', 'posts.Edit'],
            //the rest of the Controller here
            });
            

Because we told our Application about our Models and Controllers, and our Controllers about their Views, Ext JS will automatically load all of our app files for us. This means we don't have to manually add script tags into our html files whenever we add a new class, but more importantly it enables us to create a minimized build of our entire application using the Ext JS 4 SDK Tools.

For more information about writing Ext JS 4 applications, please see the application architecture guide.

ApplicationConfig
ApplicationEvents
Controller

Controllers are the glue that binds an application together. All they really do is listen for events (usually from views) and take some action. Here's how we might create a Controller to manage Users:

  
    Ext.define('MyApp.controller.Users', {
            extend: 'Ext.app.Controller',
            init: function() {
            console.log('Initialized Users! This happens before the Application launch function is called');
            }
            });
            

The init function is a special method that is called when your application boots. It is called before the Application's launch function is executed so gives a hook point to run any code before your Viewport is created.

The init function is a great place to set up how your controller interacts with the view, and is usually used in conjunction with another Controller function - control. The control function makes it easy to listen to events on your view classes and take some action with a handler function. Let's update our Users controller to tell us when the panel is rendered:

  
    Ext.define('MyApp.controller.Users', {
            extend: 'Ext.app.Controller',
            init: function() {
            this.control({
            'viewport > panel': {
            render: this.onPanelRendered
            }
            });
            },
            onPanelRendered: function() {
            console.log('The panel was rendered');
            }
            });
            

We've updated the init function to use this.control to set up listeners on views in our application. The control function uses the new ComponentQuery engine to quickly and easily get references to components on the page. If you are not familiar with ComponentQuery yet, be sure to check out the documentation. In brief though, it allows us to pass a CSS-like selector that will find every matching component on the page.

In our init function above we supplied 'viewport > panel', which translates to "find me every Panel that is a direct child of a Viewport". We then supplied an object that maps event names (just 'render' in this case) to handler functions. The overall effect is that whenever any component that matches our selector fires a 'render' event, our onPanelRendered function is called.

Using refs

One of the most useful parts of Controllers is the new ref system. These use the new Ext.ComponentQuery to make it really easy to get references to Views on your page. Let's look at an example of this now:

  
    Ext.define('MyApp.controller.Users', {
            extend: 'Ext.app.Controller',
            refs: [
            {
            ref: 'list',
            selector: 'grid'
            }
            ],
            init: function() {
            this.control({
            'button': {
            click: this.refreshGrid
            }
            });
            },
            refreshGrid: function() {
            this.getList().store.load();
            }
            });
            

This example assumes the existence of a Grid on the page, which contains a single button to refresh the Grid when clicked. In our refs array, we set up a reference to the grid. There are two parts to this - the 'selector', which is a ComponentQuery selector which finds any grid on the page and assigns it to the reference 'list'.

By giving the reference a name, we get a number of things for free. The first is the getList function that we use in the refreshGrid method above. This is generated automatically by the Controller based on the name of our ref, which was capitalized and prepended with get to go from 'list' to 'getList'.

The way this works is that the first time getList is called by your code, the ComponentQuery selector is run and the first component that matches the selector ('grid' in this case) will be returned. All future calls to getList will use a cached reference to that grid. Usually it is advised to use a specific ComponentQuery selector that will only match a single View in your application (in the case above our selector will match any grid on the page).

Bringing it all together, our init function is called when the application boots, at which time we call this.control to listen to any click on a button and call our refreshGrid function (again, this will match any button on the page so we advise a more specific selector than just 'button', but have left it this way for simplicity). When the button is clicked we use out getList function to refresh the grid.

You can create any number of refs and control any number of components this way, simply adding more functions to your Controller as you go. For an example of real-world usage of Controllers see the Feed Viewer example in the examples/app/feed-viewer folder in the SDK download.

Generated getter methods

Refs aren't the only thing that generate convenient getter methods. Controllers often have to deal with Models and Stores so the framework offers a couple of easy ways to get access to those too. Let's look at another example:

  
    Ext.define('MyApp.controller.Users', {
            extend: 'Ext.app.Controller',
            models: ['User'],
            stores: ['AllUsers', 'AdminUsers'],
            init: function() {
            var User = this.getUserModel(),
            allUsers = this.getAllUsersStore();
            var ed = new User({name: 'Ed'});
            allUsers.add(ed);
            }
            });
            

By specifying Models and Stores that the Controller cares about, it again dynamically loads them from the appropriate locations (app/model/User.js, app/store/AllUsers.js and app/store/AdminUsers.js in this case) and creates getter functions for them all. The example above will create a new User model instance and add it to the AllUsers Store. Of course, you could do anything in this function but in this case we just did something simple to demonstrate the functionality.

Further Reading

For more information about writing Ext JS 4 applications, please see the application architecture guide. Also see the Ext.app.Application documentation.

ControllerConfig
ControllerEvents
EventBus

NOTE This is a private utility class for internal use by the framework. Don't rely on its existence.

EventBusConfig
EventBusEvents
© Copyright 2005-2011 SharpKit. All rights reserved.