Factory Function File Pattern

17. March 2009 – 15:50

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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.
 * 
 * <p>License details: <a href="http://www.gnu.org/licenses/lgpl.html"
 * target="_blank">http://www.gnu.org/licenses/lgpl.html</a></p>
 */
 
// create namespace
Ext.ns('MyNamespace.Factory');
 
/**
 * @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

StumbleUpon Toolbar
  1. 3 Responses to “Factory Function File Pattern”

  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!!

    By ckr on Jul 27, 2009

  3. 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”.

    By Saki on Aug 2, 2009

  4. is there any particular reason you dont like factory functions?? what you prefer instead ? excellent blog

    By omar on Feb 23, 2011

Post a Comment