Commit 861c2b2f by Qiang Xue

Added `yii\web\View::POS_LOAD`

parent 7924e6d1
......@@ -236,7 +236,8 @@ determines where script should be inserted into the page. Possible values are:
- `View::POS_HEAD` for head section.
- `View::POS_BEGIN` for right after opening `<body>`.
- `View::POS_END` for right before closing `</body>`.
- `View::POS_READY` for executing code on document `ready` event. This one registers jQuery automatically.
- `View::POS_READY` for executing code on document `ready` event. This will register jQuery automatically.
- `View::POS_LOAD` for executing code on document `load` event. This will register jQuery automatically.
The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID
instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID.
......
......@@ -108,6 +108,7 @@ Yii Framework 2 Change Log
- Enh: Added ability to get incoming headers (dizews)
- Enh: Added `beforeRun()` and `afterRun()` to `yii\base\Action` (qiangxue)
- Enh: Added support for using timeZone with `yii\base\Formatter` (dizews)
- Enh: Added `yii\web\View::POS_LOAD` (qiangxue)
- Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue)
- Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
- Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue)
......
......@@ -67,6 +67,11 @@ class View extends \yii\base\View
*/
const POS_READY = 4;
/**
* The location of registered JavaScript code block.
* This means the JavaScript code block will be enclosed within `jQuery(window).load()`.
*/
const POS_LOAD = 5;
/**
* This is internally used as the placeholder for receiving the content registered for the head section.
*/
const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
......@@ -336,6 +341,8 @@ class View extends \yii\base\View
* - [[POS_HEAD]]: in the head section
* - [[POS_BEGIN]]: at the beginning of the body section
* - [[POS_END]]: at the end of the body section
* - [[POS_LOAD]]: enclosed within jQuery(window).load().
* Note that by using this position, the method will automatically register the jQuery js file.
* - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
* Note that by using this position, the method will automatically register the jQuery js file.
*
......@@ -347,7 +354,7 @@ class View extends \yii\base\View
{
$key = $key ?: md5($js);
$this->js[$position][$key] = $js;
if ($position === self::POS_READY) {
if ($position === self::POS_READY || $position === self::POS_LOAD) {
JqueryAsset::register($this);
}
}
......@@ -458,6 +465,10 @@ class View extends \yii\base\View
$js = "jQuery(document).ready(function(){\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
$lines[] = Html::script($js, ['type' => 'text/javascript']);
}
if (!empty($this->js[self::POS_LOAD])) {
$js = "jQuery(window).load(function(){\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
$lines[] = Html::script($js, ['type' => 'text/javascript']);
}
return empty($lines) ? '' : implode("\n", $lines);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment