From 1a934eb5880ee42194fd1c87082753b26db5f67c Mon Sep 17 00:00:00 2001 From: Philip Horger Date: Wed, 3 Aug 2016 15:18:55 -0700 Subject: [PATCH 1/5] Use the loadEvent object to provide arbitrary $.get settings The jQuery AJAX API is pretty powerful. Rather than trying to wrap all of its abilities, for questionable gain, we can just use the load event object as the settings parameter. This lets downstream users override arbitrary settings, like `headers` or `cache` or `timeout`. The downside is if people are storing funky, non-url things in the load event, this will break their use case. But it's kind of a weird thing to do in the current IAS interface, so we probably don't have to be too paranoid about backwards compatibility. For people who only use event.url, or don't use the event object at all, they won't even know anything changed. --- src/jquery-ias.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index b01f190e..14afd873 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -182,12 +182,13 @@ delay = delay || this.defaultDelay; var loadEvent = { - url: url + url: url, + dataType: 'html' }; self.fire('load', [loadEvent]); - this.jsXhr = $.get(loadEvent.url, null, $.proxy(function(data) { + function xhrDoneCallback(data) { $itemContainer = $(this.itemsContainerSelector, data).eq(0); if (0 === $itemContainer.length) { $itemContainer = $(data).filter(this.itemsContainerSelector).eq(0); @@ -211,7 +212,9 @@ callback.call(self, data, items); } } - }, self), 'html'); + } + this.jsXhr = $.get(loadEvent) + .done($.proxy(xhrDoneCallback, self)); return this.jsXhr; }; From c81f466a8a8a248a508d5bee872d486ac6b97863 Mon Sep 17 00:00:00 2001 From: Philip Horger Date: Wed, 3 Aug 2016 15:54:51 -0700 Subject: [PATCH 2/5] Update the docs, now that loadEvent is more capable --- doc/events.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/events.md b/doc/events.md index b0a1784d..fa2a252b 100644 --- a/doc/events.md +++ b/doc/events.md @@ -24,11 +24,18 @@ The load event object contains the following properties. |-----------|---------------|-------------------------| | event.url | string | url that will be loaded | -Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned html by stripping everything outside the container element (header, footer, etc.). +Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned url by stripping everything outside the container element (header, footer, etc.). Because it is used as the settings object in $.get, you can also use this for more exotic configuration. ```javascript ias.on('load', function(event) { event.url = event.url + "?ajax=1"; + // alternatively... + event.data = { ajax: 1 }; + + // And as a more exotic example, timeout and HTTP auth + event.timeout = 7000; //ms + event.username = 'shirley'; + event.password = 'temple'; }) ``` From 7ea002013b1d331b31721ab254906fc322cd1d6e Mon Sep 17 00:00:00 2001 From: Philip Horger Date: Wed, 3 Aug 2016 16:49:54 -0700 Subject: [PATCH 3/5] Use $.ajax instead of $.get, for compatibility with older JQ --- src/jquery-ias.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 14afd873..60e78723 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -213,7 +213,7 @@ } } } - this.jsXhr = $.get(loadEvent) + this.jsXhr = $.ajax(loadEvent) .done($.proxy(xhrDoneCallback, self)); return this.jsXhr; From b6ea1cdbff28ece9a40be470503c21d07fb17059 Mon Sep 17 00:00:00 2001 From: fieg Date: Wed, 14 Mar 2018 21:03:34 +0100 Subject: [PATCH 4/5] small tweaks --- doc/events.md | 22 ++++++++++++++++------ src/jquery-ias.js | 7 +++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/doc/events.md b/doc/events.md index fa2a252b..5f1379fd 100644 --- a/doc/events.md +++ b/doc/events.md @@ -20,25 +20,35 @@ Triggered when a new page is about to be loaded from the server. The load event object contains the following properties. -| property | type | description | -|-----------|---------------|-------------------------| -| event.url | string | url that will be loaded | +| property | type | description | +|-------------------|--------|--------------------------------------------------------------------------------| +| event.url | string | url that will be loaded | +| event.ajaxOptions | object | options that are passed to [$.ajax method](http://api.jquery.com/jquery.ajax/) | -Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned url by stripping everything outside the container element (header, footer, etc.). Because it is used as the settings object in $.get, you can also use this for more exotic configuration. +Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned url by stripping everything outside the container element (header, footer, etc.). ```javascript ias.on('load', function(event) { event.url = event.url + "?ajax=1"; + // alternatively... - event.data = { ajax: 1 }; + event.ajaxOptions.data = { ajax: 1 }; +}) +``` + +The ajaxOptions property can also be used for more exotic configurations. - // And as a more exotic example, timeout and HTTP auth +```javascript +ias.on('load', function(event) { + // A more exotic example, timeout and HTTP auth event.timeout = 7000; //ms event.username = 'shirley'; event.password = 'temple'; }) ``` +See http://api.jquery.com/jquery.ajax/ for a complete list of options. + ### loaded | argument | type | description | diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 60e78723..bdf8f913 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -183,7 +183,9 @@ var loadEvent = { url: url, - dataType: 'html' + ajaxOptions: { + dataType: 'html' + } }; self.fire('load', [loadEvent]); @@ -213,7 +215,8 @@ } } } - this.jsXhr = $.ajax(loadEvent) + + this.jsXhr = $.ajax(loadEvent.url, loadEvent.ajaxOptions) .done($.proxy(xhrDoneCallback, self)); return this.jsXhr; From 000bddd04f5b8c6997e8d44960a9999d60e202b5 Mon Sep 17 00:00:00 2001 From: Jeroen Fiege Date: Wed, 14 Mar 2018 21:09:13 +0100 Subject: [PATCH 5/5] Update events.md --- doc/events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/events.md b/doc/events.md index 5f1379fd..c55a6089 100644 --- a/doc/events.md +++ b/doc/events.md @@ -25,7 +25,7 @@ The load event object contains the following properties. | event.url | string | url that will be loaded | | event.ajaxOptions | object | options that are passed to [$.ajax method](http://api.jquery.com/jquery.ajax/) | -Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned url by stripping everything outside the container element (header, footer, etc.). +Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned html by stripping everything outside the container element (header, footer, etc.). ```javascript ias.on('load', function(event) {