ModelManager.js 5.8 KB

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