IdGenerator.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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-data-IdGenerator'>/**
  19. </span> * @author Don Griffin
  20. *
  21. * This class is a base for all id generators. It also provides lookup of id generators by
  22. * their id.
  23. *
  24. * Generally, id generators are used to generate a primary key for new model instances. There
  25. * are different approaches to solving this problem, so this mechanism has both simple use
  26. * cases and is open to custom implementations. A {@link Ext.data.Model} requests id generation
  27. * using the {@link Ext.data.Model#idgen} property.
  28. *
  29. * # Identity, Type and Shared IdGenerators
  30. *
  31. * It is often desirable to share IdGenerators to ensure uniqueness or common configuration.
  32. * This is done by giving IdGenerator instances an id property by which they can be looked
  33. * up using the {@link #get} method. To configure two {@link Ext.data.Model Model} classes
  34. * to share one {@link Ext.data.SequentialIdGenerator sequential} id generator, you simply
  35. * assign them the same id:
  36. *
  37. * Ext.define('MyApp.data.MyModelA', {
  38. * extend: 'Ext.data.Model',
  39. * idgen: {
  40. * type: 'sequential',
  41. * id: 'foo'
  42. * }
  43. * });
  44. *
  45. * Ext.define('MyApp.data.MyModelB', {
  46. * extend: 'Ext.data.Model',
  47. * idgen: {
  48. * type: 'sequential',
  49. * id: 'foo'
  50. * }
  51. * });
  52. *
  53. * To make this as simple as possible for generator types that are shared by many (or all)
  54. * Models, the IdGenerator types (such as 'sequential' or 'uuid') are also reserved as
  55. * generator id's. This is used by the {@link Ext.data.UuidGenerator} which has an id equal
  56. * to its type ('uuid'). In other words, the following Models share the same generator:
  57. *
  58. * Ext.define('MyApp.data.MyModelX', {
  59. * extend: 'Ext.data.Model',
  60. * idgen: 'uuid'
  61. * });
  62. *
  63. * Ext.define('MyApp.data.MyModelY', {
  64. * extend: 'Ext.data.Model',
  65. * idgen: 'uuid'
  66. * });
  67. *
  68. * This can be overridden (by specifying the id explicitly), but there is no particularly
  69. * good reason to do so for this generator type.
  70. *
  71. * # Creating Custom Generators
  72. *
  73. * An id generator should derive from this class and implement the {@link #generate} method.
  74. * The constructor will apply config properties on new instances, so a constructor is often
  75. * not necessary.
  76. *
  77. * To register an id generator type, a derived class should provide an `alias` like so:
  78. *
  79. * Ext.define('MyApp.data.CustomIdGenerator', {
  80. * extend: 'Ext.data.IdGenerator',
  81. * alias: 'idgen.custom',
  82. *
  83. * configProp: 42, // some config property w/default value
  84. *
  85. * generate: function () {
  86. * return ... // a new id
  87. * }
  88. * });
  89. *
  90. * Using the custom id generator is then straightforward:
  91. *
  92. * Ext.define('MyApp.data.MyModel', {
  93. * extend: 'Ext.data.Model',
  94. * idgen: 'custom'
  95. * });
  96. * // or...
  97. *
  98. * Ext.define('MyApp.data.MyModel', {
  99. * extend: 'Ext.data.Model',
  100. * idgen: {
  101. * type: 'custom',
  102. * configProp: value
  103. * }
  104. * });
  105. *
  106. * It is not recommended to mix shared generators with generator configuration. This leads
  107. * to unpredictable results unless all configurations match (which is also redundant). In
  108. * such cases, a custom generator with a default id is the best approach.
  109. *
  110. * Ext.define('MyApp.data.CustomIdGenerator', {
  111. * extend: 'Ext.data.SequentialIdGenerator',
  112. * alias: 'idgen.custom',
  113. *
  114. * id: 'custom', // shared by default
  115. *
  116. * prefix: 'ID_',
  117. * seed: 1000
  118. * });
  119. *
  120. * Ext.define('MyApp.data.MyModelX', {
  121. * extend: 'Ext.data.Model',
  122. * idgen: 'custom'
  123. * });
  124. *
  125. * Ext.define('MyApp.data.MyModelY', {
  126. * extend: 'Ext.data.Model',
  127. * idgen: 'custom'
  128. * });
  129. *
  130. * // the above models share a generator that produces ID_1000, ID_1001, etc..
  131. *
  132. */
  133. Ext.define('Ext.data.IdGenerator', {
  134. <span id='Ext-data-IdGenerator-property-isGenerator'> /**
  135. </span> * @property {Boolean} isGenerator
  136. * `true` in this class to identify an object as an instantiated IdGenerator, or subclass thereof.
  137. */
  138. isGenerator: true,
  139. <span id='Ext-data-IdGenerator-method-constructor'> /**
  140. </span> * Initializes a new instance.
  141. * @param {Object} config (optional) Configuration object to be applied to the new instance.
  142. */
  143. constructor: function(config) {
  144. var me = this;
  145. Ext.apply(me, config);
  146. if (me.id) {
  147. Ext.data.IdGenerator.all[me.id] = me;
  148. }
  149. },
  150. <span id='Ext-data-IdGenerator-cfg-id'> /**
  151. </span> * @cfg {String} id
  152. * The id by which to register a new instance. This instance can be found using the
  153. * {@link Ext.data.IdGenerator#get} static method.
  154. */
  155. getRecId: function (rec) {
  156. return rec.modelName + '-' + rec.internalId;
  157. },
  158. <span id='Ext-data-IdGenerator-method-generate'> /**
  159. </span> * Generates and returns the next id. This method must be implemented by the derived
  160. * class.
  161. *
  162. * @return {String} The next id.
  163. * @method generate
  164. * @abstract
  165. */
  166. statics: {
  167. <span id='Ext-data-IdGenerator-static-property-all'> /**
  168. </span> * @property {Object} all
  169. * This object is keyed by id to lookup instances.
  170. * @private
  171. * @static
  172. */
  173. all: {},
  174. <span id='Ext-data-IdGenerator-static-method-get'> /**
  175. </span> * Returns the IdGenerator given its config description.
  176. * @param {String/Object} config If this parameter is an IdGenerator instance, it is
  177. * simply returned. If this is a string, it is first used as an id for lookup and
  178. * then, if there is no match, as a type to create a new instance. This parameter
  179. * can also be a config object that contains a `type` property (among others) that
  180. * are used to create and configure the instance.
  181. * @static
  182. */
  183. get: function (config) {
  184. var generator,
  185. id,
  186. type;
  187. if (typeof config == 'string') {
  188. id = type = config;
  189. config = null;
  190. } else if (config.isGenerator) {
  191. return config;
  192. } else {
  193. id = config.id || config.type;
  194. type = config.type;
  195. }
  196. generator = this.all[id];
  197. if (!generator) {
  198. generator = Ext.create('idgen.' + type, config);
  199. }
  200. return generator;
  201. }
  202. }
  203. });
  204. </pre>
  205. </body>
  206. </html>