Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / data / Types.js
index d6e31f9..9fcd2d0 100644 (file)
@@ -1,9 +1,17 @@
-/*!
- * Ext JS Library 3.3.0
- * Copyright(c) 2006-2010 Ext JS, Inc.
- * licensing@extjs.com
- * http://www.extjs.com/license
- */
+/*
+
+This file is part of Ext JS 4
+
+Copyright (c) 2011 Sencha Inc
+
+Contact:  http://www.sencha.com/contact
+
+GNU General Public License Usage
+This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+
+If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
+
+*/
 /**
  * @class Ext.data.Types
  * <p>This is s static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
@@ -19,8 +27,8 @@
  * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
  * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
  * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
- * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
- * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
+ * Depending on the Reader type, this could be an Array ({@link Ext.data.reader.Array ArrayReader}), an object
+ * ({@link Ext.data.reader.Json JsonReader}), or an XML element.</div></li>
  * </ul></div></div></li>
  * <li><code>sortType</code> : <i>Function</i> <div class="sub-desc">A function to convert the stored data into comparable form, as defined by {@link Ext.data.SortTypes}.</div></li>
  * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
@@ -39,21 +47,28 @@ Ext.data.Types.VELATLONG = {
     type: 'VELatLong'
 };
 </code></pre>
- * <p>Then, when declaring a Record, use <pre><code>
+ * <p>Then, when declaring a Model, use <pre><code>
 var types = Ext.data.Types; // allow shorthand type access
-UnitRecord = Ext.data.Record.create([
-    { name: 'unitName', mapping: 'UnitName' },
-    { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
-    { name: 'latitude', mapping: 'lat', type: types.FLOAT },
-    { name: 'latitude', mapping: 'lat', type: types.FLOAT },
-    { name: 'position', type: types.VELATLONG }
-]);
+Ext.define('Unit',
+    extend: 'Ext.data.Model', 
+    fields: [
+        { name: 'unitName', mapping: 'UnitName' },
+        { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
+        { name: 'latitude', mapping: 'lat', type: types.FLOAT },
+        { name: 'latitude', mapping: 'lat', type: types.FLOAT },
+        { name: 'position', type: types.VELATLONG }
+    ]
+});
 </code></pre>
  * @singleton
  */
-Ext.data.Types = new function(){
+Ext.define('Ext.data.Types', {
+    singleton: true,
+    requires: ['Ext.data.SortTypes']
+}, function() {
     var st = Ext.data.SortTypes;
-    Ext.apply(this, {
+    
+    Ext.apply(Ext.data.Types, {
         /**
          * @type Regexp
          * @property stripRe
@@ -68,7 +83,9 @@ Ext.data.Types = new function(){
          * This data type means that no conversion is applied to the raw data before it is placed into a Record.
          */
         AUTO: {
-            convert: function(v){ return v; },
+            convert: function(v) {
+                return v;
+            },
             sortType: st.none,
             type: 'auto'
         },
@@ -79,7 +96,10 @@ Ext.data.Types = new function(){
          * This data type means that the raw data is converted into a String before it is placed into a Record.
          */
         STRING: {
-            convert: function(v){ return (v === undefined || v === null) ? '' : String(v); },
+            convert: function(v) {
+                var defaultValue = this.useNull ? null : '';
+                return (v === undefined || v === null) ? defaultValue : String(v);
+            },
             sortType: st.asUCString,
             type: 'string'
         },
@@ -91,7 +111,7 @@ Ext.data.Types = new function(){
          * <p>The synonym <code>INTEGER</code> is equivalent.</p>
          */
         INT: {
-            convert: function(v){
+            convert: function(v) {
                 return v !== undefined && v !== null && v !== '' ?
                     parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
             },
@@ -106,7 +126,7 @@ Ext.data.Types = new function(){
          * <p>The synonym <code>NUMBER</code> is equivalent.</p>
          */
         FLOAT: {
-            convert: function(v){
+            convert: function(v) {
                 return v !== undefined && v !== null && v !== '' ?
                     parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
             },
@@ -122,7 +142,12 @@ Ext.data.Types = new function(){
          * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
          */
         BOOL: {
-            convert: function(v){ return v === true || v === 'true' || v == 1; },
+            convert: function(v) {
+                if (this.useNull && v === undefined || v === null || v === '') {
+                    return null;
+                }
+                return v === true || v === 'true' || v == 1;
+            },
             sortType: st.none,
             type: 'bool'
         },
@@ -135,23 +160,24 @@ Ext.data.Types = new function(){
          * being applied.
          */
         DATE: {
-            convert: function(v){
+            convert: function(v) {
                 var df = this.dateFormat;
-                if(!v){
+                if (!v) {
                     return null;
                 }
-                if(Ext.isDate(v)){
+                if (Ext.isDate(v)) {
                     return v;
                 }
-                if(df){
-                    if(df == 'timestamp'){
+                if (df) {
+                    if (df == 'timestamp') {
                         return new Date(v*1000);
                     }
-                    if(df == 'time'){
+                    if (df == 'time') {
                         return new Date(parseInt(v, 10));
                     }
-                    return Date.parseDate(v, df);
+                    return Ext.Date.parse(v, df);
                 }
+                
                 var parsed = Date.parse(v);
                 return parsed ? new Date(parsed) : null;
             },
@@ -160,7 +186,7 @@ Ext.data.Types = new function(){
         }
     });
     
-    Ext.apply(this, {
+    Ext.apply(Ext.data.Types, {
         /**
          * @type Object.
          * @property BOOLEAN
@@ -169,6 +195,7 @@ Ext.data.Types = new function(){
          * <p>The synonym <code>BOOL</code> is equivalent.</p>
          */
         BOOLEAN: this.BOOL,
+        
         /**
          * @type Object.
          * @property INTEGER
@@ -176,6 +203,7 @@ Ext.data.Types = new function(){
          * <p>The synonym <code>INT</code> is equivalent.</p>
          */
         INTEGER: this.INT,
+        
         /**
          * @type Object.
          * @property NUMBER
@@ -184,4 +212,5 @@ Ext.data.Types = new function(){
          */
         NUMBER: this.FLOAT    
     });
-};
\ No newline at end of file
+});
+