From b17112842d41bf272a6b9a340de70bf2a8919eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Tue, 4 Aug 2015 12:52:09 +0200 Subject: [PATCH 1/3] Add event "pjax:updateOptions" to update options according to content --- jquery.pjax.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/jquery.pjax.js b/jquery.pjax.js index 28390ffe..db388236 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -169,6 +169,7 @@ function handleSubmit(event, container, options) { // // Returns whatever $.ajax returns. function pjax(options) { + var context; options = $.extend(true, {}, $.ajaxSettings, pjax.defaults, options) if ($.isFunction(options.url)) { @@ -200,6 +201,21 @@ function pjax(options) { return !event.isDefaultPrevented() } + function updateOptions(opts) { + var hasBeenPushed = options.push && !options.replace; + + // Update options + options = $.extend(true, {}, options, opts); + + // Update container + context = options.context = findContainerFor(options.container); + + // Replace state, we were not supposed to push + if (hasBeenPushed && !options.push) { + window.history.replaceState(pjax.state, pjax.state.title, pjax.state.url) + } + } + var timeoutTimer options.beforeSend = function(xhr, settings) { @@ -259,6 +275,8 @@ function pjax(options) { var latestVersion = xhr.getResponseHeader('X-PJAX-Version') + fire('pjax:updateOptions', [data, updateOptions], {}) + var container = extractContainer(data, xhr, options) var url = parseURL(container.url) From 460260bd6c3ed80713ba735b17f1b39cb8c7cc6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Tue, 4 Aug 2015 12:54:19 +0200 Subject: [PATCH 2/3] Change event name to "beforeApply" --- jquery.pjax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.pjax.js b/jquery.pjax.js index db388236..0697d774 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -275,7 +275,7 @@ function pjax(options) { var latestVersion = xhr.getResponseHeader('X-PJAX-Version') - fire('pjax:updateOptions', [data, updateOptions], {}) + fire('pjax:beforeApply', [data, updateOptions], {}) var container = extractContainer(data, xhr, options) From b722de5de23955984ac37f781e2ced41347c49b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Tue, 4 Aug 2015 14:55:17 +0200 Subject: [PATCH 3/3] Add xhr to pjax:beforeApply event --- jquery.pjax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.pjax.js b/jquery.pjax.js index 0697d774..4a7c402e 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -275,7 +275,7 @@ function pjax(options) { var latestVersion = xhr.getResponseHeader('X-PJAX-Version') - fire('pjax:beforeApply', [data, updateOptions], {}) + fire('pjax:beforeApply', [xhr, data, updateOptions], {}) var container = extractContainer(data, xhr, options)