]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-function.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-function.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-function', function(Y) {
9
10 /**
11  * Provides a DataSource implementation which can be used to retrieve data from a custom function.
12  *
13  * @module datasource
14  * @submodule datasource-function
15  */
16
17 /**
18  * Function subclass for the DataSource Utility.
19  * @class DataSource.Function
20  * @extends DataSource.Local
21  * @constructor
22  */    
23 var LANG = Y.Lang,
24
25     DSFn = function() {
26         DSFn.superclass.constructor.apply(this, arguments);
27     };
28     
29
30     /////////////////////////////////////////////////////////////////////////////
31     //
32     // DataSource.Function static properties
33     //
34     /////////////////////////////////////////////////////////////////////////////
35 Y.mix(DSFn, {
36     /**
37      * Class name.
38      *
39      * @property NAME
40      * @type String
41      * @static     
42      * @final
43      * @value "dataSourceFunction"
44      */
45     NAME: "dataSourceFunction",
46
47
48     /////////////////////////////////////////////////////////////////////////////
49     //
50     // DataSource.Function Attributes
51     //
52     /////////////////////////////////////////////////////////////////////////////
53
54     ATTRS: {
55         /**
56         * @attribute source
57         * @description Pointer to live data.
58         * @type MIXED
59         * @default null
60         */
61         source: {
62             validator: LANG.isFunction
63         }
64     }
65 });
66     
67 Y.extend(DSFn, Y.DataSource.Local, {
68     /**
69      * Passes query string to IO. Fires <code>response</code> event when
70      * response is received asynchronously.
71      *
72      * @method _defRequestFn
73      * @param e {Event.Facade} Event Facade with the following properties:
74      * <dl>
75      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
76      * <dt>request (Object)</dt> <dd>The request.</dd>
77      * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
78      *     <dl>
79      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
80      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
81      *     </dl>
82      * </dd>
83      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
84      * </dl>
85      * @protected
86      */
87     _defRequestFn: function(e) {
88         var fn = this.get("source"),
89             response;
90             
91             if(fn) {
92                 try {
93                     response = fn(e.request, this, e);
94                     this.fire("data", Y.mix({data:response}, e));
95                 }
96                 catch(error) {
97                     e.error = error;
98                     this.fire("data", e);
99                 }
100             }
101             else {
102                 e.error = new Error("Function data failure");
103                 this.fire("data", e);
104             }
105             
106         return e.tId;
107     }
108 });
109   
110 Y.DataSource.Function = DSFn;
111     
112
113
114
115 }, '3.3.0' ,{requires:['datasource-local']});