thtmorais / yii2-pace

PACE (Progress Automatically Certain to Entertain) for Yii PHP Framework
MIT License
3 stars 1 forks source link

paceOptions can't be used because it's placed after pace-js is loaded (and tried to use it during loading) #5

Open fredeom opened 3 years ago

fredeom commented 3 years ago

yii2-pace/src/Pace.php

$this->getView()->registerJs('window.paceOptions=' . Json::encode(ArrayHelper::getValue($paceOptions,'options')), \yii\web\View::POS_BEGIN);

yii2-pace/src/PaceAsset.php

'position' => \yii\web\View::POS_HEAD

You place pace-js library in head and window.paceOptions={...} after that in body, so it isn't used.

Am I right?

thtmorais commented 3 years ago

Hello @fredeom , all right?

Yes, you right.

You can open a PR?

fredeom commented 3 years ago

Hello @fredeom , all right?

Yes, you right.

You can open a PR?

Yes, I can. Yes I would, but I don't.

I've added pace-js in asset bundle and registered my own script with paceOptions there. Added pace-js hooks on concrete links and am controling ajax calls.

{
  "require": {
    "npm-asset/pace-js": "^1.2",
  }
}
class AppAsset extends AssetBundle
{
    public $js = [
        'js/paceOptions.js',
    ];
}
class PaceAsset extends AssetBundle
{
    public $sourcePath = "@npm/pace-js/";

    public $js = [
        YII_DEBUG ? 'pace.js' : 'pace.min.js'
    ];

    public $jsOptions = [
        'position' => \yii\web\View::POS_END
    ];

    public $depends = [
        'app\assets\AppAsset'
    ];
}
AppAsset::register($this);
PaceAsset::register($this);
paceOptions = {
    ajax: true,
    startOnPageLoad: false,
}
$(".long-get-request").on("click", function (e) {
    e.preventDefault();
    $('#overlay').show();
    Pace.start();
    $.get(e.target.href, () => {
        $('#overlay').hide();
        Pace.stop();
    });
});