API Docs for: 3.13.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. /**
  10. * Provides the base DataSchema implementation, which can be extended to
  11. * create DataSchemas for specific data formats, such XML, JSON, text and
  12. * arrays.
  13. *
  14. * @module dataschema
  15. * @submodule dataschema-base
  16. */
  17.  
  18. var LANG = Y.Lang,
  19. /**
  20. * Base class for the YUI DataSchema Utility.
  21. * @class DataSchema.Base
  22. * @static
  23. */
  24. SchemaBase = {
  25. /**
  26. * Overridable method returns data as-is.
  27. *
  28. * @method apply
  29. * @param schema {Object} Schema to apply.
  30. * @param data {Object} Data.
  31. * @return {Object} Schema-parsed data.
  32. * @static
  33. */
  34. apply: function(schema, data) {
  35. return data;
  36. },
  37.  
  38. /**
  39. * Applies field parser, if defined
  40. *
  41. * @method parse
  42. * @param value {Object} Original value.
  43. * @param field {Object} Field.
  44. * @return {Object} Type-converted value.
  45. */
  46. parse: function(value, field) {
  47. if(field.parser) {
  48. var parser = (LANG.isFunction(field.parser)) ?
  49. field.parser : Y.Parsers[field.parser+''];
  50. if(parser) {
  51. value = parser.call(this, value);
  52. }
  53. else {
  54. Y.log("Could not find parser for field " + Y.dump(field), "warn", "dataschema-json");
  55. }
  56. }
  57. return value;
  58. }
  59. };
  60.  
  61. Y.namespace("DataSchema").Base = SchemaBase;
  62. Y.namespace("Parsers");
  63.