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-data-IdGenerator'>/**
19 </span> * @author Don Griffin
21 * This class is a base for all id generators. It also provides lookup of id generators by
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.
29 * # Identity, Type and Shared IdGenerators
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:
37 * Ext.define('MyApp.data.MyModelA', {
38 * extend: 'Ext.data.Model',
45 * Ext.define('MyApp.data.MyModelB', {
46 * extend: 'Ext.data.Model',
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:
58 * Ext.define('MyApp.data.MyModelX', {
59 * extend: 'Ext.data.Model',
63 * Ext.define('MyApp.data.MyModelY', {
64 * extend: 'Ext.data.Model',
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.
71 * # Creating Custom Generators
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
77 * To register an id generator type, a derived class should provide an `alias` like so:
79 * Ext.define('MyApp.data.CustomIdGenerator', {
80 * extend: 'Ext.data.IdGenerator',
81 * alias: 'idgen.custom',
83 * configProp: 42, // some config property w/default value
85 * generate: function () {
86 * return ... // a new id
90 * Using the custom id generator is then straightforward:
92 * Ext.define('MyApp.data.MyModel', {
93 * extend: 'Ext.data.Model',
98 * Ext.define('MyApp.data.MyModel', {
99 * extend: 'Ext.data.Model',
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.
110 * Ext.define('MyApp.data.CustomIdGenerator', {
111 * extend: 'Ext.data.SequentialIdGenerator',
112 * alias: 'idgen.custom',
114 * id: 'custom', // shared by default
120 * Ext.define('MyApp.data.MyModelX', {
121 * extend: 'Ext.data.Model',
125 * Ext.define('MyApp.data.MyModelY', {
126 * extend: 'Ext.data.Model',
130 * // the above models share a generator that produces ID_1000, ID_1001, etc..
133 Ext.define('Ext.data.IdGenerator', {
137 <span id='Ext-data-IdGenerator-method-constructor'> /**
138 </span> * Initializes a new instance.
139 * @param {Object} config (optional) Configuration object to be applied to the new instance.
141 constructor: function(config) {
144 Ext.apply(me, config);
147 Ext.data.IdGenerator.all[me.id] = me;
151 <span id='Ext-data-IdGenerator-cfg-id'> /**
152 </span> * @cfg {String} id
153 * The id by which to register a new instance. This instance can be found using the
154 * {@link Ext.data.IdGenerator#get} static method.
157 getRecId: function (rec) {
158 return rec.modelName + '-' + rec.internalId;
161 <span id='Ext-data-IdGenerator-method-generate'> /**
162 </span> * Generates and returns the next id. This method must be implemented by the derived
165 * @return {String} The next id.
171 <span id='Ext-data-IdGenerator-static-property-all'> /**
172 </span> * @property {Object} all
173 * This object is keyed by id to lookup instances.
179 <span id='Ext-data-IdGenerator-static-method-get'> /**
180 </span> * Returns the IdGenerator given its config description.
181 * @param {String/Object} config If this parameter is an IdGenerator instance, it is
182 * simply returned. If this is a string, it is first used as an id for lookup and
183 * then, if there is no match, as a type to create a new instance. This parameter
184 * can also be a config object that contains a `type` property (among others) that
185 * are used to create and configure the instance.
188 get: function (config) {
193 if (typeof config == 'string') {
196 } else if (config.isGenerator) {
199 id = config.id || config.type;
203 generator = this.all[id];
205 generator = Ext.create('idgen.' + type, config);