|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('datasource-function', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Provides a DataSource implementation which can be used to retrieve data from |
|
12 * a custom function. |
|
13 * |
|
14 * @module datasource |
|
15 * @submodule datasource-function |
|
16 */ |
|
17 |
|
18 /** |
|
19 * Function subclass for the DataSource Utility. |
|
20 * @class DataSource.Function |
|
21 * @extends DataSource.Local |
|
22 * @constructor |
|
23 */ |
|
24 var LANG = Y.Lang, |
|
25 |
|
26 DSFn = function() { |
|
27 DSFn.superclass.constructor.apply(this, arguments); |
|
28 }; |
|
29 |
|
30 |
|
31 ///////////////////////////////////////////////////////////////////////////// |
|
32 // |
|
33 // DataSource.Function static properties |
|
34 // |
|
35 ///////////////////////////////////////////////////////////////////////////// |
|
36 Y.mix(DSFn, { |
|
37 /** |
|
38 * Class name. |
|
39 * |
|
40 * @property NAME |
|
41 * @type String |
|
42 * @static |
|
43 * @final |
|
44 * @value "dataSourceFunction" |
|
45 */ |
|
46 NAME: "dataSourceFunction", |
|
47 |
|
48 |
|
49 ///////////////////////////////////////////////////////////////////////////// |
|
50 // |
|
51 // DataSource.Function Attributes |
|
52 // |
|
53 ///////////////////////////////////////////////////////////////////////////// |
|
54 |
|
55 ATTRS: { |
|
56 /** |
|
57 * Stores the function that will serve the response data. |
|
58 * |
|
59 * @attribute source |
|
60 * @type {Any} |
|
61 * @default null |
|
62 */ |
|
63 source: { |
|
64 validator: LANG.isFunction |
|
65 } |
|
66 } |
|
67 }); |
|
68 |
|
69 Y.extend(DSFn, Y.DataSource.Local, { |
|
70 /** |
|
71 * Passes query data to the source function. Fires <code>response</code> |
|
72 * event with the function results (synchronously). |
|
73 * |
|
74 * @method _defRequestFn |
|
75 * @param e {Event.Facade} Event Facade with the following properties: |
|
76 * <dl> |
|
77 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
78 * <dt>request (Object)</dt> <dd>The request.</dd> |
|
79 * <dt>callback (Object)</dt> <dd>The callback object with the following |
|
80 * properties: |
|
81 * <dl> |
|
82 * <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
83 * <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
84 * </dl> |
|
85 * </dd> |
|
86 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
87 * </dl> |
|
88 * @protected |
|
89 */ |
|
90 _defRequestFn: function(e) { |
|
91 var fn = this.get("source"), |
|
92 payload = e.details[0]; |
|
93 |
|
94 if (fn) { |
|
95 try { |
|
96 payload.data = fn(e.request, this, e); |
|
97 } catch (ex) { |
|
98 payload.error = ex; |
|
99 } |
|
100 } else { |
|
101 payload.error = new Error("Function data failure"); |
|
102 } |
|
103 |
|
104 this.fire("data", payload); |
|
105 |
|
106 return e.tId; |
|
107 } |
|
108 }); |
|
109 |
|
110 Y.DataSource.Function = DSFn; |
|
111 |
|
112 |
|
113 }, '3.10.3', {"requires": ["datasource-local"]}); |