Ext JS
Ext JS is a JavaScript framework for building rich, interactive web applications. It provides a set of UI components and tools that allow developers to create modern web applications that look and feel like desktop applications.
Components
Components are the heart of Ext JS. In reality, they are just JS components with specific attributes.
Components definitions
To define/declare a component, you use the Ext.define method, as follow:
A component MUST extend another component, whether that’s an Ext core component or a custom one.
For example, to define a custom button:
This button was a component for the classic toolkit. In modern, it can be found as
Ext.Button.
All Ext JS’s core components and their attributes/methods/events can all be found in the documentation with examples.
Use components
To use a component, you need to use either its class name (the string value, first argument of the define method) or its alias attribute.
Notice how the above button we created has an alias equal to
widget.customButton.
To use it, simply put it in the items of another class or a container using the xtype attribute. For example:
When you use a component, you can add or either overwrite its existing properties as shown above.
Requires
To use any component, more often than not, the compiler find it on its own. However, sometimes, you’ll have to add the component’s classname in the requires array of another component you want to include it in.
For example:
Toolkits
Sencha Ext JS offers three different toolkits, each of which is designed to support a specific type of application development:
-
Classic: The Classic toolkit is Ext JS’s original toolkit, and is designed for building traditional, “desktop-style” web applications.
This component was made by Stratis Dermanoutsos. The code can be found here. -
Modern: The Modern toolkit is a newer addition to Ext JS, and is designed for building modern, “mobile-first” web applications.
This component was made by Stratis Dermanoutsos. The code can be found here. -
Universal: The Universal toolkit is a combination of the Classic and Modern toolkits, and is designed to support applications that need to run on both desktop and mobile devices.
This component was made by Stratis Dermanoutsos. The code can be found here.
Layouts
There are several layouts that a container in Ext JS can use.
To declare which one will be chosen, we use the layout property.
Available layouts
-
borderThis is a multi-pane, application-oriented UI layout style that supports multiple nested panels, automatic bars between regions and built-in expanding and collapsing of regions.
This component was made by Stratis Dermanoutsos. The code can be found here.This layout’s children must declare a specific region to reside in.
Such regions are:
centereastnorthsouthwest
-
cardThis layout manages multiple child Components, each fitted to the Container, where only a single child Component can be visible at any given time.
This component was made by Stratis Dermanoutsos. The code can be found here. -
fitThis is a base class for layouts that contain a single item that automatically expands to fill the layout’s container.
This component was made by Stratis Dermanoutsos. The code can be found here. -
hboxA layout that arranges items horizontally across a
Ext.container.Container.This component was made by Stratis Dermanoutsos. The code can be found here.It is basically the same as using the following CSS in an HTML’s div:
This component was made by Stratis Dermanoutsos. The code can be found here. -
vboxA layout that arranges items vertically across a
Ext.container.Container.This component was made by Stratis Dermanoutsos. The code can be found here.It is basically the same as using the following CSS in an HTML’s div:
This component was made by Stratis Dermanoutsos. The code can be found here.
Stores
A store is a client-side collection of records (instances of a Model class). It basically holds instances of a Model class.
Let’s say we define a store of Users.
What we did, is create a store that holds 4 instances/objects of the User model.
The model can be defined as such:
This component was made by Stratis Dermanoutsos. The code can be found here.
To display the data in a dataview or a grid, simply add a property store: users in the component’s attributes.
Store inheritance
We can have a store inherit another store. This can be done with the type and source properties.
typeis used when the new store inherits the parent store’s properties but the data icluded, although identical, are different instances and, as such, will create duplicates.sourceis used when the new store inherits the parent store’s data.Sourceis typically used when we want to filter or sort data without editing the parent store.
Sorting
To sort a store’s data, you can either declare it with the sorters property or call the sort method.
Let’s say we have the following store:
-
Property
This component was made by Stratis Dermanoutsos. The code can be found here. -
Method
This component was made by Stratis Dermanoutsos. The code can be found here.
Filtering
Same with sorting, to filter a store’s data, you can either declare it with the filters property or call the filter method.
-
Property
This component was made by Stratis Dermanoutsos. The code can be found here. -
Method
This component was made by Stratis Dermanoutsos. The code can be found here.
Global stores
To make a store global is a very quick process:
-
Go to your
Application.jsfile and, inside the class definition, add the following:This component was made by Stratis Dermanoutsos. The code can be found here. -
In your store, provide a
storeIdattribute, usually the same as thealias.This component was made by Stratis Dermanoutsos. The code can be found here.
Install and create your first app
With NPM
-
Gerenate your first project
This component was made by Stratis Dermanoutsos. The code can be found here. -
Run the app
This component was made by Stratis Dermanoutsos. The code can be found here.
With CMD
-
Gerenate your first project
This component was made by Stratis Dermanoutsos. The code can be found here. -
Run the app
This component was made by Stratis Dermanoutsos. The code can be found here.
Build your App
-
Clean the current build
This component was made by Stratis Dermanoutsos. The code can be found here. -
Build
This component was made by Stratis Dermanoutsos. The code can be found here.
Build types
There are 2 main types of builds.
-
developmentwhich builds the app for the developer to run.This build includes non-minified code and detailed error messages.
-
productionwhich is the obvious choice for a complete app, ready to be deployed.This build does not have any debugging potential as it includes minified code for optimized performance.
-
testingwhich is a more dev friendly option.This is mostly used when the development is in the very late stages and the app can only be tested after being built and deployed.
This build includes non-minified code and detailed error messages.
-
universal. This is a bit complicated.In universal mode, an Ext JS application includes both minified and non-minified code, as well as detailed error messages.
Templates
Templates are used to create a project will default code, functions, methods, imports etc already setup for you on project creation.
You can also create your own templates to use.
Turning app into template
-
Supposedly your project is called
ProjectTemplate.Find and replace all references of
ProjectTemplateto{appName}. -
Delete all
(classic|modern)(-(el_GR|en|...)).json(p)files.These are the build files that are created on running the app.
-
Add the
.tplextension to:app.jsonbuild.xmlindex.html
-
Add the
.tpl.defaultextention to all other files except for*.jsonand*.scssones. -
“Escape” all dot characters (
.) that are inside curly brackets ({}) inapp.json.tpl.For example, change
"{toolkit.name}"to
"{toolkit\.name}"To make your life easier, search for regex
\{(\w+\.\w+)+\}. -
Change values inside curly brackets (
{}) so that they get compiled correctly.Basically, when building an app using a template, the program assumes that whatever is in curly brackets (
{}) is a variable to be changed. So, if you want to keep the curly brackets ({}) and the value inside, you must change the format so it understand it’s here to stay.Instead of
bind: '{binding}'we want
bind: '{["\{binding\}"]}'DO NOT attempt this for
{appName}that was mentioned earlier!
Create app using template
sencha generate app -ext -s <PATH_TO_TEMPLATE> <APP_NAME> <PATH_TO_CREATE_APP>
Eg.
sencha generate app -ext -s ./templates/some-template myApp ./myApp
Packages
Create a package
sencha generate package <PACKAGE_NAME>
Eg.
sencha generate package CustomComponents
Inside {workspace}/packages/local/ you will find a folder with the package name you set earlier.
package.json
This is an example of a package.json file’s content. It may vary from app to app but this is how I fill them:
Initialize a repository
sencha package repo init -name <YOUR_NAME> -email <YOUR_EMAIL>
Eg.
sencha package repo init -name "Stratis" -email "stratis.dermanoutsos@gmail.com"
This must be run inside the package’s directory.
This is used to sign the package as yours, in case you want to publish it.
Build a package
-
Generate the
*.pkgfile.sencha package buildThis must be run inside the package’s directory.
-
Add the built package to the local repository.
sencha package add <PATH_TO_THE_pkg_FILE>Usually, it’s located in
{workspace}/build/{packageName}/.
Make a package remote
-
Find the
pkgsdirectory in your local machine.Usual path for Windows:
C:\Users\USER\bin\Sencha\Cmd\repo\pkgs\. -
Copy the version of the package you built. Inside the
pkgsdirectory, the paths work this way:pkgs/{packageName}/{version}/.Copy the version directory. (eg.
1.0.0/)We only copy the version directory to not lose any other version of any other package.
If the package does not exist in the remote machine, copy all of the
{packageName}/. -
Paste it inside the
{packageName}/directory in the remote machine.Eg.
http://{path}/cmd/packages/{packageName}/. -
Be sure to update
catalog.jsonso that it references the latest version.There is 1
catalog.jsonfor thepackages/folder and 1 for each{packageName}/folder.Both must be updated.
Use remote package
-
Add your remote repository your sencha’s list.
sencha package repo add <REPOSITORY_NAME> <REPOSITORY_PATH>Eg.
sencha package repo add StratisPackages http://{path}/cmd/packages/This can be run anywhere. The list is global
-
Add the package’s name in the
requiresofapp.json. Eg."requires": [ "CustomComponents" ],To use a specific version, add a
@followed by the version. (More here)"requires": [ "CustomComponents@1.0.1" ], -
Sync the repository.
sencha repo sync -
Refresh package list.
sencha app refresh -packagesIMPORTANT: Make sure you do not have a local copy of the package.
If there isn’t one already, in the app’s workspace directory, inside
packages/folder, a new folder calledremote/has been created with the packages you use.