Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initPjax called multiple times #323

Closed
ShadesOB opened this issue Jun 29, 2015 · 3 comments
Closed

initPjax called multiple times #323

ShadesOB opened this issue Jun 29, 2015 · 3 comments

Comments

@ShadesOB
Copy link

initPjax (client side) gets run on grid refresh. Which results in duplicate "pjax:complete" events being bound to grid object.

You can probably think of a better way to fix this. But attached below is a possible fix.

Located in yii2-grid\ColumnTrait.php line 311

protected function initPjax($script = '')
    {

        if (!$this->grid->pjax || empty($script)) {
            return;
        }
        $scriptId = md5($script);

        $cont = 'jQuery("#' . $this->grid->pjaxSettings['options']['id'] . '")';
        $grid = $this->grid->options['id'];
        $view = $this->grid->getView();
        $view->registerJs(

            //Stops duplicate scripts running
            $cont.'.each(function(){
                if($(this).data("pjax:complete:'.$scriptId.'")){
                    return true;
                }
                '.$cont.'.data("pjax:complete:'.$scriptId.'",1);
                '.$cont.'.on("pjax:complete", function(){'.$script.'});
            });'
        );
    }
@kartik-v
Copy link
Owner

Resolved via latest fix. Made it a little simpler for validating this.

@ShadesOB
Copy link
Author

This looks good.
Possible idea for future may be to bind these on initial page load and then never do it via pjax.
Pros: not sending unneeded js. No need to unbind \ rebind.
cons: if js changes it won't update till page reload - if current solution works, is it worth putting more time into it?

Also this is without going through any code to even see if it's possible.

Thanks for continued awesome work thought. :D

@kartik-v
Copy link
Owner

All of these are already initialized on page load. The problem is you are overwriting content via pjax which destroys the jquery elements and as a result all its event bindings. Hence this has to be parsed in something like pjax:complete (as is done now) to ensure the element exists for reinitializing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants