]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/data/ModelManager.js
4 * The ModelManager keeps track of all {@link Ext.data.Model} types defined in your application.
6 * ## Creating Model Instances
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:
12 * Ext.define('User', {
13 * extend: 'Ext.data.Model',
15 * fields: ['first', 'last']
19 * // method 1, create using Ext.create (recommended)
20 * Ext.create('User', {
25 * // method 2, create on the type directly
31 * // method 3, create through the manager (deprecated)
32 * Ext.ModelManager.create({
37 * ## Accessing Model Types
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:
42 * Ext.define('User', {
43 * extend: 'Ext.data.Model',
45 * fields: ['first', 'last']
49 * // method 1, access model type through the manager
50 * var UserType = Ext.ModelManager.getModel('User');
52 * // method 2, reference the type directly
53 * var UserType = User;
55 Ext
.define('Ext.data.ModelManager', {
56 extend
: 'Ext.AbstractManager',
57 alternateClassName
: ['Ext.ModelMgr', 'Ext.ModelManager'],
62 * @property defaultProxyType
63 * The string type of the default Model Proxy.
68 * @property associationStack
69 * Private stack of associations that must be created once their associated model has been defined.
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
82 registerType: function(name
, config
) {
83 var proto
= config
.prototype,
86 if (proto
&& proto
.isModel
) {
87 // registering an already defined model
91 extend
: config
.extend
|| 'Ext.data.Model',
94 model
= Ext
.define(name
, config
);
96 this.types
[name
] = model
;
100 onModelDefined
: Ext
.emptyFn
,
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.
108 // onModelDefined: function(model) {
109 // var stack = this.associationStack,
110 // length = stack.length,
112 // association, i, created;
114 // for (i = 0; i < length; i++) {
115 // association = stack[i];
117 // if (association.associatedModel == model.modelName) {
118 // create.push(association);
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);
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
134 // * @param {Ext.data.association.Association} association The association.
136 // registerDeferredAssociation: function(association){
137 // this.associationStack.push(association);
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.
145 getModel: function(id
) {
147 if (typeof model
== 'string') {
148 model
= this.types
[model
];
149 if (!model
&& this.modelNamespace
) {
150 model
= this.types
[this.modelNamespace
+ '.' + model
];
157 * Creates a new instance of a Model using the given data.
159 * __Note:__ This method is deprecated. Use {@link Ext.ClassManager#create Ext.create} instead. For example:
161 * Ext.create('User', {
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}).
171 create: function(config
, name
, id
) {
172 var con
= typeof name
== 'function' ? name
: this.types
[name
|| config
.name
];
173 return new con(config
, id
);
178 * Old way for creating Model classes. Instead use:
180 * Ext.define("MyModel", {
181 * extend: "Ext.data.Model",
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.
189 * @deprecated 2.0.0 Please use {@link Ext#define} instead.
191 Ext
.regModel = function() {
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: []});.');
196 return this.ModelManager
.registerType
.apply(this.ModelManager
, arguments
);