ModelManager.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  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; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  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. 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 Ext#create Ext.create} method. Ext.create replaces
  24. the deprecated {@link #create Ext.ModelManager.create} method. It is also possible to create a model instance
  25. this by using the Model type directly. The following 3 snippets are equivalent:
  26. Ext.define('User', {
  27. extend: 'Ext.data.Model',
  28. fields: ['first', 'last']
  29. });
  30. // method 1, create using Ext.create (recommended)
  31. Ext.create('User', {
  32. first: 'Ed',
  33. last: 'Spencer'
  34. });
  35. // method 2, create through the manager (deprecated)
  36. Ext.ModelManager.create({
  37. first: 'Ed',
  38. last: 'Spencer'
  39. }, 'User');
  40. // method 3, create on the type directly
  41. new User({
  42. first: 'Ed',
  43. last: 'Spencer'
  44. });
  45. __Accessing Model Types__
  46. A reference to a Model type can be obtained by using the {@link #getModel} function. Since models types
  47. are normal classes, you can access the type directly. The following snippets are equivalent:
  48. Ext.define('User', {
  49. extend: 'Ext.data.Model',
  50. fields: ['first', 'last']
  51. });
  52. // method 1, access model type through the manager
  53. var UserType = Ext.ModelManager.getModel('User');
  54. // method 2, reference the type directly
  55. var UserType = User;
  56. * @markdown
  57. * @singleton
  58. */
  59. Ext.define('Ext.ModelManager', {
  60. extend: 'Ext.AbstractManager',
  61. alternateClassName: 'Ext.ModelMgr',
  62. requires: ['Ext.data.association.Association'],
  63. singleton: true,
  64. typeName: 'mtype',
  65. <span id='Ext-ModelManager-property-associationStack'> /**
  66. </span> * Private stack of associations that must be created once their associated model has been defined
  67. * @property {Ext.data.association.Association[]} associationStack
  68. */
  69. associationStack: [],
  70. <span id='Ext-ModelManager-method-registerType'> /**
  71. </span> * Registers a model definition. All model plugins marked with isDefault: true are bootstrapped
  72. * immediately, as are any addition plugins defined in the model config.
  73. * @private
  74. */
  75. registerType: function(name, config) {
  76. var proto = config.prototype,
  77. model;
  78. if (proto &amp;&amp; proto.isModel) {
  79. // registering an already defined model
  80. model = config;
  81. } else {
  82. // passing in a configuration
  83. if (!config.extend) {
  84. config.extend = 'Ext.data.Model';
  85. }
  86. model = Ext.define(name, config);
  87. }
  88. this.types[name] = model;
  89. return model;
  90. },
  91. <span id='Ext-ModelManager-method-onModelDefined'> /**
  92. </span> * @private
  93. * Private callback called whenever a model has just been defined. This sets up any associations
  94. * that were waiting for the given model to be defined
  95. * @param {Function} model The model that was just created
  96. */
  97. onModelDefined: function(model) {
  98. var stack = this.associationStack,
  99. length = stack.length,
  100. create = [],
  101. association, i, created;
  102. for (i = 0; i &lt; length; i++) {
  103. association = stack[i];
  104. if (association.associatedModel == model.modelName) {
  105. create.push(association);
  106. }
  107. }
  108. for (i = 0, length = create.length; i &lt; length; i++) {
  109. created = create[i];
  110. this.types[created.ownerModel].prototype.associations.add(Ext.data.association.Association.create(created));
  111. Ext.Array.remove(stack, created);
  112. }
  113. },
  114. <span id='Ext-ModelManager-method-registerDeferredAssociation'> /**
  115. </span> * Registers an association where one of the models defined doesn't exist yet.
  116. * The ModelManager will check when new models are registered if it can link them
  117. * together
  118. * @private
  119. * @param {Ext.data.association.Association} association The association
  120. */
  121. registerDeferredAssociation: function(association){
  122. this.associationStack.push(association);
  123. },
  124. <span id='Ext-ModelManager-method-getModel'> /**
  125. </span> * Returns the {@link Ext.data.Model} for a given model name
  126. * @param {String/Object} id The id of the model or the model instance.
  127. * @return {Ext.data.Model} a model class.
  128. */
  129. getModel: function(id) {
  130. var model = id;
  131. if (typeof model == 'string') {
  132. model = this.types[model];
  133. }
  134. return model;
  135. },
  136. <span id='Ext-ModelManager-method-create'> /**
  137. </span> * Creates a new instance of a Model using the given data. Deprecated, instead use Ext.create:
  138. *
  139. * Ext.create('User', {
  140. * first: 'Ed',
  141. * last: 'Spencer'
  142. * });
  143. *
  144. * @deprecated 4.1 Use {@link Ext#create Ext.create} instead.
  145. *
  146. * @param {Object} data Data to initialize the Model's fields with
  147. * @param {String} name The name of the model to create
  148. * @param {Number} id (Optional) unique id of the Model instance (see {@link Ext.data.Model})
  149. */
  150. create: function(config, name, id) {
  151. var Con = typeof name == 'function' ? name : this.types[name || config.name];
  152. return new Con(config, id);
  153. }
  154. }, function() {
  155. <span id='Ext-method-regModel'> /**
  156. </span> * Old way for creating Model classes. Instead use:
  157. *
  158. * Ext.define(&quot;MyModel&quot;, {
  159. * extend: &quot;Ext.data.Model&quot;,
  160. * fields: []
  161. * });
  162. *
  163. * @param {String} name Name of the Model class.
  164. * @param {Object} config A configuration object for the Model you wish to create.
  165. * @return {Ext.data.Model} The newly registered Model
  166. * @member Ext
  167. * @deprecated 4.0.0 Use {@link Ext#define} instead.
  168. */
  169. Ext.regModel = function() {
  170. //&lt;debug&gt;
  171. if (Ext.isDefined(Ext.global.console)) {
  172. Ext.global.console.warn('Ext.regModel has been deprecated. Models can now be created by extending Ext.data.Model: Ext.define(&quot;MyModel&quot;, {extend: &quot;Ext.data.Model&quot;, fields: []});.');
  173. }
  174. //&lt;/debug&gt;
  175. return this.ModelManager.registerType.apply(this.ModelManager, arguments);
  176. };
  177. });
  178. </pre>
  179. </body>
  180. </html>