Factory Function File Pattern

Code in this post can be obsolete, however, principles and theory may still apply.
You know, I’m not very big fan of factory functions, nevertheless, I’m aware of the fact that they may be necessary in some situations. Here is the file pattern that works (briefly tested).

Keep each factory function in a separate file name of which should be Namespace.Factory.functionName.js

// vim: ts=4:sw=4:nu:fdc=4:nospell
/*global Ext, MyNamespace */
 * @class MyNamespace.Factory
 * A Factory function pattern
 * @author    Ing. Jozef Sakáloš
 * @copyright (c) 2009, by Ing. Jozef Sakáloš
 * @date      17. March 2009
 * @version   0.1
 * @revision  $Id$
 * @license MyNamespace.Factory.myPanel.js is licensed under the terms of
 * the Open Source LGPL 3.0 license. Commercial use is permitted to the extent
 * that the code/component(s) do NOT become part of another Open Source or 
 * Commercially licensed development library or toolkit 
 * without explicit permission.
 * License details:

// create namespace

 * @method myPanel
 * @param {Object} config
 * A config object
 * @return {Ext.Panel}
MyNamespace.Factory.myPanel = function(config) {

	// pre-instantiation code
	var defaults = {
		// put your defaults here
		// but avoid id, el, contentEl, renderTo, applyTo, or similar
	}; // eo defaults object

	// create config object
	var cfg = Ext.apply({}, config, defaults);

	// instantiate
	var cmp = new Ext.Panel(cfg);

	// post-instantiation code

	// return the created component
	return cmp;

} // eo function MyNamespace.Factory.myPanel

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

3 Responses

  1. You can think about it like this:

    – defaults are applied first
    – then config

    So, if property1=”John” in defaults and property1=”Mary” in config the target will result in having property1=”Mary”.

  2. Saki – a question here.

    Ext.apply({}, config, defaults);

    Does that apply in order? Meaning, if I wanted to override some of my defaults, would I not want to reverse config and defaults?

    Ext.apply({}, defaults, config);

    Thanks for putting this together!!

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?