Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Ext JS 5 Components

Mats Bryntse
December 02, 2014

Building Ext JS 5 Components

From SenchaDay 2014 in Karlsruhe

Mats Bryntse

December 02, 2014
Tweet

More Decks by Mats Bryntse

Other Decks in Technology

Transcript

  1. Intro | Content Basics Design phase Tips & Tricks •

    Ext class system • Extending a class • Component model • Choose base class • Architecture • Mixins, plugins • Destructor • Writing testable code • Avoiding private Ext code
  2. Basics | Background One of the top strengths of Ext

    JS: Mature reusable UI components.
  3. Class system | Component life cycle Ext.define("MyApp.FilterField", {
 extend :

    "Ext.form.TextField",
 
 initComponent : function() { … }, afterRender : function() { … }, onDestroy : function() { … },
 … });
  4. Class system | Advanced Ext.define("MyApp.FilterField", {
 extend : “Ext.some.Class”,
 


    onClassExtended : function() { … }, … }, function() { … }) // Class def callback
  5. Design phase | Choosing a base class or… UI Class

    Pure JS class? <div> foo </div>
  6. Design phase | Choosing a base class Non-UI class should

    extend Ext.Base or Ext.util.Observable Ext.define("MyApp.SomeClass", {
 extend : “Ext.Base” // Or omit this });
  7. Design phase | Choosing a base class For custom UI

    classes, you have more choices: Ext.Component Ext.Panel Ext.Container <div> Foo </div>
  8. Design phase | Abstractions Use the right Ext JS abstraction

    level Native DOM Ext JS “DOM Core” Ext.Component HtmlElement, doc.getElementById JS API, Ext.Element, Ext.get Sizing, life cycle Ext.Container Layout, contain other components
  9. Design phase | Configuration •Avoid too many mandatory config settings

    •Provide reasonable defaults •For reusable components: Avoid using public API configs internally cls, style, listeners
  10. Design phase | Configuration Scheduler v1.0 new Sch.panel.SchedulerGrid({
 renderTo :

    document.body, viewPreset : “dayAndWeek”, startDate : new Date(2014, 11, 1), endDate : new Date(2015, 11, 1), eventStore : new Sch.data.EventStore(), resourceStore : new Sch.data.ResourceStore(), rowHeight : 25, …
 });

  11. Design phase | Configuration Scheduler v3.0 Ext.define(“Sch.panel.SchedulerGrid”, {
 viewPreset :

    “dayAndWeek”, rowHeight : 25, initComponent : function() { Ext.applyIf(this, { startDate : new Date(), eventStore : new Sch.data.EventStore(), resourceStore : new Sch.data.ResourceStore()
 }); });
  12. Mixins & Plugins | Mixins •Mixins are just simple objects

    •Mixins make code reusable for classes •We use them extensively
  13. Mixins & Plugins | Mixins •Very simple to use! Ext.define(“My.Mixin”,

    {
 foo : “bar” });
 Ext.define(“My.awesome.Class”, {
 mixins : [“My.Mixin”] });

  14. Mixins & Plugins | Plugins •Works for any Ext.Component •Easy

    way to enable extra functionality for instances •Avoids class bloat, Ext grid is a great example •Ext JS grid has tons of “optional” features
  15. Mixins & Plugins | Plugins Row expander, cell editing, row

    editing, header drag drop, header resize, row drag drop…
  16. Mixins & Plugins | Plugins •By using mixins, the core

    part of the Grid stays manageable •If you don’t use feature X, Sencha Cmd will not include it
  17. Mixins & Plugins | Plugins Ext.define(“My.Plugin”, { extend : “Ext.AbstractPlugin”,


    init : function(component) {…} });
 new My.awesome.Class({
 plugins : [new My.Plugin()] });
 Definition: Usage:
  18. Rounding up | Tips & tricks Stay on top of

    the API as much as possible Upgrade pains to be expected otherwise If you must override a private Ext JS method, document it We detect private overrides through tests
  19. Rounding up | Tips & tricks Overriding Ext JS Ext.define("MyApp.view.UserCombo",

    {
 extend : “Ext.form.ComboBox",
 // TODO document!
 onExpand : function() {
 
 } });
 Always question these! Other solutions? Use a listener?
  20. Rounding up | Tips & tricks Forgetting to remove event

    listeners. Ext.define("MyApp.view.UserList", {
 extend : "Ext.some.Class",
 
 initComponent : function() {
 this.store.on('load', this.onLoad, this);
 }, onLoad : function() { … }
 });

  21. Rounding up | Tips & tricks Use onDestroy hook to

    remove event listeners. Or cmp.mon Ext.define("MyApp.view.UserList", {
 extend : "Ext.some.Class",
 
 initComponent : function() {
 this.store.on('load', this.onLoad, this);
 }, onDestroy : function() { this.store.un('load', this.onLoad, this);
 } });
  22. Rounding up | Testing Writing testable code Models & Stores

    are pure JS, not tied to DOM Very easy to test. Very fast. Views & controllers are much harder
  23. Rounding up | Testing Don’t forget to test Component Maker

    Test Pack on Github https://github.com/matsbryntse/Component-maker-test-pack Yes, you must test your component in IE too.