4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-ModelManager'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.ModelManager
21 * @extends Ext.AbstractManager
23 The ModelManager keeps track of all {@link Ext.data.Model} types defined in your application.
25 __Creating Model Instances__
27 Model instances can be created by using the {@link Ext#create Ext.create} method. Ext.create replaces
28 the deprecated {@link #create Ext.ModelManager.create} method. It is also possible to create a model instance
29 this by using the Model type directly. The following 3 snippets are equivalent:
32 extend: 'Ext.data.Model',
33 fields: ['first', 'last']
36 // method 1, create using Ext.create (recommended)
42 // method 2, create through the manager (deprecated)
43 Ext.ModelManager.create({
48 // method 3, create on the type directly
54 __Accessing Model Types__
56 A reference to a Model type can be obtained by using the {@link #getModel} function. Since models types
57 are normal classes, you can access the type directly. The following snippets are equivalent:
60 extend: 'Ext.data.Model',
61 fields: ['first', 'last']
64 // method 1, access model type through the manager
65 var UserType = Ext.ModelManager.getModel('User');
67 // method 2, reference the type directly
73 Ext.define('Ext.ModelManager', {
74 extend: 'Ext.AbstractManager',
75 alternateClassName: 'Ext.ModelMgr',
76 requires: ['Ext.data.Association'],
82 <span id='Ext-ModelManager-property-associationStack'> /**
83 </span> * Private stack of associations that must be created once their associated model has been defined
84 * @property {Ext.data.Association[]} associationStack
88 <span id='Ext-ModelManager-method-registerType'> /**
89 </span> * Registers a model definition. All model plugins marked with isDefault: true are bootstrapped
90 * immediately, as are any addition plugins defined in the model config.
93 registerType: function(name, config) {
94 var proto = config.prototype,
96 if (proto && proto.isModel) {
97 // registering an already defined model
100 // passing in a configuration
101 if (!config.extend) {
102 config.extend = 'Ext.data.Model';
104 model = Ext.define(name, config);
106 this.types[name] = model;
110 <span id='Ext-ModelManager-method-onModelDefined'> /**
112 * Private callback called whenever a model has just been defined. This sets up any associations
113 * that were waiting for the given model to be defined
114 * @param {Function} model The model that was just created
116 onModelDefined: function(model) {
117 var stack = this.associationStack,
118 length = stack.length,
120 association, i, created;
122 for (i = 0; i < length; i++) {
123 association = stack[i];
125 if (association.associatedModel == model.modelName) {
126 create.push(association);
130 for (i = 0, length = create.length; i < length; i++) {
132 this.types[created.ownerModel].prototype.associations.add(Ext.data.Association.create(created));
133 Ext.Array.remove(stack, created);
137 <span id='Ext-ModelManager-method-registerDeferredAssociation'> /**
138 </span> * Registers an association where one of the models defined doesn't exist yet.
139 * The ModelManager will check when new models are registered if it can link them
142 * @param {Ext.data.Association} association The association
144 registerDeferredAssociation: function(association){
145 this.associationStack.push(association);
148 <span id='Ext-ModelManager-method-getModel'> /**
149 </span> * Returns the {@link Ext.data.Model} for a given model name
150 * @param {String/Object} id The id of the model or the model instance.
151 * @return {Ext.data.Model} a model class.
153 getModel: function(id) {
155 if (typeof model == 'string') {
156 model = this.types[model];
161 <span id='Ext-ModelManager-method-create'> /**
162 </span> * Creates a new instance of a Model using the given data.
164 * This method is deprecated. Use {@link Ext#create Ext.create} instead. For example:
166 * Ext.create('User', {
171 * @param {Object} data Data to initialize the Model's fields with
172 * @param {String} name The name of the model to create
173 * @param {Number} id (Optional) unique id of the Model instance (see {@link Ext.data.Model})
175 create: function(config, name, id) {
176 var con = typeof name == 'function' ? name : this.types[name || config.name];
178 return new con(config, id);
182 <span id='Ext-method-regModel'> /**
183 </span> * Old way for creating Model classes. Instead use:
185 * Ext.define("MyModel", {
186 * extend: "Ext.data.Model",
190 * @param {String} name Name of the Model class.
191 * @param {Object} config A configuration object for the Model you wish to create.
192 * @return {Ext.data.Model} The newly registered Model
194 * @deprecated 4.0.0 Use {@link Ext#define} instead.
196 Ext.regModel = function() {
198 if (Ext.isDefined(Ext.global.console)) {
199 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: []});.');
202 return this.ModelManager.registerType.apply(this.ModelManager, arguments);