Many classes have shortcut names used when creating (instantiating) a class with a
configuration object. The shortcut name is referred to as an alias
(or xtype
if the
class extends Ext.Component). The alias/xtype is listed next to the class name of
applicable classes for quick reference.
Framework classes or their members may be specified as private
or protected
. Else,
the class / member is public
. Public
, protected
, and private
are access
descriptors used to convey how and when the class or class member should be used.
Public classes and class members are available for use by any other class or application code and may be relied upon as a stable and persistent within major product versions. Public classes and members may safely be extended via a subclass.
Protected class members are stable public
members intended to be used by the
owning class or its subclasses. Protected members may safely be extended via a subclass.
Private classes and class members are used internally by the framework and are not intended to be used by application developers. Private classes and members may change or be omitted from the framework at any time without notice and should not be relied upon in application logic.
static
label next to the
method name. *See Static below.Below is an example class member that we can disect to show the syntax of a class member (the lookupComponent method as viewed from the Ext.button.Button class in this case).
Let's look at each part of the member row:
lookupComponent
in this example)( item )
in this example)Ext.Component
in this case). This may be omitted for methods that do not
return anything other than undefined
or may display as multiple possible values
separated by a forward slash /
signifying that what is returned may depend on the
results of the method call (i.e. a method may return a Component if a get method calls is
successful or false
if unsuccessful which would be displayed as
Ext.Component/Boolean
).PROTECTED
in
this example - see the Flags section below)Ext.container.Container
in this example). The source
class will be displayed as a blue link if the member originates from the current class
and gray if it is inherited from an ancestor or mixed-in class.view source
in the example)item : Object
in the example).undefined
a "Returns" section
will note the type of class or object returned and a description (Ext.Component
in the
example)Available since 3.4.0
- not pictured in
the example) just after the member descriptionDefaults to: false
)The API documentation uses a number of flags to further commnicate the class member's function and intent. The label may be represented by a text label, an abbreviation, or an icon.
classInstance.method1().method2().etc();
false
is returned from
an event handler- Indicates a framework class
- A singleton framework class. *See the singleton flag for more information
- A component-type framework class (any class within the Ext JS framework that extends Ext.Component)
- Indicates that the class, member, or guide is new in the currently viewed version
- Indicates a class member of type config
- Indicates a class member of type property
- Indicates a class member of type
method
- Indicates a class member of type event
- Indicates a class member of type
theme variable
- Indicates a class member of type
theme mixin
- Indicates that the class, member, or guide is new in the currently viewed version
Just below the class name on an API doc page is a row of buttons corresponding to the types of members owned by the current class. Each button shows a count of members by type (this count is updated as filters are applied). Clicking the button will navigate you to that member section. Hovering over the member-type button will reveal a popup menu of all members of that type for quick navigation.
Getting and setter methods that correlate to a class config option will show up in the methods section as well as in the configs section of both the API doc and the member-type menus just beneath the config they work with. The getter and setter method documentation will be found in the config row for easy reference.
Your page history is kept in localstorage and displayed (using the available real estate) just below the top title bar. By default, the only search results shown are the pages matching the product / version you're currently viewing. You can expand what is displayed by clicking on the button on the right-hand side of the history bar and choosing the "All" radio option. This will show all recent pages in the history bar for all products / versions.
Within the history config menu you will also see a listing of your recent page visits. The results are filtered by the "Current Product / Version" and "All" radio options. Clicking on the button will clear the history bar as well as the history kept in local storage.
If "All" is selected in the history config menu the checkbox option for "Show product details in the history bar" will be enabled. When checked, the product/version for each historic page will show alongside the page name in the history bar. Hovering the cursor over the page names in the history bar will also show the product/version as a tooltip.
Both API docs and guides can be searched for using the search field at the top of the page.
On API doc pages there is also a filter input field that filters the member rows using the filter string. In addition to filtering by string you can filter the class members by access level, inheritance, and read only. This is done using the checkboxes at the top of the page.
The checkbox at the bottom of the API class navigation tree filters the class list to include or exclude private classes.
Clicking on an empty search field will show your last 10 searches for quick navigation.
Each API doc page (with the exception of Javascript primitives pages) has a menu view of metadata relating to that class. This metadata view will have one or more of the following:
Ext.button.Button
class has an alternate class name of Ext.Button
). Alternate class
names are commonly maintained for backward compatibility.Runnable examples (Fiddles) are expanded on a page by default. You can collapse and expand example code blocks individually using the arrow on the top-left of the code block. You can also toggle the collapse state of all examples using the toggle button on the top-right of the page. The toggle-all state will be remembered between page loads.
Class members are collapsed on a page by default. You can expand and collapse members using the arrow icon on the left of the member row or globally using the expand / collapse all toggle button top-right.
Viewing the docs on narrower screens or browsers will result in a view optimized for a smaller form factor. The primary differences between the desktop and "mobile" view are:
The class source can be viewed by clicking on the class name at the top of an API doc page. The source for class members can be viewed by clicking on the "view source" link on the right-hand side of the member row.
Ext JS delivers some exciting improvements for you to use in your application architecture. We have added support for ViewModels and MVVM as well as ViewControllers to enhance MVC applications. Best of all, these choices are not mutually exclusive, so you can introduce these features incrementally or even blend them.
A Controller is a class derived from Ext.app.Controller. These Controllers use CSS-like selectors (called “Component Queries”) to match components and respond to their events. They also use “refs” to select and retrieve component instances.
These controllers are created at application launch and remain present for the life of the application. During its lifetime, views of interest to a controller will come and go. There may even be multiple instances of views that the controller manages.
For large applications, these techniques can create certain challenges.
In such environments, views and controllers may be authored by multiple development teams and integrated into the final application. Ensuring that controllers only react to their intended views can be difficult. Further, it is common for developers to want to limit the number of controllers created at application launch. While lazily creating controllers is possible with some effort, they can’t be destroyed, so they remain even if they are no longer needed.
While Ext JS 5+ is backwards compatible with legacy application-level controllers, it introduces a new type of controller designed to handle these challenges: Ext.app.ViewController. ViewController does this in the following ways:
For the purposes of ViewControllers, we can look at just two examples. The first is a basic use of a listeners config on a child item in a view:
Ext.define('MyApp.view.foo.Foo', {
extend: 'Ext.panel.Panel',
xtype: 'foo',
controller: 'foo',
items: [{
xtype: 'textfield',
fieldLabel: 'Bar',
listeners: {
change: 'onBarChange' // no scope given here
}
}]
});
Ext.define('MyApp.view.foo.FooController', {
extend: 'Ext.app.ViewController',
alias: 'controller.foo',
onBarChange: function (barTextField) {
// called by 'change' event
}
});
The above use of listeners shows a named event handler (onBarChange
) with no specified
“scope”. Internally, the event system resolves the default scope for the Bar textfield
to its owning ViewController.
Historically, the listeners config was reserved for use by a component’s creator, so how would a view listen to its own events, or perhaps those fired by its base class? The answer is that we need to use an explicit scope:
Ext.define('MyApp.view.foo.Foo', {
extend: 'Ext.panel.Panel',
xtype: 'foo',
controller: 'foo',
listeners: {
collapse: 'onCollapse',
scope: 'controller'
},
items: [{
...
}]
});
The above example leverages two new features in Ext JS: named scopes and declarative listeners. We’ll focus on the named scope here. There are two valid values for named scope: “this” and “controller”. When writing MVC applications, we almost always use “controller” which has the obvious result of looking on that view’s ViewController (not the ViewController of the view that created the instance).
Since a view is a type of Ext.Component, we have assigned this view an “xtype” which enables other views to create an instance of our view in the same way this view created its textfield. To see how this comes together, consider a view that uses this one. For example:
Ext.define('MyApp.view.bar.Bar', {
extend: 'Ext.panel.Panel',
xtype: 'bar',
controller: 'bar',
items: [{
xtype: 'foo',
listeners: {
collapse: 'onCollapse'
}
}]
});
In this case, the Bar view is creating an instance of the Foo view as one of its items. Further, it is listening to the collapse event just like the Foo view. The listeners declared by the Foo view will fire on Foo’s ViewController while the listeners declared in the Bar view will fire on Bar’s ViewController.
One of the most common loose ends when writing controller logic is getting ahold of the necessary components to complete a particular action. Something as simple as:
Ext.define('MyApp.view.foo.Foo', {
extend: 'Ext.panel.Panel',
xtype: 'foo',
controller: 'foo',
tbar: [{
xtype: 'button',
text: 'Add',
handler: 'onAdd'
}],
items: [{
xtype: 'grid',
...
}]
});
Ext.define('MyApp.view.foo.FooController', {
extend: 'Ext.app.ViewController',
alias: 'controller.foo',
onAdd: function () {
// ... get the grid and add a record ...
}
});
But how should one acquire the grid component? All techniques require you to place some recognizable property on the grid to allow it to be uniquely identified. Older techniques used the “id” config (and getCmp) or the “itemId” config (using “refs” or some component query method). The advantage of “id” is fast lookup, but since these identifiers must be unique across the entire application and the DOM as well, this is not often desirable. Using “itemId” and some form of query is more flexible, but it’s necessary to perform a search to find the desired component.
Using the reference
config, we simply add the “reference” to the grid and use
lookupReference
to get at it:
Ext.define('MyApp.view.foo.Foo', {
extend: 'Ext.panel.Panel',
xtype: 'foo',
controller: 'foo',
tbar: [{
xtype: 'button',
text: 'Add',
handler: 'onAdd'
}],
items: [{
xtype: 'grid',
reference: 'fooGrid'
...
}]
});
Ext.define('MyApp.view.foo.FooController', {
extend: 'Ext.app.ViewController',
alias: 'controller.foo',
onAdd: function () {
var grid = this.lookupReference('fooGrid');
}
});
This is similar to assigning an itemId of “fooGrid” and doing: “this.down(‘#fooGrid’)”.
The difference under the hood, however, is quite significant. First, a reference config
instructs the component to register itself with its owning view (identified by the
presence of a ViewController in this case). Second, the lookupReference
method simply
consults the cache to see if references need to be refreshed (say due to add or remove
on a container). If all is well, it just returns the reference from the cache. Or,
in pseudo-code:
lookupReference: (reference) {
var cache = this.references;
if (!cache) {
Ext.fixReferences(); // fix all references
cache = this.references; // now the cache is valid
}
return cache[reference];
}
In other words, there is no search and the linkages damaged by adding or removing items from containers are fixed in one go, when they are needed. As we will see below, this approach has benefits other than efficiency.
The use of selectors along with the controller refs
config was very flexible, but at
the same time had certain risks. The fact that these selectors “see” everything at all
levels of the component hierarchy is both powerful and prone to mistakes. For example, a
controller could be working 100% when it runs in isolation but then fails as soon as
other views are introduced because its selectors have undesired matches with the new
view.
These problems can be managed by following certain practices, but when using listeners and references with a ViewController these problems are simply not possible. This is because the listeners and reference configs connect only with their owning ViewController. Views are free to chose any reference value that is unique within that view knowing that these names will not be exposed to the view’s creator.
Likewise, listeners are resolved on their owning ViewController and cannot be accidentally dispatched to event handlers in other controllers with errant selectors. While listeners are often preferable to selectors, the two mechanisms play well together for those situations where a selector-based approach is desired.
To complete this model, views need to fire events that can be consumed by their owning
view’s ViewController. There is a helper method in ViewController for this purpose:
fireViewEvent
. For example:
Ext.define('MyApp.view.foo.FooController', {
extend: 'Ext.app.ViewController',
alias: 'controller.foo',
onAdd: function () {
var record = new MyApp.model.Thing();
var grid = this.lookupReference('fooGrid');
grid.store.add(record);
this.fireViewEvent('addrecord', this, record);
}
});
This enables the standard form of listener for the creator of this view:
Ext.define('MyApp.view.bar.Bar', {
extend: 'Ext.panel.Panel',
xtype: 'bar',
controller: 'bar',
items: [{
xtype: 'foo',
listeners: {
collapse: 'onCollapse',
addrecord: 'onAddRecord'
}
}]
});
In Ext JS 4.2, the MVC event dispatcher was generalized with the introduction of event domains. These event domains intercepted events as they were fired and dispatched them to controllers controlled by selector matching. The “component” event domain had full component query selectors while the other domains had more limited selectors.
With Ext JS 5+, each ViewController creates an instance of a new type of event domain
called the “view” event domain. This event domain allows ViewControllers to use the
standard listen
and control
methods while limiting their scope implicitly to their
view. It also adds a special selector to match the view itself:
Ext.define('MyApp.view.foo.FooController', {
extend: 'Ext.app.ViewController',
alias: 'controller.foo',
control: {
'#': { // matches the view itself
collapse: 'onCollapse'
},
button: {
click: 'onAnyButtonClick'
}
}
});
The key difference between listeners and selectors can be seen above. The “button” selector will match any button in this view or any child view, irrespective of depth, even if that button belongs to a great-grandchild view. In other words, selector-based handlers do not respect encapsulation boundaries. This behavior is consistent with previous Ext.app.Controller behavior and can be a useful technique in limited situations.
Lastly, these event domains respect nesting and effectively “bubble” an event up the view hierarchy. That is to say, when an event fires, it’s first delivered to any standard listeners. Then, it’s delivered to its owning ViewController, followed by its parent ViewController (if any) on up the hierarchy. Eventually, the event is delivered to the standard “component” event domain to be handled by Ext.app.Controller-derived controllers.
A common technique with large applications is to dynamically create controllers when they are first needed. This could help reduce the load time of the application and help runtime performance as well by not activating all potential controllers. The limitation to this in previous versions was that, once created, these controllers would remain active in the application. It was not possible to destroy them and release their resources. Likewise, this did not change the reality that a controller could have any number of associated views (including none).
The ViewController, however, is created very early in the component’s lifecycle and is bound to that view for its entire lifetime. When that view is destroyed, the ViewController is likewise destroyed. This means that the ViewController is no longer forced to manage states where there is no view or many views.
This one-to-one relationship means reference tracking is simplified and no longer prone to leaking destroyed components. A ViewController can implement any of these methods to perform tasks at key points in its lifecycle:
callParent
).We think ViewControllers will greatly streamline your MVC applications. They also work very well with ViewModels, so you can combine these approaches and their respective strengths.