diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 62ff029ac1..97f43bc949 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 Change Log ----------------------- - Bug #6351: Find MySQL FK constraints from `information_schema` tables instead of `SHOW CREATE TABLE` to improve reliability (nineinchnick) +- Bug #6363, #8301, #8582, #9566: Fixed data methods and PJAX issues when used together (derekisbusy) - Bug #8723: Fixed `yii\helpers\VarDumper::export()` unable to export circle referenced objects with `Closure` (klimov-paul) - Bug #9108: Negative number resulted in no formatting when using `Formatter::asSize()` or `Formatter::asShortSize` (nxnx, cebe) - Bug #9288: Fixed `FileHelper::createDirectory` directory creation to be concurrency friendly (dynasource) diff --git a/framework/assets/yii.js b/framework/assets/yii.js index ad798d1154..ccf1c96181 100644 --- a/framework/assets/yii.js +++ b/framework/assets/yii.js @@ -143,16 +143,51 @@ yii = (function ($) { * * @param $e the jQuery representation of the element */ - handleAction: function ($e) { + handleAction: function ($e, event) { var method = $e.data('method'), $form = $e.closest('form'), action = $e.attr('href'), - params = $e.data('params'); + params = $e.data('params'), + pjax = $e.data('pjax'), + pjaxPushState = !!$e.data('pjax-push-state'), + pjaxReplaceState = !!$e.data('pjax-replace-state'), + pjaxTimeout = $e.data('pjax-timeout'), + pjaxScrollTo = $e.data('pjax-scrollto'), + pjaxContainer, + pjaxOptions = {}; + + if (pjax !== undefined && $.support.pjax) { + if ($e.data('pjax-container')) { + pjaxContainer = $e.data('pjax-container'); + } else { + pjaxContainer = $e.closest('[data-pjax-container=""]'); + } + // default to body if pjax container not found + if (!pjaxContainer.length) { + pjaxContainer = $('body'); + } + pjaxOptions = { + container: pjaxContainer, + push: pjaxPushState, + replace: pjaxReplaceState, + scrollTo: pjaxScrollTo, + timeout: pjaxTimeout + } + } if (method === undefined) { if (action && action != '#') { - window.location = action; + if (pjax !== undefined && $.support.pjax) { + $.pjax.click(event, pjaxOptions); + } else { + window.location = action; + } } else if ($e.is(':submit') && $form.length) { + if (pjax !== undefined && $.support.pjax) { + $form.on('submit',function(e){ + $.pjax.submit(e, pjaxOptions); + }) + } $form.trigger('submit'); } return; @@ -202,7 +237,11 @@ yii = (function ($) { oldAction = $form.attr('action'); $form.attr('action', action); } - + if (pjax !== undefined && $.support.pjax) { + $form.on('submit',function(e){ + $.pjax.submit(e, pjaxOptions); + }) + } $form.trigger('submit'); $.when($form.data('yiiSubmitFinalizePromise')).then( function () { @@ -291,10 +330,10 @@ yii = (function ($) { if (message !== undefined) { pub.confirm(message, function () { - pub.handleAction($this); + pub.handleAction($this, event); }); } else { - pub.handleAction($this); + pub.handleAction($this, event); } event.stopImmediatePropagation(); return false; diff --git a/framework/widgets/Pjax.php b/framework/widgets/Pjax.php index 754d2bcf93..c271ffcab4 100644 --- a/framework/widgets/Pjax.php +++ b/framework/widgets/Pjax.php @@ -109,7 +109,13 @@ class Pjax extends Widget echo Html::tag('title', Html::encode($view->title)); } } else { - echo Html::beginTag('div', $this->options); + echo Html::beginTag('div', array_merge([ + 'data-pjax-container' => '', + 'data-pjax-push-state' => $this->enablePushState, + 'data-pjax-replace-state' => $this->enableReplaceState, + 'data-pjax-timeout' => $this->timeout, + 'data-pjax-scrollto' => $this->scrollTo, + ], $this->options)); } }