]> git.proxmox.com Git - sencha-touch.git/blob - src/src/data/ModelManager.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / data / ModelManager.js
1 /**
2 * @author Ed Spencer
3 *
4 * The ModelManager keeps track of all {@link Ext.data.Model} types defined in your application.
5 *
6 * ## Creating Model Instances
7 *
8 * Model instances can be created by using the {@link Ext.ClassManager#create Ext.create} method. Ext.create replaces
9 * the deprecated {@link #create Ext.ModelManager.create} method. It is also possible to create a model instance
10 * this by using the Model type directly. The following 3 snippets are equivalent:
11 *
12 * Ext.define('User', {
13 * extend: 'Ext.data.Model',
14 * config: {
15 * fields: ['first', 'last']
16 * }
17 * });
18 *
19 * // method 1, create using Ext.create (recommended)
20 * Ext.create('User', {
21 * first: 'Ed',
22 * last: 'Spencer'
23 * });
24 *
25 * // method 2, create on the type directly
26 * new User({
27 * first: 'Ed',
28 * last: 'Spencer'
29 * });
30 *
31 * // method 3, create through the manager (deprecated)
32 * Ext.ModelManager.create({
33 * first: 'Ed',
34 * last: 'Spencer'
35 * }, 'User');
36 *
37 * ## Accessing Model Types
38 *
39 * A reference to a Model type can be obtained by using the {@link #getModel} function. Since models types
40 * are normal classes, you can access the type directly. The following snippets are equivalent:
41 *
42 * Ext.define('User', {
43 * extend: 'Ext.data.Model',
44 * config: {
45 * fields: ['first', 'last']
46 * }
47 * });
48 *
49 * // method 1, access model type through the manager
50 * var UserType = Ext.ModelManager.getModel('User');
51 *
52 * // method 2, reference the type directly
53 * var UserType = User;
54 */
55 Ext.define('Ext.data.ModelManager', {
56 extend: 'Ext.AbstractManager',
57 alternateClassName: ['Ext.ModelMgr', 'Ext.ModelManager'],
58
59 singleton: true,
60
61 /**
62 * @property defaultProxyType
63 * The string type of the default Model Proxy.
64 * @removed 2.0.0
65 */
66
67 /**
68 * @property associationStack
69 * Private stack of associations that must be created once their associated model has been defined.
70 * @removed 2.0.0
71 */
72
73 modelNamespace: null,
74
75 /**
76 * Registers a model definition. All model plugins marked with `isDefault: true` are bootstrapped
77 * immediately, as are any addition plugins defined in the model config.
78 * @param {String} name
79 * @param {Object} config
80 * @return {Object}
81 */
82 registerType: function(name, config) {
83 var proto = config.prototype,
84 model;
85
86 if (proto && proto.isModel) {
87 // registering an already defined model
88 model = config;
89 } else {
90 config = {
91 extend: config.extend || 'Ext.data.Model',
92 config: config
93 };
94 model = Ext.define(name, config);
95 }
96 this.types[name] = model;
97 return model;
98 },
99
100 onModelDefined: Ext.emptyFn,
101
102 // /**
103 // * @private
104 // * Private callback called whenever a model has just been defined. This sets up any associations
105 // * that were waiting for the given model to be defined.
106 // * @param {Function} model The model that was just created.
107 // */
108 // onModelDefined: function(model) {
109 // var stack = this.associationStack,
110 // length = stack.length,
111 // create = [],
112 // association, i, created;
113 //
114 // for (i = 0; i < length; i++) {
115 // association = stack[i];
116 //
117 // if (association.associatedModel == model.modelName) {
118 // create.push(association);
119 // }
120 // }
121 //
122 // for (i = 0, length = create.length; i < length; i++) {
123 // created = create[i];
124 // this.types[created.ownerModel].prototype.associations.add(Ext.data.association.Association.create(created));
125 // Ext.Array.remove(stack, created);
126 // }
127 // },
128 //
129 // /**
130 // * Registers an association where one of the models defined doesn't exist yet.
131 // * The ModelManager will check when new models are registered if it can link them
132 // * together.
133 // * @private
134 // * @param {Ext.data.association.Association} association The association.
135 // */
136 // registerDeferredAssociation: function(association){
137 // this.associationStack.push(association);
138 // },
139
140 /**
141 * Returns the {@link Ext.data.Model} for a given model name.
142 * @param {String/Object} id The `id` of the model or the model instance.
143 * @return {Ext.data.Model} A model class.
144 */
145 getModel: function(id) {
146 var model = id;
147 if (typeof model == 'string') {
148 model = this.types[model];
149 if (!model && this.modelNamespace) {
150 model = this.types[this.modelNamespace + '.' + model];
151 }
152 }
153 return model;
154 },
155
156 /**
157 * Creates a new instance of a Model using the given data.
158 *
159 * __Note:__ This method is deprecated. Use {@link Ext.ClassManager#create Ext.create} instead. For example:
160 *
161 * Ext.create('User', {
162 * first: 'Ed',
163 * last: 'Spencer'
164 * });
165 *
166 * @param {Object} data Data to initialize the Model's fields with.
167 * @param {String} name The name of the model to create.
168 * @param {Number} id (optional) Unique id of the Model instance (see {@link Ext.data.Model}).
169 * @return {Object}
170 */
171 create: function(config, name, id) {
172 var con = typeof name == 'function' ? name : this.types[name || config.name];
173 return new con(config, id);
174 }
175 }, function() {
176
177 /**
178 * Old way for creating Model classes. Instead use:
179 *
180 * Ext.define("MyModel", {
181 * extend: "Ext.data.Model",
182 * fields: []
183 * });
184 *
185 * @param {String} name Name of the Model class.
186 * @param {Object} config A configuration object for the Model you wish to create.
187 * @return {Ext.data.Model} The newly registered Model.
188 * @member Ext
189 * @deprecated 2.0.0 Please use {@link Ext#define} instead.
190 */
191 Ext.regModel = function() {
192 //<debug>
193 Ext.Logger.deprecate('Ext.regModel has been deprecated. Models can now be created by ' +
194 'extending Ext.data.Model: Ext.define("MyModel", {extend: "Ext.data.Model", fields: []});.');
195 //</debug>
196 return this.ModelManager.registerType.apply(this.ModelManager, arguments);
197 };
198 });