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