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