nicoknoll/ProcessPageDelete

javascript get's added to all backend admin pages

somatonic opened this issue · 12 comments

I have admin page to render in backend, they're normal pages. When this module is installed I get this script include from your module parsed in in my html:

...<script type="text/javascript" src="/site/modules/ProcessPageDelete/ProcessPageDeleteCheck.js?v=1"></script></head>
<body>

The script addition should change and added via $config->scripts->add(); and not on every and all admin ProcessPageView::execute.

It's not possible to use $config->scripts->add(); because it's an autoload module and then the javascript would be added before jquery gets added.

Yeah that's part of how this module is done. One again I'll say it: Process modules shouldn't be autoload.

You already use manually adding script on hook ProcessPageView::execute which means on every page view.

To avoid it you may need to reconsider how the module works.

I added the autoload condition to only load on ProcessPageList. So Javascript shouldn't get loaded on every page anymore..

Ah yes I see.

But I would refactor and move that part of adding script and action to a new basic Wire module and make that autoload, and keep the process module load on as needed/requested.

But if I make that autoload same problem with the javascript. Because it's needed for the autoload module not for the process module...

But you said the problem is fixed with the conditional autoload.
I'm just saying for the separation - may be a good practice. So you can leave the Process module not autoload.

I thought so but I was wrong. With that condition it doesn't get added anymore.

So I'll probably have to change it back.

Why not use the hook to add a script inline after the output of a ProcessPageList::execute

'autoload' => 'process=ProcessPageList'

// add the jQuery hook
$this->addHookAfter('ProcessPageList::execute', $this, 'addJavascript');

public function addJavascript(HookEvent $event) {
    // js path
    $class = $this->className();
    $info = $this->getModuleInfo();
    $version = (int) $info['version'];
    $path = $this->config->urls->$class . "ProcessPageDeleteCheck.js?v=$version";

    $event->return .= '<script type="text/javascript" src="'.$path.'"></script>';
}

Maybe even just a script inline and not a external file, it's only small.
That works a lot easier and more to the point, better, cause a autoload = ProcessPageList makes it only load when the ProcessPageList is loaded and that is later in the process than page view I guess.

Just tested and there seems a problem when doing this with getting loaded on every ajax request for the ProcessPageList... So the addscript should account for that:

public function addJavascript(HookEvent $event) {
    // js path

    if($this->config->ajax) return; // so it only gets added on first time markup output

    $class = $this->className();
    $info = $this->getModuleInfo();
    $version = (int) $info['version'];
    $path = $this->config->urls->$class . "ProcessPageDeleteCheck.js?v=$version";

    $event->return .= '<script src="'.$path.'"></script>';
}

You're great! Recognized the same problem but didn't know what to. Implemented your code know and everything is working. Thanks!

I think it's fine to keep everything in one module if you're fine with that and would close this issue.