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