]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-jsonschema.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-jsonschema.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('datasource-jsonschema', function(Y) {
9
10 /**
11  * Extends DataSource with schema-parsing on JSON data.
12  *
13  * @module datasource
14  * @submodule datasource-jsonschema
15  */
16
17 /**
18  * Adds schema-parsing to the DataSource Utility.
19  * @class DataSourceJSONSchema
20  * @extends Plugin.Base
21  */    
22 var DataSourceJSONSchema = function() {
23     DataSourceJSONSchema.superclass.constructor.apply(this, arguments);
24 };
25
26 Y.mix(DataSourceJSONSchema, {
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 "dataSourceJSONSchema"
47      */
48     NAME: "dataSourceJSONSchema",
49
50     /////////////////////////////////////////////////////////////////////////////
51     //
52     // DataSourceJSONSchema Attributes
53     //
54     /////////////////////////////////////////////////////////////////////////////
55
56     ATTRS: {
57         schema: {
58             //value: {}
59         }
60     }
61 });
62
63 Y.extend(DataSourceJSONSchema, 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. To accommodate XHR responses,
77      * will first look for data in data.responseText. Otherwise will just work
78      * with data.
79      *
80      * @method _beforeDefDataFn
81      * <dl>
82      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
83      * <dt>request (Object)</dt> <dd>The request.</dd>
84      * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
85      *     <dl>
86      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
87      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
88      *     </dl>
89      * </dd>
90      * <dt>data (Object)</dt> <dd>Raw data.</dd>
91      * </dl>
92      * @protected
93      */
94     _beforeDefDataFn: function(e) {
95         var data = e.data ? (e.data.responseText ?  e.data.responseText : e.data) : e.data,
96             response = Y.DataSchema.JSON.apply.call(this, this.get("schema"), data);
97             
98         // Default
99         if(!response) {
100             response = {
101                 meta: {},
102                 results: data
103             };
104         }
105         
106         this.get("host").fire("response", Y.mix({response:response}, e));
107         return new Y.Do.Halt("DataSourceJSONSchema plugin halted _defDataFn");
108     }
109 });
110     
111 Y.namespace('Plugin').DataSourceJSONSchema = DataSourceJSONSchema;
112
113
114
115 }, '3.3.0' ,{requires:['datasource-local', 'plugin', 'dataschema-json']});