3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.ModelManager
18 * @extends Ext.AbstractManager
20 The ModelManager keeps track of all {@link Ext.data.Model} types defined in your application.
22 __Creating Model Instances__
23 Model instances can be created by using the {@link #create} function. It is also possible to do
24 this by using the Model type directly. The following snippets are equivalent:
27 extend: 'Ext.data.Model',
28 fields: ['first', 'last']
31 // method 1, create through the manager
32 Ext.ModelManager.create({
37 // method 2, create on the type directly
43 __Accessing Model Types__
44 A reference to a Model type can be obtained by using the {@link #getModel} function. Since models types
45 are normal classes, you can access the type directly. The following snippets are equivalent:
48 extend: 'Ext.data.Model',
49 fields: ['first', 'last']
52 // method 1, access model type through the manager
53 var UserType = Ext.ModelManager.getModel('User');
55 // method 2, reference the type directly
61 Ext.define('Ext.ModelManager', {
62 extend: 'Ext.AbstractManager',
63 alternateClassName: 'Ext.ModelMgr',
64 requires: ['Ext.data.Association'],
71 * Private stack of associations that must be created once their associated model has been defined
72 * @property associationStack
78 * Registers a model definition. All model plugins marked with isDefault: true are bootstrapped
79 * immediately, as are any addition plugins defined in the model config.
82 registerType: function(name, config) {
83 var proto = config.prototype,
85 if (proto && proto.isModel) {
86 // registering an already defined model
89 // passing in a configuration
91 config.extend = 'Ext.data.Model';
93 model = Ext.define(name, config);
95 this.types[name] = model;
101 * Private callback called whenever a model has just been defined. This sets up any associations
102 * that were waiting for the given model to be defined
103 * @param {Function} model The model that was just created
105 onModelDefined: function(model) {
106 var stack = this.associationStack,
107 length = stack.length,
109 association, i, created;
111 for (i = 0; i < length; i++) {
112 association = stack[i];
114 if (association.associatedModel == model.modelName) {
115 create.push(association);
119 for (i = 0, length = create.length; i < length; i++) {
121 this.types[created.ownerModel].prototype.associations.add(Ext.data.Association.create(created));
122 Ext.Array.remove(stack, created);
127 * Registers an association where one of the models defined doesn't exist yet.
128 * The ModelManager will check when new models are registered if it can link them
131 * @param {Ext.data.Association} association The association
133 registerDeferredAssociation: function(association){
134 this.associationStack.push(association);
138 * Returns the {@link Ext.data.Model} for a given model name
139 * @param {String/Object} id The id of the model or the model instance.
141 getModel: function(id) {
143 if (typeof model == 'string') {
144 model = this.types[model];
150 * Creates a new instance of a Model using the given data.
151 * @param {Object} data Data to initialize the Model's fields with
152 * @param {String} name The name of the model to create
153 * @param {Number} id Optional unique id of the Model instance (see {@link Ext.data.Model})
155 create: function(config, name, id) {
156 var con = typeof name == 'function' ? name : this.types[name || config.name];
158 return new con(config, id);
163 * Creates a new Model class from the specified config object. See {@link Ext.data.Model} for full examples.
165 * @param {Object} config A configuration object for the Model you wish to create.
166 * @return {Ext.data.Model} The newly registered Model
170 Ext.regModel = function() {
172 if (Ext.isDefined(Ext.global.console)) {
173 Ext.global.console.warn('Ext.regModel has been deprecated. Models can now be created by extending Ext.data.Model: Ext.define("MyModel", {extend: "Ext.data.Model", fields: []});.');
176 return this.ModelManager.registerType.apply(this.ModelManager, arguments);