-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.yql.js
78 lines (71 loc) · 2.59 KB
/
jquery.yql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* jQuery YQL plugin
*
* Copyright (c) 2010 Gabriel Falcão
* Copyright (c) 2010 Lincoln de Sousa
* licensed under MIT license.
*
* http://github.com/gabrielfalcao/jquery-yql/raw/master/license.txt
*
* Version: 0.3.0
*/
(function($){
$.extend(
{
_prepareYQLQuery: function (query, params) {
$.each(
params, function (key) {
var name = "#{" + key + "}";
var value = $.trim(this);
if (!value.match(/^[0-9]+$/)) {
value = '"' + value + '"';
}
while (query.search(name) > -1) {
query = query.replace(name, value);
}
var name = "@" + key;
var value = $.trim(this);
if (!value.match(/^[0-9]+$/)) {
value = '"' + value + '"';
}
while (query.search(name) > -1) {
query = query.replace(name, value);
}
}
);
return query;
},
yql: function (query) {
var $self = this;
var successCallback = null;
var errorCallback = null;
if (typeof arguments[1] == 'object') {
query = $self._prepareYQLQuery(query, arguments[1]);
successCallback = arguments[2];
errorCallback = arguments[3];
} else if (typeof arguments[1] == 'function') {
successCallback = arguments[1];
errorCallback = arguments[2];
}
var doAsynchronously = successCallback != null;
var yqlJson = {
url: "http://query.yahooapis.com/v1/public/yql",
dataType: "jsonp",
success: successCallback,
async: doAsynchronously,
data: {
q: query,
format: "json",
env: 'store://datatables.org/alltableswithkeys',
callback: "?"
}
}
if (errorCallback) {
yqlJson.error = errorCallback;
}
$.ajax(yqlJson);
return $self.toReturn;
}
}
);
})(jQuery);