Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / AbstractManager.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-AbstractManager'>/**
19 </span> * @class Ext.AbstractManager
20  * @extends Object
21  * @ignore
22  * Base Manager class
23  */
24
25 Ext.define('Ext.AbstractManager', {
26
27     /* Begin Definitions */
28
29     requires: ['Ext.util.HashMap'],
30
31     /* End Definitions */
32
33     typeName: 'type',
34
35     constructor: function(config) {
36         Ext.apply(this, config || {});
37
38 <span id='Ext-AbstractManager-property-all'>        /**
39 </span>         * Contains all of the items currently managed
40          * @property all
41          * @type Ext.util.MixedCollection
42          */
43         this.all = Ext.create('Ext.util.HashMap');
44
45         this.types = {};
46     },
47
48 <span id='Ext-AbstractManager-method-get'>    /**
49 </span>     * Returns an item by id.
50      * For additional details see {@link Ext.util.HashMap#get}.
51      * @param {String} id The id of the item
52      * @return {Mixed} The item, &lt;code&gt;undefined&lt;/code&gt; if not found.
53      */
54     get : function(id) {
55         return this.all.get(id);
56     },
57
58 <span id='Ext-AbstractManager-method-register'>    /**
59 </span>     * Registers an item to be managed
60      * @param {Mixed} item The item to register
61      */
62     register: function(item) {
63         this.all.add(item);
64     },
65
66 <span id='Ext-AbstractManager-method-unregister'>    /**
67 </span>     * Unregisters an item by removing it from this manager
68      * @param {Mixed} item The item to unregister
69      */
70     unregister: function(item) {
71         this.all.remove(item);
72     },
73
74 <span id='Ext-AbstractManager-method-registerType'>    /**
75 </span>     * &lt;p&gt;Registers a new item constructor, keyed by a type key.
76      * @param {String} type The mnemonic string by which the class may be looked up.
77      * @param {Constructor} cls The new instance class.
78      */
79     registerType : function(type, cls) {
80         this.types[type] = cls;
81         cls[this.typeName] = type;
82     },
83
84 <span id='Ext-AbstractManager-method-isRegistered'>    /**
85 </span>     * Checks if an item type is registered.
86      * @param {String} type The mnemonic string by which the class may be looked up
87      * @return {Boolean} Whether the type is registered.
88      */
89     isRegistered : function(type){
90         return this.types[type] !== undefined;
91     },
92
93 <span id='Ext-AbstractManager-method-create'>    /**
94 </span>     * Creates and returns an instance of whatever this manager manages, based on the supplied type and config object
95      * @param {Object} config The config object
96      * @param {String} defaultType If no type is discovered in the config object, we fall back to this type
97      * @return {Mixed} The instance of whatever this manager is managing
98      */
99     create: function(config, defaultType) {
100         var type        = config[this.typeName] || config.type || defaultType,
101             Constructor = this.types[type];
102
103         //&lt;debug&gt;
104         if (Constructor == undefined) {
105             Ext.Error.raise(&quot;The '&quot; + type + &quot;' type has not been registered with this manager&quot;);
106         }
107         //&lt;/debug&gt;
108
109         return new Constructor(config);
110     },
111
112 <span id='Ext-AbstractManager-method-onAvailable'>    /**
113 </span>     * Registers a function that will be called when an item with the specified id is added to the manager. This will happen on instantiation.
114      * @param {String} id The item id
115      * @param {Function} fn The callback function. Called with a single parameter, the item.
116      * @param {Object} scope The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the callback is executed. Defaults to the item.
117      */
118     onAvailable : function(id, fn, scope){
119         var all = this.all,
120             item;
121         
122         if (all.containsKey(id)) {
123             item = all.get(id);
124             fn.call(scope || item, item);
125         } else {
126             all.on('add', function(map, key, item){
127                 if (key == id) {
128                     fn.call(scope || item, item);
129                     all.un('add', fn, scope);
130                 }
131             });
132         }
133     },
134     
135 <span id='Ext-AbstractManager-method-each'>    /**
136 </span>     * Executes the specified function once for each item in the collection.
137      * Returning false from the function will cease iteration.
138      * 
139      * The paramaters passed to the function are:
140      * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
141      * &lt;li&gt;&lt;b&gt;key&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;The key of the item&lt;/p&gt;&lt;/li&gt;
142      * &lt;li&gt;&lt;b&gt;value&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The value of the item&lt;/p&gt;&lt;/li&gt;
143      * &lt;li&gt;&lt;b&gt;length&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The total number of items in the collection&lt;/p&gt;&lt;/li&gt;
144      * &lt;/ul&gt;&lt;/div&gt;
145      * @param {Object} fn The function to execute.
146      * @param {Object} scope The scope to execute in. Defaults to &lt;tt&gt;this&lt;/tt&gt;.
147      */
148     each: function(fn, scope){
149         this.all.each(fn, scope || this);    
150     },
151     
152 <span id='Ext-AbstractManager-method-getCount'>    /**
153 </span>     * Gets the number of items in the collection.
154      * @return {Number} The number of items in the collection.
155      */
156     getCount: function(){
157         return this.all.getCount();
158     }
159 });
160 </pre>
161 </body>
162 </html>