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