|
1 YUI.add('datasource-jsonschema', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Extends DataSource with schema-parsing on JSON data. |
|
5 * |
|
6 * @module datasource |
|
7 * @submodule datasource-jsonschema |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Adds schema-parsing to the DataSource Utility. |
|
12 * @class DataSourceJSONSchema |
|
13 * @extends Plugin.Base |
|
14 */ |
|
15 var DataSourceJSONSchema = function() { |
|
16 DataSourceJSONSchema.superclass.constructor.apply(this, arguments); |
|
17 }; |
|
18 |
|
19 Y.mix(DataSourceJSONSchema, { |
|
20 /** |
|
21 * The namespace for the plugin. This will be the property on the host which |
|
22 * references the plugin instance. |
|
23 * |
|
24 * @property NS |
|
25 * @type String |
|
26 * @static |
|
27 * @final |
|
28 * @value "schema" |
|
29 */ |
|
30 NS: "schema", |
|
31 |
|
32 /** |
|
33 * Class name. |
|
34 * |
|
35 * @property NAME |
|
36 * @type String |
|
37 * @static |
|
38 * @final |
|
39 * @value "dataSourceJSONSchema" |
|
40 */ |
|
41 NAME: "dataSourceJSONSchema", |
|
42 |
|
43 ///////////////////////////////////////////////////////////////////////////// |
|
44 // |
|
45 // DataSourceJSONSchema Attributes |
|
46 // |
|
47 ///////////////////////////////////////////////////////////////////////////// |
|
48 |
|
49 ATTRS: { |
|
50 schema: { |
|
51 //value: {} |
|
52 } |
|
53 } |
|
54 }); |
|
55 |
|
56 Y.extend(DataSourceJSONSchema, Y.Plugin.Base, { |
|
57 /** |
|
58 * Internal init() handler. |
|
59 * |
|
60 * @method initializer |
|
61 * @param config {Object} Config object. |
|
62 * @private |
|
63 */ |
|
64 initializer: function(config) { |
|
65 this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
66 }, |
|
67 |
|
68 /** |
|
69 * Parses raw data into a normalized response. To accommodate XHR responses, |
|
70 * will first look for data in data.responseText. Otherwise will just work |
|
71 * with data. |
|
72 * |
|
73 * @method _beforeDefDataFn |
|
74 * @param tId {Number} Unique transaction ID. |
|
75 * @param request {Object} The request. |
|
76 * @param callback {Object} The callback object with the following properties: |
|
77 * <dl> |
|
78 * <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
79 * <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
80 * </dl> |
|
81 * @param data {Object} Raw data. |
|
82 * @protected |
|
83 */ |
|
84 _beforeDefDataFn: function(e) { |
|
85 var data = e.data && (e.data.responseText || e.data), |
|
86 schema = this.get('schema'), |
|
87 payload = e.details[0]; |
|
88 |
|
89 payload.response = Y.DataSchema.JSON.apply.call(this, schema, data) || { |
|
90 meta: {}, |
|
91 results: data |
|
92 }; |
|
93 |
|
94 this.get("host").fire("response", payload); |
|
95 |
|
96 return new Y.Do.Halt("DataSourceJSONSchema plugin halted _defDataFn"); |
|
97 } |
|
98 }); |
|
99 |
|
100 Y.namespace('Plugin').DataSourceJSONSchema = DataSourceJSONSchema; |
|
101 |
|
102 |
|
103 }, '@VERSION@', {"requires": ["datasource-local", "plugin", "dataschema-json"]}); |