|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('datasource-arrayschema', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Extends DataSource with schema-parsing on array data. |
|
|
12 |
* |
|
|
13 |
* @module datasource |
|
|
14 |
* @submodule datasource-arrayschema |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Adds schema-parsing to the DataSource Utility. |
|
|
19 |
* @class DataSourceArraySchema |
|
|
20 |
* @extends Plugin.Base |
|
|
21 |
*/ |
|
|
22 |
var DataSourceArraySchema = function() { |
|
|
23 |
DataSourceArraySchema.superclass.constructor.apply(this, arguments); |
|
|
24 |
}; |
|
|
25 |
|
|
|
26 |
Y.mix(DataSourceArraySchema, { |
|
|
27 |
/** |
|
|
28 |
* The namespace for the plugin. This will be the property on the host which |
|
|
29 |
* references the plugin instance. |
|
|
30 |
* |
|
|
31 |
* @property NS |
|
|
32 |
* @type String |
|
|
33 |
* @static |
|
|
34 |
* @final |
|
|
35 |
* @value "schema" |
|
|
36 |
*/ |
|
|
37 |
NS: "schema", |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Class name. |
|
|
41 |
* |
|
|
42 |
* @property NAME |
|
|
43 |
* @type String |
|
|
44 |
* @static |
|
|
45 |
* @final |
|
|
46 |
* @value "dataSourceArraySchema" |
|
|
47 |
*/ |
|
|
48 |
NAME: "dataSourceArraySchema", |
|
|
49 |
|
|
|
50 |
///////////////////////////////////////////////////////////////////////////// |
|
|
51 |
// |
|
|
52 |
// DataSourceArraySchema Attributes |
|
|
53 |
// |
|
|
54 |
///////////////////////////////////////////////////////////////////////////// |
|
|
55 |
|
|
|
56 |
ATTRS: { |
|
|
57 |
schema: { |
|
|
58 |
//value: {} |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
}); |
|
|
62 |
|
|
|
63 |
Y.extend(DataSourceArraySchema, Y.Plugin.Base, { |
|
|
64 |
/** |
|
|
65 |
* Internal init() handler. |
|
|
66 |
* |
|
|
67 |
* @method initializer |
|
|
68 |
* @param config {Object} Config object. |
|
|
69 |
* @private |
|
|
70 |
*/ |
|
|
71 |
initializer: function(config) { |
|
|
72 |
this.doBefore("_defDataFn", this._beforeDefDataFn); |
|
|
73 |
}, |
|
|
74 |
|
|
|
75 |
/** |
|
|
76 |
* Parses raw data into a normalized response. |
|
|
77 |
* |
|
|
78 |
* @method _beforeDefDataFn |
|
|
79 |
* <dl> |
|
|
80 |
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
|
81 |
* <dt>request (Object)</dt> <dd>The request.</dd> |
|
|
82 |
* <dt>callback (Object)</dt> <dd>The callback object with the following properties: |
|
|
83 |
* <dl> |
|
|
84 |
* <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
|
85 |
* <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
|
86 |
* </dl> |
|
|
87 |
* </dd> |
|
|
88 |
* <dt>data (Object)</dt> <dd>Raw data.</dd> |
|
|
89 |
* </dl> |
|
|
90 |
* @protected |
|
|
91 |
*/ |
|
|
92 |
_beforeDefDataFn: function(e) { |
|
|
93 |
var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data, |
|
|
94 |
response = Y.DataSchema.Array.apply(this.get("schema"), data); |
|
|
95 |
|
|
|
96 |
// Default |
|
|
97 |
if(!response) { |
|
|
98 |
response = { |
|
|
99 |
meta: {}, |
|
|
100 |
results: data |
|
|
101 |
}; |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
this.get("host").fire("response", Y.mix({response:response}, e)); |
|
|
105 |
return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn"); |
|
|
106 |
} |
|
|
107 |
}); |
|
|
108 |
|
|
|
109 |
Y.namespace('Plugin').DataSourceArraySchema = DataSourceArraySchema; |
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
}, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-array']}); |