Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added optional error callback to offer error handling to plugin users.

  • Loading branch information...
commit 5494cda7fbfd12d5504ad7e838f28be29f60734e 1 parent ca227f2
@guilhermechapiewski authored
Showing with 52 additions and 25 deletions.
  1. +30 −24 jquery.yql.js
  2. +22 −1 tests/test.yql.js
View
54 jquery.yql.js
@@ -13,45 +13,51 @@
$.extend(
{
_prepareYQLQuery: function (query, params) {
- $.each(
- params, function (key) {
- var name = "#{" + key + "}";
- var value = $.trim(this);
- if (!value.match(/^[0-9]+$/)) {
- value = '"' + value + '"';
- }
- query = query.replace(name, value);
+ $.each(
+ params, function (key) {
+ var name = "#{" + key + "}";
+ var value = $.trim(this);
+ if (!value.match(/^[0-9]+$/)) {
+ value = '"' + value + '"';
}
- );
+ 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;
-
- $.ajax(
- {
- 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: "?"
- }
+ 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;
}
}
View
23 tests/test.yql.js
@@ -154,4 +154,25 @@ asyncTest('It can fetch location data by IP address', function() {
start();
});
- });
+ });
+
+test('It should call the optional error callback when ajax fails', function() {
+ expect(1);
+
+ $.ajax = function (params) {
+ params.error("error data");
+ }
+
+ $.yql("SELECT * FROM unknown_table WHERE param=#{param} ", {param: "blah"},
+ function (data) {
+ ok(false, "should not reach here");
+ start();
+ },
+ function (error_data) {
+ equal(error_data, "error data");
+ start();
+ }
+ );
+
+ $.ajax = oldAjax;
+});
Please sign in to comment.
Something went wrong with that request. Please try again.