mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-13 12:49:04 +08:00
Merge branch 'master' of github.com:yiisoft/yii2
* 'master' of github.com:yiisoft/yii2: Added `yii\web\View::POS_LOAD`
This commit is contained in:
@@ -66,6 +66,11 @@ class View extends \yii\base\View
|
||||
* This means the JavaScript code block will be enclosed within `jQuery(document).ready()`.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user