On Github Pencroff / DiveInToExtJs5
Created by Sergii Danilov / @pencroff
Hello, my name is Sergii. I am a web developer. Let's discus today about ExtJs 5. Next => What is Sencha ExtJS?Wikipedia: «Ext JS is a pure JavaScript application framework for building interactive web applications using techniques such as Ajax, DHTML and DOM scripting. Originally built as an add-on library extension of YUI by Jack Slocum, Ext JS includes interoperability with jQuery and Prototype.»
Sencha: «Ext JS 5 ships with more than 300 classes. We have more than 2 million developers to date and they come from various backgrounds and locations.»
What is Sencha ExtJS? So. What is ExtJs? This is JavaScript framework that contains all features for client-side development out of the box. ExtJs uses class-based development and as we can see it uses more than 300 classes and has big community with more than 2 million developers. Next => Popularity ExtJSClass-based development
Application architecture (Backbone.js, AngularJS, Ember.js)
Data manipulation, like Underscore.js, but more
DOM queries (JQuery, Zepto.js)
UI elements (JQuery UI, Kendo UI)
Charts (Highcharts, Chart.js)
Loader (RequireJs, yepnope.js)
Features out of the box ExtJs has a lot of features out of the box that really help in development. As foundation in framework uses Class-based approach. ExtJs helps organise application architecture like AngularJS, Ember.js, Backbone.js. Also it has wide opportunities for data manipulation and dom queries. HTML and CSS are hidden from developers under UI elements and charts. All this features load into browser by internal loader. Next => Class-based approachExt.define('App.common.proxy.AppRest', { extend: 'Ext.data.proxy.Rest', alternateClassName: 'Ext.data.AppRestProxy', alias: 'proxy.apprest', reader: { type: 'json', rootProperty: 'data', totalProperty: 'total' } });Inheritance Look at Inheritance example. It's really easy to build a branched hierarchy of classes. Next => Overriding
Ext.define('Jarvus.hotfixes.ext.form.field.Tag.FindRecord', { override: 'Ext.form.field.Tag', // compatibility: '5.0.1255', findRecord: function(field, value) { return this.getStore().findRecord(field, value); } });
New version still has bugs and this feature use for hotfixes
Overriding Overriding give us very good opportunity update hardcoded value inside application. A few days ago I have updated hardcoded time format inside third party component. Next => Mixins// Declaration Ext.define('CanSing', { sing: function() { alert("For he's a jolly good fellow...") } }); // Using Ext.define('Musician', { mixins: { canSing: 'CanSing' }, sing: function() { // delegate singing operation to mixin this.mixins.canSing.sing.call(this); } })Mixins If you need to blend some duplicate logic that is not depended from data, you can use mixins mechanism. Next => Statics
Ext.define('Computer', { statics: { factory: function(brand) { /* 'this' in static methods refer to the class itself */ return new this(brand); } }, constructor: function(brand) { this._brand = brand; } }); var dellComputer = Computer.factory('Dell');Statics Static section gives us an opportunity to put methods to some type, not to instance. Next => Singleton
Ext.define('Exceptions', { singleton: true, requires: ['Messages'], onRequestException: function (conn, response, request, eOpts) { 'use strict'; var exceptionInfo = { Status: response.status + ' - ' + response.statusText, Description: response.responseText }; Messages.showException(exceptionInfo); } });Singleton Also in ExtJs5 it is very easy to declare singleton behavior. Next => Data Package
Ext.define('App.model.PostModel', { extend: 'Ext.data.Model', idProperty: '_id', fields: [ { name: '_id', type: 'string' }, { name: 'title', type: 'string' }, { name: 'created', type: 'date' }, { name: 'published', type: 'boolean' }, { name: 'tags', type: 'auto' }, { name: 'locales', type: 'auto' } // complex type ], proxy: { type: 'apprest', url: '/api/posts' } });Model example Look at example of the model. It extended from base Model type and has fields array and proxy for using some kind of native storage (Server side, Local storage and etc.). Next => Custom type
Ext.define('App.fields.Gender', { extend: 'Ext.data.field.String',s alias: 'data.field.gender', validators: { type: 'inclusion', list: [ 'female', 'male' ] } });Custom type Model supports Boolean, Date, Integer, Number and String field types, but you can declare a custom type. Next => Proxy
Ext.define('App.model.User', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'name', type: 'string' }, { name: 'gender', type: 'gender' } ], validators: { // Validator type could be: Presence, Length, Range, // Format, Inclusion, Exclusion, Email and Custom name: [ 'presence', { type: 'length', min: 7 }, { type: 'exclusion', list: ['Bender'] } ] } });Validations Model has a few predefined types of validators list which could be extended by custom validators. Next => Object validation
var newUser = new App.model.User({ id: 10, name: 'Bender', gender: 'robot' }); // run some validation on the new user we just created console.log('Is User valid?', newUser.isValid());Object validation Look at the example of model validation. This model is not valid because name can't be Bender and gender can't be robot. Next => Associations
Ext.define('App.model.Person', { extend: 'App.model.Base', fields: [ 'name' ], hasMany: 'Order' // the "entityName" for "App.model.Order" (see below) }); Ext.define('App.model.Order', { extend: 'App.model.Base', fields: [ 'name' ], hasMany: 'OrderItem' });Sandbox Association example There is ‘one to many’ association example. Let's play with it in the sandbox. Next => Store
Ext.define('App.model.PostStore', { extend: 'Ext.data.Store', model: 'App.model.PostModel', storeId: 'PostStore', autoLoad: true, pageSize: 20, proxy: { type: 'apprest', url: '/api/posts' }, listeners: { load: 'onStoreLoad' } });Store example Look at example of storage. As you can see it has a model and some options. It supports data pagination out of the box. Next => Application architecture
or
What is better?
Application architecture Next part of ExtJs is application architecture entities. In 4th version support MVC was added and in 5th version they added MVVM. What is better? Next => MVCExt.define('App.root.RootCtrl', { extend: 'Ext.app.ViewController', routes : { ':area/:verb/:id' : { action: 'onNavigateDetails', before: 'beforeNavigateDetails' } }, onNavigateDetails: function (area, verb, id) { // update viewport by route parameters }, beforeNavigateDetails: function (area, verb, id, action) { // validate route parameters if (verb !== 'add' && verb !== 'edit') this.redirectTo('404'); action.resume(); } });Router The Router class is a new addition to Ext JS 5 that was built to make hash change interpretation very simple in a single page application. Next => Loader
Ext.Loader.setConfig({ disableCaching: false, // Disable _dc parameter enabled: true, // dynamic dependency loading paths: { 'Global': 'src/common/Global.js', 'Messages': 'src/common/Messages.js', 'Exceptions': 'src/common/Exceptions.js', 'Request': 'src/common/Request.js', 'App.common.proxy.AppRest': 'src/common/AppRestProxy.js' } });Loader Loader used for resolving object dependencies based on 'require' section. Next => Sencha CMD