Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Model.html
index 596a172..fce03bc 100644 (file)
@@ -1,4 +1,21 @@
-<!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.Model-method-constructor'><span id='Ext-data.Model'>/**
+<!DOCTYPE html>
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+  <title>The source code</title>
+  <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
+  <script type="text/javascript" src="../prettify/prettify.js"></script>
+  <style type="text/css">
+    .highlight { display: block; background-color: #ddd; }
+  </style>
+  <script type="text/javascript">
+    function highlight() {
+      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
+    }
+  </script>
+</head>
+<body onload="prettyPrint(); highlight();">
+  <pre class="prettyprint lang-js"><span id='Ext-data-Model-method-constructor'><span id='Ext-data-Model'>/**
 </span></span> * @author Ed Spencer
  * @class Ext.data.Model
  *
@@ -219,7 +236,7 @@ store.load();
  *
  * @constructor
  * @param {Object} data An object containing keys corresponding to this model's fields, and their associated values
- * @param {Number} id Optional unique ID to assign to this model instance
+ * @param {Number} id (optional) Unique ID to assign to this model instance
  */
 Ext.define('Ext.data.Model', {
     alternateClassName: 'Ext.data.Record',
@@ -369,11 +386,11 @@ Ext.define('Ext.data.Model', {
                 // Fire the onModelDefined template method on ModelManager
                 Ext.ModelManager.onModelDefined(cls);
             });
-        }
+        };
     },
 
     inheritableStatics: {
-<span id='Ext-data.Model-method-setProxy'>        /**
+<span id='Ext-data-Model-method-setProxy'>        /**
 </span>         * Sets the Proxy to use for this model. Accepts any options that can be accepted by {@link Ext#createByAlias Ext.createByAlias}
          * @param {String/Object/Ext.data.proxy.Proxy} proxy The proxy
          * @static
@@ -394,7 +411,7 @@ Ext.define('Ext.data.Model', {
             return proxy;
         },
 
-<span id='Ext-data.Model-method-getProxy'>        /**
+<span id='Ext-data-Model-method-getProxy'>        /**
 </span>         * Returns the configured Proxy for this Model
          * @return {Ext.data.proxy.Proxy} The proxy
          */
@@ -402,7 +419,7 @@ Ext.define('Ext.data.Model', {
             return this.proxy;
         },
 
-<span id='Ext-data.Model-method-load'>        /**
+<span id='Ext-data-Model-method-load'>        /**
 </span>         * &lt;b&gt;Static&lt;/b&gt;. Asynchronously loads a model instance by id. Sample usage:
     &lt;pre&gt;&lt;code&gt;
     MyApp.User = Ext.define('User', {
@@ -465,7 +482,7 @@ Ext.define('Ext.data.Model', {
         REJECT : 'reject',
         COMMIT : 'commit',
 
-<span id='Ext-data.Model-method-id'>        /**
+<span id='Ext-data-Model-method-id'>        /**
 </span>         * Generates a sequential id. This method is typically called when a record is {@link #create}d
          * and {@link #Record no id has been specified}. The id will automatically be assigned
          * to the record. The returned id takes the form:
@@ -487,29 +504,29 @@ Ext.define('Ext.data.Model', {
         }
     },
     
-<span id='Ext-data.Model-property-editing'>    /**
+<span id='Ext-data-Model-property-editing'>    /**
 </span>     * Internal flag used to track whether or not the model instance is currently being edited. Read-only
      * @property editing
      * @type Boolean
      */
     editing : false,
 
-<span id='Ext-data.Model-property-dirty'>    /**
+<span id='Ext-data-Model-property-dirty'>    /**
 </span>     * Readonly flag - true if this Record has been modified.
      * @type Boolean
      */
     dirty : false,
 
-<span id='Ext-data.Model-cfg-persistanceProperty'>    /**
-</span>     * @cfg {String} persistanceProperty The property on this Persistable object that its data is saved to.
+<span id='Ext-data-Model-cfg-persistenceProperty'>    /**
+</span>     * @cfg {String} persistenceProperty The property on this Persistable object that its data is saved to.
      * Defaults to 'data' (e.g. all persistable data resides in this.data.)
      */
-    persistanceProperty: 'data',
+    persistenceProperty: 'data',
 
     evented: false,
     isModel: true,
 
-<span id='Ext-data.Model-property-phantom'>    /**
+<span id='Ext-data-Model-property-phantom'>    /**
 </span>     * &lt;tt&gt;true&lt;/tt&gt; when the record does not yet exist in a server-side database (see
      * {@link #setDirty}).  Any record which has a real database pk set as its id property
      * is NOT a phantom -- it's real.
@@ -518,25 +535,26 @@ Ext.define('Ext.data.Model', {
      */
     phantom : false,
 
-<span id='Ext-data.Model-cfg-idProperty'>    /**
+<span id='Ext-data-Model-cfg-idProperty'>    /**
 </span>     * @cfg {String} idProperty The name of the field treated as this Model's unique id (defaults to 'id').
      */
     idProperty: 'id',
 
-<span id='Ext-data.Model-property-defaultProxyType'>    /**
+<span id='Ext-data-Model-property-defaultProxyType'>    /**
 </span>     * The string type of the default Model Proxy. Defaults to 'ajax'
      * @property defaultProxyType
      * @type String
      */
     defaultProxyType: 'ajax',
 
-<span id='Ext-data.Model-property-fields'>    /**
+<span id='Ext-data-Model-property-fields'>    /**
 </span>     * An array of the fields defined on this model
      * @property fields
      * @type {Array}
      */
 
-    constructor: function(data, id) {
+    // raw not documented intentionally, meant to be used internally.
+    constructor: function(data, id, raw) {
         data = data || {};
         
         var me = this,
@@ -548,26 +566,42 @@ Ext.define('Ext.data.Model', {
             isArray = Ext.isArray(data),
             newData = isArray ? {} : null; // to hold mapped array data if needed
 
-<span id='Ext-data.Model-property-internalId'>        /**
+<span id='Ext-data-Model-property-internalId'>        /**
 </span>         * An internal unique ID for each Model instance, used to identify Models that don't have an ID yet
          * @property internalId
          * @type String
          * @private
          */
         me.internalId = (id || id === 0) ? id : Ext.data.Model.id(me);
+        
+<span id='Ext-data-Model-property-raw'>        /**
+</span>         * The raw data used to create this model if created via a reader.
+         * @property raw
+         * @type Object
+         */
+        me.raw = raw;
 
         Ext.applyIf(me, {
             data: {}    
         });
         
-<span id='Ext-data.Model-property-modified'>        /**
+<span id='Ext-data-Model-property-modified'>        /**
 </span>         * Key: value pairs of all fields whose values have changed
          * @property modified
          * @type Object
          */
         me.modified = {};
 
-        me[me.persistanceProperty] = {};
+        // Deal with spelling error in previous releases
+        if (me.persistanceProperty) {
+            //&lt;debug&gt;
+            if (Ext.isDefined(Ext.global.console)) {
+                Ext.global.console.warn('Ext.data.Model: persistanceProperty has been deprecated. Use persistenceProperty instead.');
+            }
+            //&lt;/debug&gt;
+            me.persistenceProperty = me.persistanceProperty;
+        }
+        me[me.persistenceProperty] = {};
 
         me.mixins.observable.constructor.call(me);
 
@@ -603,20 +637,18 @@ Ext.define('Ext.data.Model', {
         }
 
         me.id = me.modelName + '-' + me.internalId;
-
-        Ext.ModelManager.register(me);
     },
     
-<span id='Ext-data.Model-method-get'>    /**
+<span id='Ext-data-Model-method-get'>    /**
 </span>     * Returns the value of the given field
      * @param {String} fieldName The field to fetch the value for
      * @return {Mixed} The value
      */
     get: function(field) {
-        return this[this.persistanceProperty][field];
+        return this[this.persistenceProperty][field];
     },
     
-<span id='Ext-data.Model-method-set'>    /**
+<span id='Ext-data-Model-method-set'>    /**
 </span>     * Sets the given field to the given value, marks the instance as dirty
      * @param {String|Object} fieldName The field to set, or an object containing key/value pairs
      * @param {Mixed} value The value to set
@@ -662,11 +694,28 @@ Ext.define('Ext.data.Model', {
                 }
             }
             currentValue = me.get(fieldName);
-            me[me.persistanceProperty][fieldName] = value;
+            me[me.persistenceProperty][fieldName] = value;
             
             if (field &amp;&amp; field.persist &amp;&amp; !me.isEqual(currentValue, value)) {
-                me.dirty = true;
-                me.modified[fieldName] = currentValue;
+                if (me.isModified(fieldName)) {
+                    if (me.isEqual(modified[fieldName], value)) {
+                        // the original value in me.modified equals the new value, so the
+                        // field is no longer modified
+                        delete modified[fieldName];
+                        // we might have removed the last modified field, so check to see if
+                        // there are any modified fields remaining and correct me.dirty:
+                        me.dirty = false;
+                        for (key in modified) {
+                            if (modified.hasOwnProperty(key)){
+                                me.dirty = true;
+                                break;
+                            }
+                        }
+                    }
+                } else {
+                    me.dirty = true;
+                    modified[fieldName] = currentValue;
+                }
             }
 
             if (!me.editing) {
@@ -675,7 +724,7 @@ Ext.define('Ext.data.Model', {
         }
     },
     
-<span id='Ext-data.Model-method-isEqual'>    /**
+<span id='Ext-data-Model-method-isEqual'>    /**
 </span>     * Checks if two values are equal, taking into account certain
      * special factors, for example dates.
      * @private
@@ -690,7 +739,7 @@ Ext.define('Ext.data.Model', {
         return a === b;
     },
     
-<span id='Ext-data.Model-method-beginEdit'>    /**
+<span id='Ext-data-Model-method-beginEdit'>    /**
 </span>     * Begin an edit. While in edit mode, no events (e.g.. the &lt;code&gt;update&lt;/code&gt; event)
      * are relayed to the containing store. When an edit has begun, it must be followed
      * by either {@link #endEdit} or {@link #cancelEdit}.
@@ -700,12 +749,12 @@ Ext.define('Ext.data.Model', {
         if (!me.editing) {
             me.editing = true;
             me.dirtySave = me.dirty;
-            me.dataSave = Ext.apply({}, me[me.persistanceProperty]);
+            me.dataSave = Ext.apply({}, me[me.persistenceProperty]);
             me.modifiedSave = Ext.apply({}, me.modified);
         }
     },
     
-<span id='Ext-data.Model-method-cancelEdit'>    /**
+<span id='Ext-data-Model-method-cancelEdit'>    /**
 </span>     * Cancels all changes made in the current edit operation.
      */
     cancelEdit : function(){
@@ -714,7 +763,7 @@ Ext.define('Ext.data.Model', {
             me.editing = false;
             // reset the modified state, nothing changed since the edit began
             me.modified = me.modifiedSave;
-            me[me.persistanceProperty] = me.dataSave;
+            me[me.persistenceProperty] = me.dataSave;
             me.dirty = me.dirtySave;
             delete me.modifiedSave;
             delete me.dataSave;
@@ -722,7 +771,7 @@ Ext.define('Ext.data.Model', {
         }
     },
     
-<span id='Ext-data.Model-method-endEdit'>    /**
+<span id='Ext-data-Model-method-endEdit'>    /**
 </span>     * End an edit. If any data was modified, the containing store is notified
      * (ie, the store's &lt;code&gt;update&lt;/code&gt; event will fire).
      * @param {Boolean} silent True to not notify the store of the change
@@ -740,7 +789,7 @@ Ext.define('Ext.data.Model', {
         }
     },
     
-<span id='Ext-data.Model-method-getChanges'>    /**
+<span id='Ext-data-Model-method-getChanges'>    /**
 </span>     * Gets a hash of only the fields that have been modified since this Model was created or commited.
      * @return Object
      */
@@ -758,7 +807,7 @@ Ext.define('Ext.data.Model', {
         return changes;
     },
     
-<span id='Ext-data.Model-method-isModified'>    /**
+<span id='Ext-data-Model-method-isModified'>    /**
 </span>     * Returns &lt;tt&gt;true&lt;/tt&gt; if the passed field name has been &lt;code&gt;{@link #modified}&lt;/code&gt;
      * since the load or last commit.
      * @param {String} fieldName {@link Ext.data.Field#name}
@@ -768,7 +817,7 @@ Ext.define('Ext.data.Model', {
         return this.modified.hasOwnProperty(fieldName);
     },
     
-<span id='Ext-data.Model-method-setDirty'>    /**
+<span id='Ext-data-Model-method-setDirty'>    /**
 </span>     * &lt;p&gt;Marks this &lt;b&gt;Record&lt;/b&gt; as &lt;code&gt;{@link #dirty}&lt;/code&gt;.  This method
      * is used interally when adding &lt;code&gt;{@link #phantom}&lt;/code&gt; records to a
      * {@link Ext.data.Store#writer writer enabled store}.&lt;/p&gt;
@@ -800,7 +849,7 @@ Ext.define('Ext.data.Model', {
     },
     //&lt;/debug&gt;
     
-<span id='Ext-data.Model-method-reject'>    /**
+<span id='Ext-data-Model-method-reject'>    /**
 </span>     * Usually called by the {@link Ext.data.Store} to which this model instance has been {@link #join joined}.
      * Rejects all changes made to the model instance since either creation, or the last commit operation.
      * Modified fields are reverted to their original values.
@@ -817,7 +866,7 @@ Ext.define('Ext.data.Model', {
         for (field in modified) {
             if (modified.hasOwnProperty(field)) {
                 if (typeof modified[field] != &quot;function&quot;) {
-                    me[me.persistanceProperty][field] = modified[field];
+                    me[me.persistenceProperty][field] = modified[field];
                 }
             }
         }
@@ -831,7 +880,7 @@ Ext.define('Ext.data.Model', {
         }
     },
 
-<span id='Ext-data.Model-method-commit'>    /**
+<span id='Ext-data-Model-method-commit'>    /**
 </span>     * Usually called by the {@link Ext.data.Store} which owns the model instance.
      * Commits all changes made to the instance since either creation or the last commit operation.
      * &lt;p&gt;Developers should subscribe to the {@link Ext.data.Store#update} event
@@ -852,7 +901,7 @@ Ext.define('Ext.data.Model', {
         }
     },
 
-<span id='Ext-data.Model-method-copy'>    /**
+<span id='Ext-data-Model-method-copy'>    /**
 </span>     * Creates a copy (clone) of this Model instance.
      * @param {String} id (optional) A new id, defaults to the id
      * of the instance being copied. See &lt;code&gt;{@link #id}&lt;/code&gt;.
@@ -865,10 +914,10 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
     copy : function(newId) {
         var me = this;
         
-        return new me.self(Ext.apply({}, me[me.persistanceProperty]), newId || me.internalId);
+        return new me.self(Ext.apply({}, me[me.persistenceProperty]), newId || me.internalId);
     },
 
-<span id='Ext-data.Model-method-setProxy'>    /**
+<span id='Ext-data-Model-method-setProxy'>    /**
 </span>     * Sets the Proxy to use for this model. Accepts any options that can be accepted by {@link Ext#createByAlias Ext.createByAlias}
      * @param {String/Object/Ext.data.proxy.Proxy} proxy The proxy
      * @static
@@ -889,7 +938,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return proxy;
     },
 
-<span id='Ext-data.Model-method-getProxy'>    /**
+<span id='Ext-data-Model-method-getProxy'>    /**
 </span>     * Returns the configured Proxy for this Model
      * @return {Ext.data.proxy.Proxy} The proxy
      */
@@ -897,7 +946,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return this.proxy;
     },
 
-<span id='Ext-data.Model-method-validate'>    /**
+<span id='Ext-data-Model-method-validate'>    /**
 </span>     * Validates the current data against all of its configured {@link #validations} and returns an
      * {@link Ext.data.Errors Errors} object
      * @return {Ext.data.Errors} The errors object
@@ -929,7 +978,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return errors;
     },
 
-<span id='Ext-data.Model-method-isValid'>    /**
+<span id='Ext-data-Model-method-isValid'>    /**
 </span>     * Checks if the model is valid. See {@link #validate}.
      * @return {Boolean} True if the model is valid.
      */
@@ -937,7 +986,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return this.validate().isValid();
     },
 
-<span id='Ext-data.Model-method-save'>    /**
+<span id='Ext-data-Model-method-save'>    /**
 </span>     * Saves the model instance using the configured proxy
      * @param {Object} options Options to pass to the proxy
      * @return {Ext.data.Model} The Model instance
@@ -980,7 +1029,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return me;
     },
 
-<span id='Ext-data.Model-method-destroy'>    /**
+<span id='Ext-data-Model-method-destroy'>    /**
 </span>     * Destroys the model using the configured proxy
      * @param {Object} options Options to pass to the proxy
      * @return {Ext.data.Model} The Model instance
@@ -1013,7 +1062,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return me;
     },
 
-<span id='Ext-data.Model-method-getId'>    /**
+<span id='Ext-data-Model-method-getId'>    /**
 </span>     * Returns the unique ID allocated to this model instance as defined by {@link #idProperty}
      * @return {Number} The id
      */
@@ -1021,7 +1070,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return this.get(this.idProperty);
     },
 
-<span id='Ext-data.Model-method-setId'>    /**
+<span id='Ext-data-Model-method-setId'>    /**
 </span>     * Sets the model instance's id field to the given id
      * @param {Number} id The new id
      */
@@ -1029,12 +1078,12 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         this.set(this.idProperty, id);
     },
 
-<span id='Ext-data.Model-method-join'>    /**
+<span id='Ext-data-Model-method-join'>    /**
 </span>     * Tells this model instance that it has been added to a store
      * @param {Ext.data.Store} store The store that the model has been added to
      */
     join : function(store) {
-<span id='Ext-data.Model-property-store'>        /**
+<span id='Ext-data-Model-property-store'>        /**
 </span>         * The {@link Ext.data.Store} to which this Record belongs.
          * @property store
          * @type {Ext.data.Store}
@@ -1042,14 +1091,14 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         this.store = store;
     },
 
-<span id='Ext-data.Model-method-unjoin'>    /**
+<span id='Ext-data-Model-method-unjoin'>    /**
 </span>     * Tells this model instance that it has been removed from the store
      */
     unjoin: function() {
         delete this.store;
     },
 
-<span id='Ext-data.Model-method-afterEdit'>    /**
+<span id='Ext-data-Model-method-afterEdit'>    /**
 </span>     * @private
      * If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's
      * afterEdit method is called
@@ -1058,7 +1107,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         this.callStore('afterEdit');
     },
 
-<span id='Ext-data.Model-method-afterReject'>    /**
+<span id='Ext-data-Model-method-afterReject'>    /**
 </span>     * @private
      * If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's
      * afterReject method is called
@@ -1067,7 +1116,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         this.callStore(&quot;afterReject&quot;);
     },
 
-<span id='Ext-data.Model-method-afterCommit'>    /**
+<span id='Ext-data-Model-method-afterCommit'>    /**
 </span>     * @private
      * If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's
      * afterCommit method is called
@@ -1076,7 +1125,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         this.callStore('afterCommit');
     },
 
-<span id='Ext-data.Model-method-callStore'>    /**
+<span id='Ext-data-Model-method-callStore'>    /**
 </span>     * @private
      * Helper function used by afterEdit, afterReject and afterCommit. Calls the given method on the
      * {@link Ext.data.Store store} that this instance has {@link #join joined}, if any. The store function
@@ -1091,7 +1140,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         }
     },
 
-<span id='Ext-data.Model-method-getAssociatedData'>    /**
+<span id='Ext-data-Model-method-getAssociatedData'>    /**
 </span>     * Gets all of the data from this Models *loaded* associations.
      * It does this recursively - for example if we have a User which
      * hasMany Orders, and each Order hasMany OrderItems, it will return an object like this:
@@ -1112,7 +1161,7 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return this.prepareAssociatedData(this, [], null);
     },
 
-<span id='Ext-data.Model-method-prepareAssociatedData'>    /**
+<span id='Ext-data-Model-method-prepareAssociatedData'>    /**
 </span>     * @private
      * This complex-looking method takes a given Model instance and returns an object containing all data from
      * all of that Model's *loaded* associations. See (@link #getAssociatedData}
@@ -1181,4 +1230,6 @@ Ext.data.Model.id(rec); // automatically generate a unique sequential id
         return associationData;
     }
 });
-</pre></pre></body></html>
\ No newline at end of file
+</pre>
+</body>
+</html>