|
602
|
1 |
YUI.add('datasource-arrayschema', function (Y, NAME) { |
|
|
2 |
|
|
|
3 |
/** |
|
|
4 |
* Extends DataSource with schema-parsing on array data. |
|
|
5 |
* |
|
|
6 |
* @module datasource |
|
|
7 |
* @submodule datasource-arrayschema |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Adds schema-parsing to the DataSource Utility. |
|
|
12 |
* @class DataSourceArraySchema |
|
|
13 |
* @extends Plugin.Base |
|
|
14 |
*/ |
|
|
15 |
var DataSourceArraySchema = function() { |
|
|
16 |
DataSourceArraySchema.superclass.constructor.apply(this, arguments); |
|
|
17 |
}; |
|
|
18 |
|
|
|
19 |
Y.mix(DataSourceArraySchema, { |
|
|
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 "dataSourceArraySchema" |
|
|
40 |
*/ |
|
|
41 |
NAME: "dataSourceArraySchema", |
|
|
42 |
|
|
|
43 |
///////////////////////////////////////////////////////////////////////////// |
|
|
44 |
// |
|
|
45 |
// DataSourceArraySchema Attributes |
|
|
46 |
// |
|
|
47 |
///////////////////////////////////////////////////////////////////////////// |
|
|
48 |
|
|
|
49 |
ATTRS: { |
|
|
50 |
schema: { |
|
|
51 |
//value: {} |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
}); |
|
|
55 |
|
|
|
56 |
Y.extend(DataSourceArraySchema, 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. |
|
|
70 |
* |
|
|
71 |
* @method _beforeDefDataFn |
|
|
72 |
* @param tId {Number} Unique transaction ID. |
|
|
73 |
* @param request {Object} The request. |
|
|
74 |
* @param callback {Object} The callback object with the following properties: |
|
|
75 |
* <dl> |
|
|
76 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
77 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
78 |
* </dl> |
|
|
79 |
* @param data {Object} Raw data. |
|
|
80 |
* @protected |
|
|
81 |
*/ |
|
|
82 |
_beforeDefDataFn: function(e) { |
|
|
83 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data, |
|
|
84 |
response = Y.DataSchema.Array.apply.call(this, this.get("schema"), data), |
|
|
85 |
payload = e.details[0]; |
|
|
86 |
|
|
|
87 |
// Default |
|
|
88 |
if (!response) { |
|
|
89 |
response = { |
|
|
90 |
meta: {}, |
|
|
91 |
results: data |
|
|
92 |
}; |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
payload.response = response; |
|
|
96 |
|
|
|
97 |
this.get("host").fire("response", payload); |
|
|
98 |
|
|
|
99 |
return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn"); |
|
|
100 |
} |
|
|
101 |
}); |
|
|
102 |
|
|
|
103 |
Y.namespace('Plugin').DataSourceArraySchema = DataSourceArraySchema; |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
}, '@VERSION@', {"requires": ["datasource-local", "plugin", "dataschema-array"]}); |