Blog.

Ext Extension with Factory Functions File Pattern

Code in this post can be obsolete, however, principles and theory may still apply.
See also:

// vim: ts=4:sw=4:nu:fdc=2:nospell
/*global Ext:true, AbstractPanel:true */
/*jslint browser:true, laxbreak:true */

// create namespace
Ext.ns('AbstractPanel');

/**
 * @class AbstractPanel
 * @extends Ext.Panel
 *
 * AbstractPanel File Pattern
 *
 * @author    Ing. Jozef Sakáloš
 * @copyright (c) 2010, Ing. Jozef Sakáloš
 * @version   1.0
 * @revision  $Id$
 * @depends   
 *
 * @see       http://tdg-i.com/364/abstract-classes-with-ext-js
 * @see       http://extjs-eu.omni8.net/writing-a-big-application-in-ext/
 * @see       http://extjs-eu.omni8.net/factory-functions-in-ext-extensions/
 *
 * @license   This file is released under the
 * <a href="http://www.gnu.org/licenses/gpl.html" target="_blank" data-mce-href="http://www.gnu.org/licenses/gpl.html">GNU GPL 3.0</a>
 * license. It’s free for use in GPL and GPL compatible open source software, 
 * but if you want to use the component in a commercial software (closed source),
 * you have to get a commercial license.
 */
AbstractPanel = Ext.extend(Ext.Panel, {

    // default options - can be overridden on instantiation
    // Do NOT put arrays or objects here
     border:false

    // {{{
    // private
    ,initComponent:function() {

        // create config object
        var config = {};

        // build config
        this.buildConfig(config);

        // apply config
        Ext.apply(this, Ext.apply(this.initialConfig, config));

        // call parent
        AbstractPanel.superclass.initComponent.call(this);

    } // eo function initComponent
    // }}}
    // {{{
    /**
     * Builds the config object
     * @param {Object} config The config object is passed here
     * from initComponent by reference. Do not create or return
     * a new config object, add to the passed one instead.
     *
     * You can override this function if you need to customize it
     * or you can override individual build functions called.
     */
    ,buildConfig:function(config) {
        this.buildItems(config);
        this.buildButtons(config);
        this.buildTbar(config);
        this.buildBbar(config);
    } // eo function buildConfig
    // }}}
    // {{{
    /**
     * Builds items
     * @param {Object} config The config object is passed here
     * from buildConfig by reference. Do not create or return
     * a new config object, add to the passed one instead.
     *
     * You can override this function if you need to customize it.
     */
    ,buildItems:function(config) {
        config.items = undefined;
    } // eo function buildItems
    // }}}
    // {{{
    /**
     * Builds buttons
     * @param {Object} config The config object is passed here
     * from buildConfig by reference. Do not create or return
     * a new config object, add to the passed one instead.
     *
     * You can override this function if you need to customize it.
     */
    ,buildButtons:function(config) {
        config.buttons = undefined;
    } // eo function buildButtons
    // }}}
    // {{{
    /**
     * Builds top toolbar and its items
     * @param {Object} config The config object is passed here
     * from buildConfig by reference. Do not create or return
     * a new config object, add to the passed one instead.
     *
     * You can override this function if you need to customize it.
     */
    ,buildTbar:function(config) {
        config.tbar = undefined;
    } // eo function buildTbar
    // }}}
    // {{{
    /**
     * Builds bottom toolbar and its items
     * @param {Object} config The config object is passed here
     * from buildConfig by reference. Do not create or return
     * a new config object, add to the passed one instead.
     *
     * You can override this function if you need to customize it.
     */
    ,buildBbar:function(config) {
        config.bbar = undefined;
    } // eo function buildBbar
    // }}}

}); // eo extend

// eof
saki
Follow me:
Latest posts by saki (see all)

9 Responses

  1. Sorry for the delay, I forget about this post.

    The code I created comes from gathering some know how and doing some testing.

    The idea is the Ext.extend has either
    – Superclass, overrides
    – Constructor, superclass, overrides

    Either constructor or overrides could be a clousure with their own local variables.

    The only thing I couldn’t emulate properly is the static methods.

    “getCount” is visible in each instance, and should be visible in the object that holds the definition of the class.

    I will check Crockford words in this, but at first sight seems that the ext class flow and the prototype chain ways has a grey area in that issue.

    Back to the factory.
    The idea of building without mistake is clearly needed, my quote point to the fact that in each subclassing we add props and methods to the object, that’s a limitation of the current js implementation, and my quote was only in an attempt to clear the DOM a little.

    Here is the code.

    SampleClass = function(){

    // static
    var count = 0;

    return {

    getCount : function(){
    return count;
    },

    // flaw: won\'t find privateValue as defined inside constructor.
    getPriv2 : function(){
    return privateValue;
    },

    constructor: function(){

    count++;

    // prop, setter, getter will work as they are in the same clousure.
    var privateValue = \'Mary\';

    this.getPriv = function(){
    return privateValue;
    }

    this.setPriv = function(v){
    privateValue = v;
    }

    SampleClass.superclass.constructor.call(this);
    }
    }

    }

    SampleClass = Ext.extend(Ext.Panel,SampleClass());

    var t1 = new SampleClass();
    var t2 = new SampleClass();
    t1.getPriv();
    t2.getPriv();
    t2.setPriv(\'lulu\');
    t1.getPriv();
    t2.getPriv();

    The example is purely academic, we have flaws here and we need to put those things to use.

  2. Oh I see what you mean. Well this could be used for static content, specialy for a factory pattern.
    But ok, I didn’t get that this comment was this warning. thanks

  3. about this, in every method that we access to the cofig object we could modify it, I think that´s a little dangerous and kind of messy because if we want we can modify the config object in every abstract method we override,

    maybe to prevent this we should do

    _getConfig:function(config){
    config.items = this.getItems();
    config.buttons = this.getButtons();
    config.tbar = this.getTbar();
    config.bbar = this.getBbar();
    },

  4. @Andrés, build function(s) are public on purpose. Developers can override them as they want.

    BTW, I’d be quite interested in your code with the private part…

    Saki

  5. Thanks saki, amazing work here.

    I would keep build function Private by using a clousure as overrides object.

    This way we could keep object api clean.

    Despite the little dom cleanup you get a more clean documentation doing this.

  6. I’m sorry for this newb question but,
    why

    // Do NOT put arrays or objects here

    in the default options?
    Is it relative to the extend method (this function is hard to read)?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your username and password to log into your account. Don't have an account? Sign up.

Want to collaborate on an upcoming project?