API Docs for: 3.8.0
Show:

File: dataschema/js/dataschema-base.js

  1. /**
  2.  * The DataSchema utility provides a common configurable interface for widgets to
  3.  * apply a given schema to a variety of data.
  4.  *
  5.  * @module dataschema
  6.  * @main dataschema
  7.  */

  8. /**
  9.  * Provides the base DataSchema implementation, which can be extended to
  10.  * create DataSchemas for specific data formats, such XML, JSON, text and
  11.  * arrays.
  12.  *
  13.  * @module dataschema
  14.  * @submodule dataschema-base
  15.  */

  16. var LANG = Y.Lang,
  17. /**
  18.  * Base class for the YUI DataSchema Utility.
  19.  * @class DataSchema.Base
  20.  * @static
  21.  */
  22.     SchemaBase = {
  23.     /**
  24.      * Overridable method returns data as-is.
  25.      *
  26.      * @method apply
  27.      * @param schema {Object} Schema to apply.
  28.      * @param data {Object} Data.
  29.      * @return {Object} Schema-parsed data.
  30.      * @static
  31.      */
  32.     apply: function(schema, data) {
  33.         return data;
  34.     },
  35.    
  36.     /**
  37.      * Applies field parser, if defined
  38.      *
  39.      * @method parse
  40.      * @param value {Object} Original value.
  41.      * @param field {Object} Field.
  42.      * @return {Object} Type-converted value.
  43.      */
  44.     parse: function(value, field) {
  45.         if(field.parser) {
  46.             var parser = (LANG.isFunction(field.parser)) ?
  47.             field.parser : Y.Parsers[field.parser+''];
  48.             if(parser) {
  49.                 value = parser.call(this, value);
  50.             }
  51.             else {
  52.                 Y.log("Could not find parser for field " + Y.dump(field), "warn", "dataschema-json");
  53.             }
  54.         }
  55.         return value;
  56.     }
  57. };

  58. Y.namespace("DataSchema").Base = SchemaBase;
  59. Y.namespace("Parsers");

  60.