Code in this post can be obsolete, however, principles and theory may still apply.
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: http://www.gnu.org/licenses/lgpl.html */ // 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
I'm a well seasoned developer, consultant and educator of web applications based mainly on Sencha libraries, PHP, MySQL and Node.js. Besides (Apple) computers, I love photography and mountain biking.
Follow me:
Latest posts by saki (see all)
- Ext, Angular, React, and Vue - 27. June 2019
- The Site Resurgence - 11. February 2018
- Configuring ViewModel Hierarchy - 19. June 2015
3 Responses
is there any particular reason you dont like factory functions?? what you prefer instead ? excellent blog
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”.
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!!