From 8884f00ebc2dcc2c091c059ecb7f1d2d50cb8072 Mon Sep 17 00:00:00 2001 From: Devon Irish Date: Thu, 13 Aug 2015 15:40:25 -0400 Subject: [PATCH] Update to use jqXHR Promise methods ## 0.15.0 * Replaced `statusCode` and `complete` callbacks with Promise methods of the jqXHR object returned by `$.ajax()` (`.done()`, `.fail()`, and `.always()`) for more verbose response handling * Updated `package.json` `"jquery"` dependency to `">=1.8.3"` for consistency with `bower.json` * Bumped version of `eldarion-ajax-core.js` to 0.13.0 * Updated `README.md` to reflect changes --- CHANGELOG.md | 7 +++++++ README.md | 38 +++++++++++++++++++++----------------- bower.json | 2 +- js/copyright.js | 2 +- js/eldarion-ajax-core.js | 34 ++++++++++------------------------ js/eldarion-ajax.min.js | 4 ++-- package.json | 4 ++-- 7 files changed, 44 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afd65c7..f511a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # ChangeLog +## 0.15.0 + +* Replaced `statusCode` and `complete` callbacks with Promise methods of the jqXHR object returned by `$.ajax()` (`.done()`, `.fail()`, and `.always()`) for more verbose response handling +* Updated `package.json` `"jquery"` dependency to `">=1.8.3"` for consistency with `bower.json` +* Bumped version of `eldarion-ajax-core.js` to 0.13.0 +* Updated `README.md` to reflect changes + ## 0.14.0 * Reintroduced file upload support using `FormData` if it is supported by the diff --git a/README.md b/README.md index b38790a..4d0fd3d 100644 --- a/README.md +++ b/README.md @@ -112,15 +112,11 @@ A single argument is sent with this event and is the jQuery object for the node: ### ```eldarion-ajax:success``` -This is the event that is triggered once the browser receives a successful -response (status code 200) from the server. You can handle this in order to -provide your own processors if the ones that ship by default do not meet your -needs. +This event is triggered if the request succeeds. Four arguments are passed with +this event: the jQuery object; the data returned from the server; a string +describing the status; and the jqXHR object: -Two arguments are passed with this event, the jQuery object for the node, and -the JSON data from the server: - - $(document).on("eldarion-ajax:success", "[data-prepend-inner]", function(evt, $el, data) { + $(document).on("eldarion-ajax:success", "[data-prepend-inner]", function(evt, $el, data, textStatus, jqXHR) { var $node = $($el.data("prepend-inner")); $node.data(data.html + $node.html()); }); @@ -128,19 +124,27 @@ the JSON data from the server: ### ```eldarion-ajax:error``` -This event is triggered for 400, 403, 404, and 500 status codes. +This event is triggered if the request fails. Four arguments are also passed +with this event: the jQuery object, the jqXHR object; a string describing the +type of error that occurred; and an optional exception object. Possible values +for the third argument (besides null) are "timeout", "error", "abort", and +"parsererror". When an HTTP error occurs, the fourth argument receives the +textual portion of the HTTP status, such as "Not Found" or "Internal Server +Error." ### ```eldarion-ajax:complete``` -This gets sent on the completion of every ajax request no matter the status -code and in addition to the events listing above. This is triggered from the -document rather than the element in context as the handlers processing success -messages could replace the DOM element and therefore would prevent the event -from reaching your listener. - -It is passed the element (even if it no longer exists in the DOM), a ```jaXHR``` -object, and ```textStatus```. +This event is triggered when the request finishes (after the above `success` and +`error` events are completed). This is triggered from the document rather than +the element in context as the handlers processing success messages could replace +the DOM element and therefore would prevent the event from reaching your +listener. The element is always passed as the first argument with this event +(even if it no longer exists in the DOM). In response to a successful request, +the arguments passed with this event are the same as those of the `success` +event: the element, data, textStatus, and the jqXHR object. For failed requests +the arguments are the same as those of the `error` event: the element, the jqXHR +object, textStatus, and errorThrown. ### ```eldarion-ajax:modify-data``` diff --git a/bower.json b/bower.json index 8fea320..8e96430 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "eldarion-ajax", - "version": "0.13.0", + "version": "0.15.0", "main": "./js/eldarion-ajax.min.js", "dependencies": { "jquery": ">=1.8.3" diff --git a/js/copyright.js b/js/copyright.js index 7acd507..a3bc7fb 100644 --- a/js/copyright.js +++ b/js/copyright.js @@ -1,6 +1,6 @@ /* ==================================================================== * eldarion-ajax.min.js v0.14.0 - * eldarion-ajax-core v0.12.0 + * eldarion-ajax-core v0.13.0 * eldarion-ajax-handlers v0.1.2 * ==================================================================== * Copyright (c) 2015, Eldarion, Inc. diff --git a/js/eldarion-ajax-core.js b/js/eldarion-ajax-core.js index 4f994a0..53539e6 100644 --- a/js/eldarion-ajax-core.js +++ b/js/eldarion-ajax-core.js @@ -1,5 +1,5 @@ /* ==================================================================== - * eldarion-ajax-core.js v0.12.0 + * eldarion-ajax-core.js v0.13.0 * ==================================================================== * Copyright (c) 2015, Eldarion, Inc. * All rights reserved. @@ -73,30 +73,16 @@ cache: cache, processData: processData, contentType: contentType, - headers: {'X-Eldarion-Ajax': true}, - statusCode: { - 200: function (responseData) { - if (!responseData) { - responseData = {}; - } - $el.trigger('eldarion-ajax:success', [$el, responseData]); - }, - 500: function () { - $el.trigger('eldarion-ajax:error', [$el, 500]); - }, - 400: function () { - $el.trigger('eldarion-ajax:error', [$el, 400]); - }, - 403: function () { - $el.trigger('eldarion-ajax:error', [$el, 403]); - }, - 404: function () { - $el.trigger('eldarion-ajax:error', [$el, 404]); - } - }, - complete: function (jqXHR, textStatus) { - $(document).trigger('eldarion-ajax:complete', [$el, jqXHR, textStatus]); + headers: {'X-Eldarion-Ajax': true} + }).done( function (responseData, textStatus, jqXHR) { + if (!responseData) { + responseData = {}; } + $el.trigger('eldarion-ajax:success', [$el, responseData, textStatus, jqXHR]); + }).fail( function (jqXHR, textStatus, errorThrown) { + $el.trigger('eldarion-ajax:error', [$el, jqXHR, textStatus, errorThrown]); + }).always( function (responseData, textStatus, jqXHR) { + $(document).trigger('eldarion-ajax:complete', [$el, responseData, textStatus, jqXHR]); }); }; diff --git a/js/eldarion-ajax.min.js b/js/eldarion-ajax.min.js index fcf877d..d762887 100644 --- a/js/eldarion-ajax.min.js +++ b/js/eldarion-ajax.min.js @@ -1,6 +1,6 @@ /* ==================================================================== * eldarion-ajax.min.js v0.14.0 - * eldarion-ajax-core v0.12.0 + * eldarion-ajax-core v0.13.0 * eldarion-ajax-handlers v0.1.2 * ==================================================================== * Copyright (c) 2015, Eldarion, Inc. @@ -31,4 +31,4 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== */ -if(document.all&&!window.setTimeout.isPolyfill){var __nativeST__=window.setTimeout;window.setTimeout=function(e,t){var o=Array.prototype.slice.call(arguments,2);return __nativeST__(e instanceof Function?function(){e.apply(null,o)}:e,t)},window.setTimeout.isPolyfill=!0}if(document.all&&!window.setInterval.isPolyfill){var __nativeSI__=window.setInterval;window.setInterval=function(e,t){var o=Array.prototype.slice.call(arguments,2);return __nativeSI__(e instanceof Function?function(){e.apply(null,o)}:e,t)},window.setInterval.isPolyfill=!0}!function(e,t){"use strict";"function"==typeof define&&define.amd?define(["jquery"],t):t("object"==typeof exports?require("jquery"):e.jQuery)}(this,function(e){"use strict";var t=function(){};t.prototype._ajax=function(t,o,a,n){t.trigger("eldarion-ajax:begin",[t]);var r=t.triggerHandler("eldarion-ajax:modify-data",n),c="application/x-www-form-urlencoded; charset=UTF-8",i=!0,p=!0,s="object"==typeof n;r&&(n=r),s&&(c=!1,i=!1,p=!1),e.ajax({url:o,type:a,dataType:"json",data:n,cache:p,processData:i,contentType:c,headers:{"X-Eldarion-Ajax":!0},statusCode:{200:function(e){e||(e={}),t.trigger("eldarion-ajax:success",[t,e])},500:function(){t.trigger("eldarion-ajax:error",[t,500])},400:function(){t.trigger("eldarion-ajax:error",[t,400])},403:function(){t.trigger("eldarion-ajax:error",[t,403])},404:function(){t.trigger("eldarion-ajax:error",[t,404])}},complete:function(o,a){e(document).trigger("eldarion-ajax:complete",[t,o,a])}})},t.prototype.click=function(o){var a=e(this),n=a.attr("href"),r=a.data("method"),c=a.data("data"),i=null,p=null;r||(r="get"),c&&(i={},c.split(",").map(function(t){p=t.split(":"),i[p[0]]=0===p[1].indexOf("#")?e(p[1]).val():p[1]})),o.preventDefault(),t.prototype._ajax(a,n,r,i)},t.prototype.submit=function(o){var a=e(this),n=a.attr("action"),r=a.attr("method");o.preventDefault(),void 0===window.FormData?t.prototype._ajax(a,n,r,a.serialize()):t.prototype._ajax(a,n,r,new FormData(a[0]))},t.prototype.cancel=function(t){var o=e(this),a=o.attr("data-cancel-closest");t.preventDefault(),o.closest(a).remove()},t.prototype.timeout=function(o,a){var n=e(a),r=n.data("timeout"),c=n.data("url"),i=n.data("method");i||(i="get"),window.setTimeout(t.prototype._ajax,r,n,c,i,null)},t.prototype.interval=function(o,a){var n=e(a),r=n.data("interval"),c=n.data("url"),i=n.data("method");i||(i="get"),window.setInterval(t.prototype._ajax,r,n,c,i,null)},e(function(){e("body").on("click","a.ajax",t.prototype.click),e("body").on("submit","form.ajax",t.prototype.submit),e("body").on("click","a[data-cancel-closest]",t.prototype.cancel),e("[data-timeout]").each(t.prototype.timeout),e("[data-interval]").each(t.prototype.interval)})}),function(e,t){"use strict";"function"==typeof define&&define.amd?define(["jquery"],t):t("object"==typeof exports?require("jquery"):e.jQuery)}(this,function(e){"use strict";var t=function(){};t.prototype.redirect=function(e,t,o){return o.location?(window.location.href=o.location,!1):void 0},t.prototype.replace=function(t,o,a){e(o.data("replace")).replaceWith(a.html)},t.prototype.replaceClosest=function(e,t,o){t.closest(t.data("replace-closest")).replaceWith(o.html)},t.prototype.replaceInner=function(t,o,a){e(o.data("replace-inner")).html(a.html)},t.prototype.replaceClosestInner=function(e,t,o){t.closest(t.data("replace-closest-inner")).html(o.html)},t.prototype.append=function(t,o,a){e(o.data("append")).append(a.html)},t.prototype.prepend=function(t,o,a){e(o.data("prepend")).prepend(a.html)},t.prototype.refresh=function(t,o){e.each(e(o.data("refresh")),function(t,o){e.getJSON(e(o).data("refresh-url"),function(t){e(o).replaceWith(t.html)})})},t.prototype.refreshClosest=function(t,o){e.each(e(o.data("refresh-closest")),function(t,a){e.getJSON(e(a).data("refresh-url"),function(t){o.closest(e(a)).replaceWith(t.html)})})},t.prototype.clear=function(t,o){e(o.data("clear")).html("")},t.prototype.remove=function(t,o){e(o.data("remove")).remove()},t.prototype.clearClosest=function(e,t){t.closest(t.data("clear-closest")).html("")},t.prototype.removeClosest=function(e,t){t.closest(t.data("remove-closest")).remove()},t.prototype.fragments=function(t,o,a){a.fragments&&e.each(a.fragments,function(t,o){e(t).replaceWith(o)}),a["inner-fragments"]&&e.each(a["inner-fragments"],function(t,o){e(t).html(o)}),a["append-fragments"]&&e.each(a["append-fragments"],function(t,o){e(t).append(o)}),a["prepend-fragments"]&&e.each(a["prepend-fragments"],function(t,o){e(t).prepend(o)})},e(function(){e(document).on("eldarion-ajax:success",t.prototype.redirect),e(document).on("eldarion-ajax:success",t.prototype.fragments),e(document).on("eldarion-ajax:success","[data-replace]",t.prototype.replace),e(document).on("eldarion-ajax:success","[data-replace-closest]",t.prototype.replaceClosest),e(document).on("eldarion-ajax:success","[data-replace-inner]",t.prototype.replaceInner),e(document).on("eldarion-ajax:success","[data-replace-closest-inner]",t.prototype.replaceClosestInner),e(document).on("eldarion-ajax:success","[data-append]",t.prototype.append),e(document).on("eldarion-ajax:success","[data-prepend]",t.prototype.prepend),e(document).on("eldarion-ajax:success","[data-refresh]",t.prototype.refresh),e(document).on("eldarion-ajax:success","[data-refresh-closest]",t.prototype.refreshClosest),e(document).on("eldarion-ajax:success","[data-clear]",t.prototype.clear),e(document).on("eldarion-ajax:success","[data-remove]",t.prototype.remove),e(document).on("eldarion-ajax:success","[data-clear-closest]",t.prototype.clearClosest),e(document).on("eldarion-ajax:success","[data-remove-closest]",t.prototype.removeClosest)})}); \ No newline at end of file +if(document.all&&!window.setTimeout.isPolyfill){var __nativeST__=window.setTimeout;window.setTimeout=function(e,t){var o=Array.prototype.slice.call(arguments,2);return __nativeST__(e instanceof Function?function(){e.apply(null,o)}:e,t)},window.setTimeout.isPolyfill=!0}if(document.all&&!window.setInterval.isPolyfill){var __nativeSI__=window.setInterval;window.setInterval=function(e,t){var o=Array.prototype.slice.call(arguments,2);return __nativeSI__(e instanceof Function?function(){e.apply(null,o)}:e,t)},window.setInterval.isPolyfill=!0}!function(e,t){"use strict";"function"==typeof define&&define.amd?define(["jquery"],t):t("object"==typeof exports?require("jquery"):e.jQuery)}(this,function(e){"use strict";var t=function(){};t.prototype._ajax=function(t,o,a,n){t.trigger("eldarion-ajax:begin",[t]);var r=t.triggerHandler("eldarion-ajax:modify-data",n),c="application/x-www-form-urlencoded; charset=UTF-8",i=!0,p=!0,s="object"==typeof n;r&&(n=r),s&&(c=!1,i=!1,p=!1),e.ajax({url:o,type:a,dataType:"json",data:n,cache:p,processData:i,contentType:c,headers:{"X-Eldarion-Ajax":!0}}).done(function(e,o,a){e||(e={}),t.trigger("eldarion-ajax:success",[t,e,o,a])}).fail(function(e,o,a){t.trigger("eldarion-ajax:error",[t,e,o,a])}).always(function(o,a,n){e(document).trigger("eldarion-ajax:complete",[t,o,a,n])})},t.prototype.click=function(o){var a=e(this),n=a.attr("href"),r=a.data("method"),c=a.data("data"),i=null,p=null;r||(r="get"),c&&(i={},c.split(",").map(function(t){p=t.split(":"),0===p[1].indexOf("#")?i[p[0]]=e(p[1]).val():i[p[0]]=p[1]})),o.preventDefault(),t.prototype._ajax(a,n,r,i)},t.prototype.submit=function(o){var a=e(this),n=a.attr("action"),r=a.attr("method");o.preventDefault(),void 0===window.FormData?t.prototype._ajax(a,n,r,a.serialize()):t.prototype._ajax(a,n,r,new FormData(a[0]))},t.prototype.cancel=function(t){var o=e(this),a=o.attr("data-cancel-closest");t.preventDefault(),o.closest(a).remove()},t.prototype.timeout=function(o,a){var n=e(a),r=n.data("timeout"),c=n.data("url"),i=n.data("method");i||(i="get"),window.setTimeout(t.prototype._ajax,r,n,c,i,null)},t.prototype.interval=function(o,a){var n=e(a),r=n.data("interval"),c=n.data("url"),i=n.data("method");i||(i="get"),window.setInterval(t.prototype._ajax,r,n,c,i,null)},e(function(){e("body").on("click","a.ajax",t.prototype.click),e("body").on("submit","form.ajax",t.prototype.submit),e("body").on("click","a[data-cancel-closest]",t.prototype.cancel),e("[data-timeout]").each(t.prototype.timeout),e("[data-interval]").each(t.prototype.interval)})}),function(e,t){"use strict";"function"==typeof define&&define.amd?define(["jquery"],t):t("object"==typeof exports?require("jquery"):e.jQuery)}(this,function(e){"use strict";var t=function(){};t.prototype.redirect=function(e,t,o){return o.location?(window.location.href=o.location,!1):void 0},t.prototype.replace=function(t,o,a){e(o.data("replace")).replaceWith(a.html)},t.prototype.replaceClosest=function(e,t,o){t.closest(t.data("replace-closest")).replaceWith(o.html)},t.prototype.replaceInner=function(t,o,a){e(o.data("replace-inner")).html(a.html)},t.prototype.replaceClosestInner=function(e,t,o){t.closest(t.data("replace-closest-inner")).html(o.html)},t.prototype.append=function(t,o,a){e(o.data("append")).append(a.html)},t.prototype.prepend=function(t,o,a){e(o.data("prepend")).prepend(a.html)},t.prototype.refresh=function(t,o){e.each(e(o.data("refresh")),function(t,o){e.getJSON(e(o).data("refresh-url"),function(t){e(o).replaceWith(t.html)})})},t.prototype.refreshClosest=function(t,o){e.each(e(o.data("refresh-closest")),function(t,a){e.getJSON(e(a).data("refresh-url"),function(t){o.closest(e(a)).replaceWith(t.html)})})},t.prototype.clear=function(t,o){e(o.data("clear")).html("")},t.prototype.remove=function(t,o){e(o.data("remove")).remove()},t.prototype.clearClosest=function(e,t){t.closest(t.data("clear-closest")).html("")},t.prototype.removeClosest=function(e,t){t.closest(t.data("remove-closest")).remove()},t.prototype.fragments=function(t,o,a){a.fragments&&e.each(a.fragments,function(t,o){e(t).replaceWith(o)}),a["inner-fragments"]&&e.each(a["inner-fragments"],function(t,o){e(t).html(o)}),a["append-fragments"]&&e.each(a["append-fragments"],function(t,o){e(t).append(o)}),a["prepend-fragments"]&&e.each(a["prepend-fragments"],function(t,o){e(t).prepend(o)})},e(function(){e(document).on("eldarion-ajax:success",t.prototype.redirect),e(document).on("eldarion-ajax:success",t.prototype.fragments),e(document).on("eldarion-ajax:success","[data-replace]",t.prototype.replace),e(document).on("eldarion-ajax:success","[data-replace-closest]",t.prototype.replaceClosest),e(document).on("eldarion-ajax:success","[data-replace-inner]",t.prototype.replaceInner),e(document).on("eldarion-ajax:success","[data-replace-closest-inner]",t.prototype.replaceClosestInner),e(document).on("eldarion-ajax:success","[data-append]",t.prototype.append),e(document).on("eldarion-ajax:success","[data-prepend]",t.prototype.prepend),e(document).on("eldarion-ajax:success","[data-refresh]",t.prototype.refresh),e(document).on("eldarion-ajax:success","[data-refresh-closest]",t.prototype.refreshClosest),e(document).on("eldarion-ajax:success","[data-clear]",t.prototype.clear),e(document).on("eldarion-ajax:success","[data-remove]",t.prototype.remove),e(document).on("eldarion-ajax:success","[data-clear-closest]",t.prototype.clearClosest),e(document).on("eldarion-ajax:success","[data-remove-closest]",t.prototype.removeClosest)})}); diff --git a/package.json b/package.json index 8ed2e44..74410f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eldarion-ajax", - "version": "0.14.0", + "version": "0.15.0", "description": "a library for adding declarative ajax functionality to your website", "main": "js/eldarion-ajax.min.js", "directories": { @@ -20,7 +20,7 @@ "license": "BSD", "readmeFilename": "README.md", "dependencies": { - "jquery": "*" + "jquery": ">=1.8.3" }, "devDependencies": { "jshint": "2.1.4"