Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / StoreManager.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-data-StoreManager'>/**
19 </span> * @class Ext.data.StoreManager
20  * @extends Ext.util.MixedCollection
21  * &lt;p&gt;Contains a collection of all stores that are created that have an identifier.
22  * An identifier can be assigned by setting the {@link Ext.data.AbstractStore#storeId storeId} 
23  * property. When a store is in the StoreManager, it can be referred to via it's identifier:
24  * &lt;pre&gt;&lt;code&gt;
25 Ext.create('Ext.data.Store', {
26     model: 'SomeModel',
27     storeId: 'myStore'
28 });
29
30 var store = Ext.data.StoreManager.lookup('myStore');
31  * &lt;/code&gt;&lt;/pre&gt;
32  * Also note that the {@link #lookup} method is aliased to {@link Ext#getStore} for convenience.&lt;/p&gt;
33  * &lt;p&gt;
34  * If a store is registered with the StoreManager, you can also refer to the store by it's identifier when
35  * registering it with any Component that consumes data from a store:
36  * &lt;pre&gt;&lt;code&gt;
37 Ext.create('Ext.data.Store', {
38     model: 'SomeModel',
39     storeId: 'myStore'
40 });
41
42 Ext.create('Ext.view.View', {
43     store: 'myStore',
44     // other configuration here
45 });
46  * &lt;/code&gt;&lt;/pre&gt;
47  * &lt;/p&gt;
48  * @singleton
49  * @docauthor Evan Trimboli &lt;evan@sencha.com&gt;
50  * TODO: Make this an AbstractMgr
51  */
52 Ext.define('Ext.data.StoreManager', {
53     extend: 'Ext.util.MixedCollection',
54     alternateClassName: ['Ext.StoreMgr', 'Ext.data.StoreMgr', 'Ext.StoreManager'],
55     singleton: true,
56     uses: ['Ext.data.ArrayStore'],
57     
58 <span id='Ext-data-StoreManager-cfg-listeners'>    /**
59 </span>     * @cfg {Object} listeners @hide
60      */
61
62 <span id='Ext-data-StoreManager-method-register'>    /**
63 </span>     * Registers one or more Stores with the StoreManager. You do not normally need to register stores
64      * manually.  Any store initialized with a {@link Ext.data.Store#storeId} will be auto-registered. 
65      * @param {Ext.data.Store} store1 A Store instance
66      * @param {Ext.data.Store} store2 (optional)
67      * @param {Ext.data.Store} etc... (optional)
68      */
69     register : function() {
70         for (var i = 0, s; (s = arguments[i]); i++) {
71             this.add(s);
72         }
73     },
74
75 <span id='Ext-data-StoreManager-method-unregister'>    /**
76 </span>     * Unregisters one or more Stores with the StoreManager
77      * @param {String/Object} id1 The id of the Store, or a Store instance
78      * @param {String/Object} id2 (optional)
79      * @param {String/Object} etc... (optional)
80      */
81     unregister : function() {
82         for (var i = 0, s; (s = arguments[i]); i++) {
83             this.remove(this.lookup(s));
84         }
85     },
86
87 <span id='Ext-data-StoreManager-method-lookup'>    /**
88 </span>     * Gets a registered Store by id
89      * @param {String/Object} id The id of the Store, or a Store instance, or a store configuration
90      * @return {Ext.data.Store}
91      */
92     lookup : function(store) {
93         // handle the case when we are given an array or an array of arrays.
94         if (Ext.isArray(store)) {
95             var fields = ['field1'], 
96                 expand = !Ext.isArray(store[0]),
97                 data = store,
98                 i,
99                 len;
100                 
101             if(expand){
102                 data = [];
103                 for (i = 0, len = store.length; i &lt; len; ++i) {
104                     data.push([store[i]]);
105                 }
106             } else {
107                 for(i = 2, len = store[0].length; i &lt;= len; ++i){
108                     fields.push('field' + i);
109                 }
110             }
111             return Ext.create('Ext.data.ArrayStore', {
112                 data  : data,
113                 fields: fields,
114                 autoDestroy: true,
115                 autoCreated: true,
116                 expanded: expand
117             });
118         }
119         
120         if (Ext.isString(store)) {
121             // store id
122             return this.get(store);
123         } else {
124             // store instance or store config
125             return Ext.data.AbstractStore.create(store);
126         }
127     },
128
129     // getKey implementation for MixedCollection
130     getKey : function(o) {
131          return o.storeId;
132     }
133 }, function() {    
134 <span id='Ext-method-regStore'>    /**
135 </span>     * &lt;p&gt;Creates a new store for the given id and config, then registers it with the {@link Ext.data.StoreManager Store Mananger}. 
136      * Sample usage:&lt;/p&gt;
137     &lt;pre&gt;&lt;code&gt;
138     Ext.regStore('AllUsers', {
139         model: 'User'
140     });
141
142     //the store can now easily be used throughout the application
143     new Ext.List({
144         store: 'AllUsers',
145         ... other config
146     });
147     &lt;/code&gt;&lt;/pre&gt;
148      * @param {String} id The id to set on the new store
149      * @param {Object} config The store config
150      * @param {Constructor} cls The new Component class.
151      * @member Ext
152      * @method regStore
153      */
154     Ext.regStore = function(name, config) {
155         var store;
156
157         if (Ext.isObject(name)) {
158             config = name;
159         } else {
160             config.storeId = name;
161         }
162
163         if (config instanceof Ext.data.Store) {
164             store = config;
165         } else {
166             store = Ext.create('Ext.data.Store', config);
167         }
168
169         return Ext.data.StoreManager.register(store);
170     };
171
172 <span id='Ext-method-getStore'>    /**
173 </span>     * Gets a registered Store by id (shortcut to {@link Ext.data.StoreManager#lookup})
174      * @param {String/Object} id The id of the Store, or a Store instance
175      * @return {Ext.data.Store}
176      * @member Ext
177      * @method getStore
178      */
179     Ext.getStore = function(name) {
180         return Ext.data.StoreManager.lookup(name);
181     };
182 });
183 </pre>
184 </body>
185 </html>