|
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-xmlschema', function(Y) { |
|
9 |
|
10 /** |
|
11 * Extends DataSource with schema-parsing on XML data. |
|
12 * |
|
13 * @module datasource |
|
14 * @submodule datasource-xmlschema |
|
15 */ |
|
16 |
|
17 /** |
|
18 * Adds schema-parsing to the DataSource Utility. |
|
19 * @class DataSourceXMLSchema |
|
20 * @extends Plugin.Base |
|
21 */ |
|
22 var DataSourceXMLSchema = function() { |
|
23 DataSourceXMLSchema.superclass.constructor.apply(this, arguments); |
|
24 }; |
|
25 |
|
26 Y.mix(DataSourceXMLSchema, { |
|
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 "dataSourceXMLSchema" |
|
47 */ |
|
48 NAME: "dataSourceXMLSchema", |
|
49 |
|
50 ///////////////////////////////////////////////////////////////////////////// |
|
51 // |
|
52 // DataSourceXMLSchema Attributes |
|
53 // |
|
54 ///////////////////////////////////////////////////////////////////////////// |
|
55 |
|
56 ATTRS: { |
|
57 schema: { |
|
58 //value: {} |
|
59 } |
|
60 } |
|
61 }); |
|
62 |
|
63 Y.extend(DataSourceXMLSchema, 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) && e.data.responseXML && (e.data.responseXML.nodeType === 9)) ? e.data.responseXML : e.data, |
|
94 response = Y.DataSchema.XML.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("DataSourceXMLSchema plugin halted _defDataFn"); |
|
106 } |
|
107 }); |
|
108 |
|
109 Y.namespace('Plugin').DataSourceXMLSchema = DataSourceXMLSchema; |
|
110 |
|
111 |
|
112 |
|
113 }, '3.0.0b1' ,{requires:['plugin', 'datasource-local', 'dataschema-xml']}); |