]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/data/StoreManager.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / data / StoreManager.js
CommitLineData
6527f429
DM
1/**\r
2 * Contains a collection of all stores that are created that have an identifier. An identifier can be assigned by\r
3 * setting the {@link Ext.data.AbstractStore#storeId storeId} property. When a store is in the StoreManager, it can be\r
4 * referred to via it's identifier:\r
5 *\r
6 * Ext.create('Ext.data.Store', {\r
7 * model: 'SomeModel',\r
8 * storeId: 'myStore'\r
9 * });\r
10 *\r
11 * var store = Ext.data.StoreManager.lookup('myStore');\r
12 *\r
13 * Also note that the {@link #lookup} method is aliased to {@link Ext#getStore} for convenience.\r
14 *\r
15 * If a store is registered with the StoreManager, you can also refer to the store by it's identifier when registering\r
16 * it with any Component that consumes data from a store:\r
17 *\r
18 * Ext.create('Ext.data.Store', {\r
19 * model: 'SomeModel',\r
20 * storeId: 'myStore'\r
21 * });\r
22 *\r
23 * Ext.create('Ext.view.View', {\r
24 * store: 'myStore',\r
25 * // other configuration here\r
26 * });\r
27 *\r
28 */\r
29Ext.define('Ext.data.StoreManager', {\r
30 extend: 'Ext.util.MixedCollection',\r
31 alternateClassName: [\r
32 'Ext.StoreMgr',\r
33 'Ext.data.StoreMgr',\r
34 'Ext.StoreManager'\r
35 ],\r
36\r
37 singleton: true,\r
38\r
39 requires: [\r
40 'Ext.data.ArrayStore'\r
41 ],\r
42 \r
43 /**\r
44 * @cfg {Object} listeners\r
45 * @private\r
46 */\r
47\r
48 /**\r
49 * Registers one or more Stores with the StoreManager. You do not normally need to register stores manually. Any\r
50 * store initialized with a {@link Ext.data.Store#storeId} will be auto-registered.\r
51 * @param {Ext.data.Store...} stores Any number of Store instances\r
52 */\r
53 register : function() {\r
54 for (var i = 0, s; (s = arguments[i]); i++) {\r
55 this.add(s);\r
56 }\r
57 },\r
58\r
59 /**\r
60 * Unregisters one or more Stores with the StoreManager\r
61 * @param {String/Object...} stores Any number of Store instances or ID-s\r
62 */\r
63 unregister : function() {\r
64 for (var i = 0, s; (s = arguments[i]); i++) {\r
65 this.remove(this.lookup(s));\r
66 }\r
67 },\r
68\r
69 /**\r
70 * Gets a registered Store by id\r
71 * @param {String/Object} store The id of the Store, or a Store instance, or a store configuration\r
72 * @param {String} [defaultType] The store type to create when used with store configuration and there\r
73 * is no type specified on the config.\r
74 * @return {Ext.data.Store}\r
75 */\r
76 lookup : function(store, defaultType) {\r
77 // handle the case when we are given an array or an array of arrays.\r
78 if (Ext.isArray(store)) {\r
79 var fields = ['field1'], \r
80 expand = !Ext.isArray(store[0]),\r
81 data = store,\r
82 i,\r
83 len;\r
84 \r
85 if(expand){\r
86 data = [];\r
87 for (i = 0, len = store.length; i < len; ++i) {\r
88 data.push([store[i]]);\r
89 }\r
90 } else {\r
91 for(i = 2, len = store[0].length; i <= len; ++i){\r
92 fields.push('field' + i);\r
93 }\r
94 }\r
95 return new Ext.data.ArrayStore({\r
96 data : data,\r
97 fields: fields,\r
98 autoDestroy: true,\r
99 autoCreated: true,\r
100 expanded: expand\r
101 });\r
102 }\r
103 \r
104 if (Ext.isString(store)) {\r
105 // store id\r
106 return this.get(store);\r
107 } else {\r
108 // store instance or store config\r
109 return Ext.Factory.store(store, defaultType);\r
110 }\r
111 },\r
112\r
113 // getKey implementation for MixedCollection\r
114 getKey : function(o) {\r
115 return o.storeId;\r
116 }\r
117}, function() { \r
118 /**\r
119 * Creates a new store for the given id and config, then registers it with the {@link Ext.data.StoreManager Store Manager}. \r
120 * Sample usage:\r
121 *\r
122 * Ext.regStore('AllUsers', {\r
123 * model: 'User'\r
124 * });\r
125 *\r
126 * // the store can now easily be used throughout the application\r
127 * new Ext.List({\r
128 * store: 'AllUsers',\r
129 * ... other config\r
130 * });\r
131 *\r
132 * @param {String} id The id to set on the new store\r
133 * @param {Object} config The store config\r
134 * @member Ext\r
135 * @method regStore\r
136 */\r
137 Ext.regStore = function(name, config) {\r
138 var store;\r
139\r
140 if (Ext.isObject(name)) {\r
141 config = name;\r
142 } else {\r
143 if (Ext.data.StoreManager.containsKey(name)) {\r
144 return Ext.data.StoreManager.lookup(name);\r
145 }\r
146 config.storeId = name;\r
147 }\r
148\r
149 if (config instanceof Ext.data.Store) {\r
150 store = config;\r
151 } else {\r
152 store = new Ext.data.Store(config);\r
153 }\r
154\r
155 Ext.data.StoreManager.register(store);\r
156 return store;\r
157 };\r
158\r
159 /**\r
160 * Shortcut to {@link Ext.data.StoreManager#lookup}.\r
161 * @member Ext\r
162 * @method getStore\r
163 * @inheritdoc Ext.data.StoreManager#lookup\r
164 */\r
165 Ext.getStore = function(name) {\r
166 return Ext.data.StoreManager.lookup(name);\r
167 };\r
168\r
169 // A dummy empty store with a fieldless Model defined in it.\r
170 // Just for binding to Views which are instantiated with no Store defined.\r
171 // They will be able to run and render fine, and be bound to a generated Store later.\r
172 var emptyStore = Ext.regStore('ext-empty-store', { proxy: 'memory', useModelWarning: false });\r
173\r
174 emptyStore.isEmptyStore = true;\r
175\r
176 //<debug>\r
177 emptyStore.add = emptyStore.remove = emptyStore.insert = emptyStore.loadData = function () {\r
178 Ext.raise('Cannot modify ext-empty-store');\r
179 };\r
180 //</debug>\r
181});\r