You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
18 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-Record-method-constructor'><span id='Ext-data-Record'>/**
  19. </span></span> * @class Ext.data.Record
  20. * &lt;p&gt;Instances of this class encapsulate both Record &lt;em&gt;definition&lt;/em&gt; information, and Record
  21. * &lt;em&gt;value&lt;/em&gt; information for use in {@link Ext.data.Store} objects, or any code which needs
  22. * to access Records cached in an {@link Ext.data.Store} object.&lt;/p&gt;
  23. * &lt;p&gt;Constructors for this class are generated by passing an Array of field definition objects to {@link #create}.
  24. * Instances are usually only created by {@link Ext.data.Reader} implementations when processing unformatted data
  25. * objects.&lt;/p&gt;
  26. * &lt;p&gt;Note that an instance of a Record class may only belong to one {@link Ext.data.Store Store} at a time.
  27. * In order to copy data from one Store to another, use the {@link #copy} method to create an exact
  28. * copy of the Record, and insert the new instance into the other Store.&lt;/p&gt;
  29. * &lt;p&gt;When serializing a Record for submission to the server, be aware that it contains many private
  30. * properties, and also a reference to its owning Store which in turn holds references to its Records.
  31. * This means that a whole Record may not be encoded using {@link Ext.util.JSON.encode}. Instead, use the
  32. * &lt;code&gt;{@link #data}&lt;/code&gt; and &lt;code&gt;{@link #id}&lt;/code&gt; properties.&lt;/p&gt;
  33. * &lt;p&gt;Record objects generated by this constructor inherit all the methods of Ext.data.Record listed below.&lt;/p&gt;
  34. * @constructor
  35. * &lt;p&gt;This constructor should not be used to create Record objects. Instead, use {@link #create} to
  36. * generate a subclass of Ext.data.Record configured with information about its constituent fields.&lt;p&gt;
  37. * &lt;p&gt;&lt;b&gt;The generated constructor has the same signature as this constructor.&lt;/b&gt;&lt;/p&gt;
  38. * @param {Object} data (Optional) An object, the properties of which provide values for the new Record's
  39. * fields. If not specified the &lt;code&gt;{@link Ext.data.Field#defaultValue defaultValue}&lt;/code&gt;
  40. * for each field will be assigned.
  41. * @param {Object} id (Optional) The id of the Record. The id is used by the
  42. * {@link Ext.data.Store} object which owns the Record to index its collection
  43. * of Records (therefore this id should be unique within each store). If an
  44. * &lt;code&gt;id&lt;/code&gt; is not specified a &lt;b&gt;&lt;code&gt;{@link #phantom}&lt;/code&gt;&lt;/b&gt;
  45. * Record will be created with an {@link #Record.id automatically generated id}.
  46. */
  47. Ext.data.Record = function(data, id){
  48. // if no id, call the auto id method
  49. this.id = (id || id === 0) ? id : Ext.data.Record.id(this);
  50. this.data = data || {};
  51. };
  52. <span id='Ext-data-Record-static-method-create'>/**
  53. </span> * Generate a constructor for a specific Record layout.
  54. * @param {Array} o An Array of &lt;b&gt;{@link Ext.data.Field Field}&lt;/b&gt; definition objects.
  55. * The constructor generated by this method may be used to create new Record instances. The data
  56. * object must contain properties named after the {@link Ext.data.Field field}
  57. * &lt;b&gt;&lt;tt&gt;{@link Ext.data.Field#name}s&lt;/tt&gt;&lt;/b&gt;. Example usage:&lt;pre&gt;&lt;code&gt;
  58. // create a Record constructor from a description of the fields
  59. var TopicRecord = Ext.data.Record.create([ // creates a subclass of Ext.data.Record
  60. {{@link Ext.data.Field#name name}: 'title', {@link Ext.data.Field#mapping mapping}: 'topic_title'},
  61. {name: 'author', mapping: 'username', allowBlank: false},
  62. {name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
  63. {name: 'lastPost', mapping: 'post_time', type: 'date'},
  64. {name: 'lastPoster', mapping: 'user2'},
  65. {name: 'excerpt', mapping: 'post_text', allowBlank: false},
  66. // In the simplest case, if no properties other than &lt;tt&gt;name&lt;/tt&gt; are required,
  67. // a field definition may consist of just a String for the field name.
  68. 'signature'
  69. ]);
  70. // create Record instance
  71. var myNewRecord = new TopicRecord(
  72. {
  73. title: 'Do my job please',
  74. author: 'noobie',
  75. totalPosts: 1,
  76. lastPost: new Date(),
  77. lastPoster: 'Animal',
  78. excerpt: 'No way dude!',
  79. signature: ''
  80. },
  81. id // optionally specify the id of the record otherwise {@link #Record.id one is auto-assigned}
  82. );
  83. myStore.{@link Ext.data.Store#add add}(myNewRecord);
  84. &lt;/code&gt;&lt;/pre&gt;
  85. * @method create
  86. * @return {Function} A constructor which is used to create new Records according
  87. * to the definition. The constructor has the same signature as {@link #Record}.
  88. * @static
  89. */
  90. Ext.data.Record.create = function(o){
  91. var f = Ext.extend(Ext.data.Record, {});
  92. var p = f.prototype;
  93. p.fields = new Ext.util.MixedCollection(false, function(field){
  94. return field.name;
  95. });
  96. for(var i = 0, len = o.length; i &lt; len; i++){
  97. p.fields.add(new Ext.data.Field(o[i]));
  98. }
  99. f.getField = function(name){
  100. return p.fields.get(name);
  101. };
  102. return f;
  103. };
  104. Ext.data.Record.PREFIX = 'ext-record';
  105. Ext.data.Record.AUTO_ID = 1;
  106. Ext.data.Record.EDIT = 'edit';
  107. Ext.data.Record.REJECT = 'reject';
  108. Ext.data.Record.COMMIT = 'commit';
  109. <span id='Ext-data-Record-method-id'>/**
  110. </span> * Generates a sequential id. This method is typically called when a record is {@link #create}d
  111. * and {@link #Record no id has been specified}. The returned id takes the form:
  112. * &lt;tt&gt;&amp;#123;PREFIX}-&amp;#123;AUTO_ID}&lt;/tt&gt;.&lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  113. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;PREFIX&lt;/tt&gt;&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;&lt;tt&gt;Ext.data.Record.PREFIX&lt;/tt&gt;
  114. * (defaults to &lt;tt&gt;'ext-record'&lt;/tt&gt;)&lt;/p&gt;&lt;/li&gt;
  115. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;AUTO_ID&lt;/tt&gt;&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;&lt;tt&gt;Ext.data.Record.AUTO_ID&lt;/tt&gt;
  116. * (defaults to &lt;tt&gt;1&lt;/tt&gt; initially)&lt;/p&gt;&lt;/li&gt;
  117. * &lt;/ul&gt;&lt;/div&gt;
  118. * @param {Record} rec The record being created. The record does not exist, it's a {@link #phantom}.
  119. * @return {String} auto-generated string id, &lt;tt&gt;&quot;ext-record-i++'&lt;/tt&gt;;
  120. */
  121. Ext.data.Record.id = function(rec) {
  122. rec.phantom = true;
  123. return [Ext.data.Record.PREFIX, '-', Ext.data.Record.AUTO_ID++].join('');
  124. };
  125. Ext.data.Record.prototype = {
  126. <span id='Ext-data-Record-property-fields'> /**
  127. </span> * &lt;p&gt;&lt;b&gt;This property is stored in the Record definition's &lt;u&gt;prototype&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
  128. * A MixedCollection containing the defined {@link Ext.data.Field Field}s for this Record. Read-only.
  129. * @property fields
  130. * @type Ext.util.MixedCollection
  131. */
  132. <span id='Ext-data-Record-property-data'> /**
  133. </span> * An object hash representing the data for this Record. Every field name in the Record definition
  134. * is represented by a property of that name in this object. Note that unless you specified a field
  135. * with {@link Ext.data.Field#name name} &quot;id&quot; in the Record definition, this will &lt;b&gt;not&lt;/b&gt; contain
  136. * an &lt;tt&gt;id&lt;/tt&gt; property.
  137. * @property data
  138. * @type {Object}
  139. */
  140. <span id='Ext-data-Record-property-id'> /**
  141. </span> * The unique ID of the Record {@link #Record as specified at construction time}.
  142. * @property id
  143. * @type {Object}
  144. */
  145. <span id='Ext-data-Record-property-node'> /**
  146. </span> * &lt;p&gt;&lt;b&gt;Only present if this Record was created by an {@link Ext.data.XmlReader XmlReader}&lt;/b&gt;.&lt;/p&gt;
  147. * &lt;p&gt;The XML element which was the source of the data for this Record.&lt;/p&gt;
  148. * @property node
  149. * @type {XMLElement}
  150. */
  151. <span id='Ext-data-Record-property-json'> /**
  152. </span> * &lt;p&gt;&lt;b&gt;Only present if this Record was created by an {@link Ext.data.ArrayReader ArrayReader} or a {@link Ext.data.JsonReader JsonReader}&lt;/b&gt;.&lt;/p&gt;
  153. * &lt;p&gt;The Array or object which was the source of the data for this Record.&lt;/p&gt;
  154. * @property json
  155. * @type {Array|Object}
  156. */
  157. <span id='Ext-data-Record-property-dirty'> /**
  158. </span> * Readonly flag - true if this Record has been modified.
  159. * @type Boolean
  160. */
  161. dirty : false,
  162. editing : false,
  163. error : null,
  164. <span id='Ext-data-Record-property-modified'> /**
  165. </span> * This object contains a key and value storing the original values of all modified
  166. * fields or is null if no fields have been modified.
  167. * @property modified
  168. * @type {Object}
  169. */
  170. modified : null,
  171. <span id='Ext-data-Record-property-phantom'> /**
  172. </span> * &lt;tt&gt;true&lt;/tt&gt; when the record does not yet exist in a server-side database (see
  173. * {@link #markDirty}). Any record which has a real database pk set as its id property
  174. * is NOT a phantom -- it's real.
  175. * @property phantom
  176. * @type {Boolean}
  177. */
  178. phantom : false,
  179. // private
  180. join : function(store){
  181. <span id='Ext-data-Record-property-store'> /**
  182. </span> * The {@link Ext.data.Store} to which this Record belongs.
  183. * @property store
  184. * @type {Ext.data.Store}
  185. */
  186. this.store = store;
  187. },
  188. <span id='Ext-data-Record-method-set'> /**
  189. </span> * Set the {@link Ext.data.Field#name named field} to the specified value. For example:
  190. * &lt;pre&gt;&lt;code&gt;
  191. // record has a field named 'firstname'
  192. var Employee = Ext.data.Record.{@link #create}([
  193. {name: 'firstname'},
  194. ...
  195. ]);
  196. // update the 2nd record in the store:
  197. var rec = myStore.{@link Ext.data.Store#getAt getAt}(1);
  198. // set the value (shows dirty flag):
  199. rec.set('firstname', 'Betty');
  200. // commit the change (removes dirty flag):
  201. rec.{@link #commit}();
  202. // update the record in the store, bypass setting dirty flag,
  203. // and do not store the change in the {@link Ext.data.Store#getModifiedRecords modified records}
  204. rec.{@link #data}['firstname'] = 'Wilma'; // updates record, but not the view
  205. rec.{@link #commit}(); // updates the view
  206. * &lt;/code&gt;&lt;/pre&gt;
  207. * &lt;b&gt;Notes&lt;/b&gt;:&lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  208. * &lt;li&gt;If the store has a writer and &lt;code&gt;autoSave=true&lt;/code&gt;, each set()
  209. * will execute an XHR to the server.&lt;/li&gt;
  210. * &lt;li&gt;Use &lt;code&gt;{@link #beginEdit}&lt;/code&gt; to prevent the store's &lt;code&gt;update&lt;/code&gt;
  211. * event firing while using set().&lt;/li&gt;
  212. * &lt;li&gt;Use &lt;code&gt;{@link #endEdit}&lt;/code&gt; to have the store's &lt;code&gt;update&lt;/code&gt;
  213. * event fire.&lt;/li&gt;
  214. * &lt;/ul&gt;&lt;/div&gt;
  215. * @param {String} name The {@link Ext.data.Field#name name of the field} to set.
  216. * @param {String/Object/Array} value The value to set the field to.
  217. */
  218. set : function(name, value){
  219. var encode = Ext.isPrimitive(value) ? String : Ext.encode;
  220. if(encode(this.data[name]) == encode(value)) {
  221. return;
  222. }
  223. this.dirty = true;
  224. if(!this.modified){
  225. this.modified = {};
  226. }
  227. if(this.modified[name] === undefined){
  228. this.modified[name] = this.data[name];
  229. }
  230. this.data[name] = value;
  231. if(!this.editing){
  232. this.afterEdit();
  233. }
  234. },
  235. // private
  236. afterEdit : function(){
  237. if (this.store != undefined &amp;&amp; typeof this.store.afterEdit == &quot;function&quot;) {
  238. this.store.afterEdit(this);
  239. }
  240. },
  241. // private
  242. afterReject : function(){
  243. if(this.store){
  244. this.store.afterReject(this);
  245. }
  246. },
  247. // private
  248. afterCommit : function(){
  249. if(this.store){
  250. this.store.afterCommit(this);
  251. }
  252. },
  253. <span id='Ext-data-Record-method-get'> /**
  254. </span> * Get the value of the {@link Ext.data.Field#name named field}.
  255. * @param {String} name The {@link Ext.data.Field#name name of the field} to get the value of.
  256. * @return {Object} The value of the field.
  257. */
  258. get : function(name){
  259. return this.data[name];
  260. },
  261. <span id='Ext-data-Record-method-beginEdit'> /**
  262. </span> * Begin an edit. While in edit mode, no events (e.g.. the &lt;code&gt;update&lt;/code&gt; event)
  263. * are relayed to the containing store.
  264. * See also: &lt;code&gt;{@link #endEdit}&lt;/code&gt; and &lt;code&gt;{@link #cancelEdit}&lt;/code&gt;.
  265. */
  266. beginEdit : function(){
  267. this.editing = true;
  268. this.modified = this.modified || {};
  269. },
  270. <span id='Ext-data-Record-method-cancelEdit'> /**
  271. </span> * Cancels all changes made in the current edit operation.
  272. */
  273. cancelEdit : function(){
  274. this.editing = false;
  275. delete this.modified;
  276. },
  277. <span id='Ext-data-Record-method-endEdit'> /**
  278. </span> * End an edit. If any data was modified, the containing store is notified
  279. * (ie, the store's &lt;code&gt;update&lt;/code&gt; event will fire).
  280. */
  281. endEdit : function(){
  282. this.editing = false;
  283. if(this.dirty){
  284. this.afterEdit();
  285. }
  286. },
  287. <span id='Ext-data-Record-method-reject'> /**
  288. </span> * Usually called by the {@link Ext.data.Store} which owns the Record.
  289. * Rejects all changes made to the Record since either creation, or the last commit operation.
  290. * Modified fields are reverted to their original values.
  291. * &lt;p&gt;Developers should subscribe to the {@link Ext.data.Store#update} event
  292. * to have their code notified of reject operations.&lt;/p&gt;
  293. * @param {Boolean} silent (optional) True to skip notification of the owning
  294. * store of the change (defaults to false)
  295. */
  296. reject : function(silent){
  297. var m = this.modified;
  298. for(var n in m){
  299. if(typeof m[n] != &quot;function&quot;){
  300. this.data[n] = m[n];
  301. }
  302. }
  303. this.dirty = false;
  304. delete this.modified;
  305. this.editing = false;
  306. if(silent !== true){
  307. this.afterReject();
  308. }
  309. },
  310. <span id='Ext-data-Record-method-commit'> /**
  311. </span> * Usually called by the {@link Ext.data.Store} which owns the Record.
  312. * Commits all changes made to the Record since either creation, or the last commit operation.
  313. * &lt;p&gt;Developers should subscribe to the {@link Ext.data.Store#update} event
  314. * to have their code notified of commit operations.&lt;/p&gt;
  315. * @param {Boolean} silent (optional) True to skip notification of the owning
  316. * store of the change (defaults to false)
  317. */
  318. commit : function(silent){
  319. this.dirty = false;
  320. delete this.modified;
  321. this.editing = false;
  322. if(silent !== true){
  323. this.afterCommit();
  324. }
  325. },
  326. <span id='Ext-data-Record-method-getChanges'> /**
  327. </span> * Gets a hash of only the fields that have been modified since this Record was created or commited.
  328. * @return Object
  329. */
  330. getChanges : function(){
  331. var m = this.modified, cs = {};
  332. for(var n in m){
  333. if(m.hasOwnProperty(n)){
  334. cs[n] = this.data[n];
  335. }
  336. }
  337. return cs;
  338. },
  339. // private
  340. hasError : function(){
  341. return this.error !== null;
  342. },
  343. // private
  344. clearError : function(){
  345. this.error = null;
  346. },
  347. <span id='Ext-data-Record-method-copy'> /**
  348. </span> * Creates a copy (clone) of this Record.
  349. * @param {String} id (optional) A new Record id, defaults to the id
  350. * of the record being copied. See &lt;code&gt;{@link #id}&lt;/code&gt;.
  351. * To generate a phantom record with a new id use:&lt;pre&gt;&lt;code&gt;
  352. var rec = record.copy(); // clone the record
  353. Ext.data.Record.id(rec); // automatically generate a unique sequential id
  354. * &lt;/code&gt;&lt;/pre&gt;
  355. * @return {Record}
  356. */
  357. copy : function(newId) {
  358. return new this.constructor(Ext.apply({}, this.data), newId || this.id);
  359. },
  360. <span id='Ext-data-Record-method-isModified'> /**
  361. </span> * Returns &lt;tt&gt;true&lt;/tt&gt; if the passed field name has been &lt;code&gt;{@link #modified}&lt;/code&gt;
  362. * since the load or last commit.
  363. * @param {String} fieldName {@link Ext.data.Field.{@link Ext.data.Field#name}
  364. * @return {Boolean}
  365. */
  366. isModified : function(fieldName){
  367. return !!(this.modified &amp;&amp; this.modified.hasOwnProperty(fieldName));
  368. },
  369. <span id='Ext-data-Record-method-isValid'> /**
  370. </span> * By default returns &lt;tt&gt;false&lt;/tt&gt; if any {@link Ext.data.Field field} within the
  371. * record configured with &lt;tt&gt;{@link Ext.data.Field#allowBlank} = false&lt;/tt&gt; returns
  372. * &lt;tt&gt;true&lt;/tt&gt; from an {@link Ext}.{@link Ext#isEmpty isempty} test.
  373. * @return {Boolean}
  374. */
  375. isValid : function() {
  376. return this.fields.find(function(f) {
  377. return (f.allowBlank === false &amp;&amp; Ext.isEmpty(this.data[f.name])) ? true : false;
  378. },this) ? false : true;
  379. },
  380. <span id='Ext-data-Record-method-markDirty'> /**
  381. </span> * &lt;p&gt;Marks this &lt;b&gt;Record&lt;/b&gt; as &lt;code&gt;{@link #dirty}&lt;/code&gt;. This method
  382. * is used interally when adding &lt;code&gt;{@link #phantom}&lt;/code&gt; records to a
  383. * {@link Ext.data.Store#writer writer enabled store}.&lt;/p&gt;
  384. * &lt;br&gt;&lt;p&gt;Marking a record &lt;code&gt;{@link #dirty}&lt;/code&gt; causes the phantom to
  385. * be returned by {@link Ext.data.Store#getModifiedRecords} where it will
  386. * have a create action composed for it during {@link Ext.data.Store#save store save}
  387. * operations.&lt;/p&gt;
  388. */
  389. markDirty : function(){
  390. this.dirty = true;
  391. if(!this.modified){
  392. this.modified = {};
  393. }
  394. this.fields.each(function(f) {
  395. this.modified[f.name] = this.data[f.name];
  396. },this);
  397. }
  398. };
  399. </pre>
  400. </body>
  401. </html>