Skip to content

Commit

Permalink
add BootboxOverrideAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
xjflyttp committed Nov 17, 2016
1 parent e1fcb16 commit bc40401
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
22 changes: 5 additions & 17 deletions BootboxAsset.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
*
*
* @author xjflyttp <xjflyttp@gmail.com>
*/

Expand All @@ -10,36 +10,24 @@
use \Yii;
use yii\web\AssetBundle;

class BootboxAsset extends AssetBundle {
class BootboxAsset extends AssetBundle
{

public $sourcePath = '@bower/bootbox';
public $basePath = '@webroot/assets';
public $js = ['bootbox.js'];
public $depends = [
'yii\bootstrap\BootstrapPluginAsset'
];

/**
* Registers this asset bundle with a view.
* @param \yii\web\View $view the view to be registered with
* @return static the registered asset bundle instance
*/
public static function registerWithOverride($view)
{
$assetbundle = self::register($view);
Yii::$app->view->registerJs('
yii.confirm = function(message, ok, cancel) {
bootbox.confirm(message, function(result) {
if (result) { !ok || ok(); } else { !cancel || cancel(); }
});
}
');

if(Yii::$app->language !== null && strlen(Yii::$app->language)>=2) {
Yii::$app->view->registerJs('bootbox.setDefaults({locale: "'.substr(Yii::$app->language,0,2).'"});');
}

return $assetbundle;
return BootboxOverrideAsset::register($view);
}

}
33 changes: 33 additions & 0 deletions BootboxOverrideAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
*
* @author xjflyttp <xjflyttp@gmail.com>
*/

namespace xj\bootbox;

use \Yii;
use yii\web\AssetBundle;

class BootboxOverrideAsset extends BootboxAsset
{

public static function register($view)
{
$view->registerJs('
yii.confirm = function(message, ok, cancel) {
bootbox.confirm(message, function(result) {
if (result) { !ok || ok(); } else { !cancel || cancel(); }
});
}
');

if (Yii::$app->language !== null && strlen(Yii::$app->language) >= 2) {
$view->registerJs('bootbox.setDefaults({locale: "' . substr(Yii::$app->language, 0, 2) . '"});');
}

return parent::register($view); // TODO: Change the autogenerated stub
}

}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ use xj\bootbox\BootboxAsset;
BootboxAsset::register($this);

//register with replace Yii.confirm
BootboxOverrideAsset::register($this);
//OR
BootboxAsset::registerWithOverride($this);
```

2 comments on commit bc40401

@karakum
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution still requires call BootboxOverrideAsset::register($this); in layout.

Look at my solution:

<?php

namespace frontend\assets;

use xj\bootbox\BootboxAsset;
use Yii;

class BootboxYiiOverrideAsset extends BootboxAsset
{
    public function registerAssetFiles($view)
    {
        parent::registerAssetFiles($view);
        $view->registerJs('
            yii.confirm = function(message, ok, cancel) {
                bootbox.confirm(message, function(result) {
                    if (result) { !ok || ok(); } else { !cancel || cancel(); }
                });
            }
        ');

        if (Yii::$app->language !== null && strlen(Yii::$app->language) >= 2) {
            $view->registerJs('bootbox.setDefaults({locale: "' . substr(Yii::$app->language, 0, 2) . '"});');
        }
    }
}

and AppAsset:

<?php

namespace frontend\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    ...

    public $depends = [
    ...
        'frontend\assets\BootboxYiiOverrideAsset',
    ];
}

It is no need to register bundle in layout!

@xjflyttp
Copy link
Owner Author

@xjflyttp xjflyttp commented on bc40401 Nov 18, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.