Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / ComponentMgr.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.ComponentMgr"></div>/**
9  * @class Ext.ComponentMgr
10  * <p>Provides a registry of all Components (instances of {@link Ext.Component} or any subclass
11  * thereof) on a page so that they can be easily accessed by {@link Ext.Component component}
12  * {@link Ext.Component#id id} (see {@link #get}, or the convenience method {@link Ext#getCmp Ext.getCmp}).</p>
13  * <p>This object also provides a registry of available Component <i>classes</i>
14  * indexed by a mnemonic code known as the Component's {@link Ext.Component#xtype xtype}.
15  * The <tt>{@link Ext.Component#xtype xtype}</tt> provides a way to avoid instantiating child Components
16  * when creating a full, nested config object for a complete Ext page.</p>
17  * <p>A child Component may be specified simply as a <i>config object</i>
18  * as long as the correct <tt>{@link Ext.Component#xtype xtype}</tt> is specified so that if and when the Component
19  * needs rendering, the correct type can be looked up for lazy instantiation.</p>
20  * <p>For a list of all available <tt>{@link Ext.Component#xtype xtypes}</tt>, see {@link Ext.Component}.</p>
21  * @singleton
22  */
23 Ext.ComponentMgr = function(){
24     var all = new Ext.util.MixedCollection();
25     var types = {};
26     var ptypes = {};
27
28     return {
29         <div id="method-Ext.ComponentMgr-register"></div>/**
30          * Registers a component.
31          * @param {Ext.Component} c The component
32          */
33         register : function(c){
34             all.add(c);
35         },
36
37         <div id="method-Ext.ComponentMgr-unregister"></div>/**
38          * Unregisters a component.
39          * @param {Ext.Component} c The component
40          */
41         unregister : function(c){
42             all.remove(c);
43         },
44
45         <div id="method-Ext.ComponentMgr-get"></div>/**
46          * Returns a component by {@link Ext.Component#id id}.
47          * For additional details see {@link Ext.util.MixedCollection#get}.
48          * @param {String} id The component {@link Ext.Component#id id}
49          * @return Ext.Component The Component, <tt>undefined</tt> if not found, or <tt>null</tt> if a
50          * Class was found.
51          */
52         get : function(id){
53             return all.get(id);
54         },
55
56         <div id="method-Ext.ComponentMgr-onAvailable"></div>/**
57          * Registers a function that will be called when a specified component is added to ComponentMgr
58          * @param {String} id The component {@link Ext.Component#id id}
59          * @param {Function} fn The callback function
60          * @param {Object} scope The scope of the callback
61          */
62         onAvailable : function(id, fn, scope){
63             all.on("add", function(index, o){
64                 if(o.id == id){
65                     fn.call(scope || o, o);
66                     all.un("add", fn, scope);
67                 }
68             });
69         },
70
71         <div id="prop-Ext.ComponentMgr-all"></div>/**
72          * The MixedCollection used internally for the component cache. An example usage may be subscribing to
73          * events on the MixedCollection to monitor addition or removal.  Read-only.
74          * @type {MixedCollection}
75          */
76         all : all,
77         
78         <div id="method-Ext.ComponentMgr-isRegistered"></div>/**
79          * Checks if a Component type is registered.
80          * @param {Ext.Component} xtype The mnemonic string by which the Component class may be looked up
81          * @return {Boolean} Whether the type is registered.
82          */
83         isRegistered : function(xtype){
84             return types[xtype] !== undefined;    
85         },
86
87         <div id="method-Ext.ComponentMgr-registerType"></div>/**
88          * <p>Registers a new Component constructor, keyed by a new
89          * {@link Ext.Component#xtype}.</p>
90          * <p>Use this method (or its alias {@link Ext#reg Ext.reg}) to register new
91          * subclasses of {@link Ext.Component} so that lazy instantiation may be used when specifying
92          * child Components.
93          * see {@link Ext.Container#items}</p>
94          * @param {String} xtype The mnemonic string by which the Component class may be looked up.
95          * @param {Constructor} cls The new Component class.
96          */
97         registerType : function(xtype, cls){
98             types[xtype] = cls;
99             cls.xtype = xtype;
100         },
101
102         <div id="method-Ext.ComponentMgr-create"></div>/**
103          * Creates a new Component from the specified config object using the
104          * config object's {@link Ext.component#xtype xtype} to determine the class to instantiate.
105          * @param {Object} config A configuration object for the Component you wish to create.
106          * @param {Constructor} defaultType The constructor to provide the default Component type if
107          * the config object does not contain a <tt>xtype</tt>. (Optional if the config contains a <tt>xtype</tt>).
108          * @return {Ext.Component} The newly instantiated Component.
109          */
110         create : function(config, defaultType){
111             return config.render ? config : new types[config.xtype || defaultType](config);
112         },
113
114         <div id="method-Ext.ComponentMgr-registerPlugin"></div>/**
115          * <p>Registers a new Plugin constructor, keyed by a new
116          * {@link Ext.Component#ptype}.</p>
117          * <p>Use this method (or its alias {@link Ext#preg Ext.preg}) to register new
118          * plugins for {@link Ext.Component}s so that lazy instantiation may be used when specifying
119          * Plugins.</p>
120          * @param {String} ptype The mnemonic string by which the Plugin class may be looked up.
121          * @param {Constructor} cls The new Plugin class.
122          */
123         registerPlugin : function(ptype, cls){
124             ptypes[ptype] = cls;
125             cls.ptype = ptype;
126         },
127
128         <div id="method-Ext.ComponentMgr-createPlugin"></div>/**
129          * Creates a new Plugin from the specified config object using the
130          * config object's {@link Ext.component#ptype ptype} to determine the class to instantiate.
131          * @param {Object} config A configuration object for the Plugin you wish to create.
132          * @param {Constructor} defaultType The constructor to provide the default Plugin type if
133          * the config object does not contain a <tt>ptype</tt>. (Optional if the config contains a <tt>ptype</tt>).
134          * @return {Ext.Component} The newly instantiated Plugin.
135          */
136         createPlugin : function(config, defaultType){
137             return new ptypes[config.ptype || defaultType](config);
138         }
139     };
140 }();
141
142 <div id="method-Ext-reg"></div>/**
143  * Shorthand for {@link Ext.ComponentMgr#registerType}
144  * @param {String} xtype The {@link Ext.component#xtype mnemonic string} by which the Component class
145  * may be looked up.
146  * @param {Constructor} cls The new Component class.
147  * @member Ext
148  * @method reg
149  */
150 Ext.reg = Ext.ComponentMgr.registerType; // this will be called a lot internally, shorthand to keep the bytes down
151 <div id="method-Ext-preg"></div>/**
152  * Shorthand for {@link Ext.ComponentMgr#registerPlugin}
153  * @param {String} ptype The {@link Ext.component#ptype mnemonic string} by which the Plugin class
154  * may be looked up.
155  * @param {Constructor} cls The new Plugin class.
156  * @member Ext
157  * @method preg
158  */
159 Ext.preg = Ext.ComponentMgr.registerPlugin;
160 Ext.create = Ext.ComponentMgr.create;
161 </pre>    \r
162 </body>\r
163 </html>