Skip to content

Commit 579d5bb

Browse files
authored
Merge pull request #257 from Spell6inder/patch-1
Fix order of registered JS code block or files
2 parents 2cb9807 + 43ba4f5 commit 579d5bb

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Yii2 multiple input change log
66
- #246 accept `\Traversable` in model attribute for `yield` compatibility (bscheshirwork)
77
- #250 accept `\Traversable` in TableRenderer and ListRenderer for `yield` compatibility (bscheshirwork)
88
- #253 allow to omit a name for static column
9+
- #257 added `jsPositions` property for the `BaseRenderer` to set right order js-code in `jsInit` and `jsTemplates` (Spell6inder)
910

1011
2.17.0
1112
======

src/renderers/BaseRenderer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ abstract class BaseRenderer extends BaseObject implements RendererInterface
183183
*/
184184
public $jsExtraSettings = [];
185185

186+
/**
187+
* @var array List of the locations of registered JavaScript code block or files in right order.
188+
*/
189+
public $jsPositions = [View::POS_HEAD, View::POS_BEGIN, View::POS_END, View::POS_READY, View::POS_LOAD];
190+
186191
/**
187192
* @inheritdoc
188193
*/
@@ -370,8 +375,8 @@ public function render()
370375
// Collect all js scripts which has to be appended to page before initialization widget
371376
$jsInit = [];
372377
if (is_array($view->js)) {
373-
foreach ($view->js as $position => $scripts) {
374-
foreach ((array)$scripts as $key => $js) {
378+
foreach ($this->jsPositions as $position) {
379+
foreach (ArrayHelper::getValue($view->js, $position, []) as $key => $js) {
375380
if (isset($jsBefore[$position][$key])) {
376381
continue;
377382
}
@@ -386,8 +391,8 @@ public function render()
386391

387392
$jsTemplates = [];
388393
if (is_array($view->js)) {
389-
foreach ($view->js as $position => $scripts) {
390-
foreach ((array)$scripts as $key => $js) {
394+
foreach ($this->jsPositions as $position) {
395+
foreach (ArrayHelper::getValue($view->js, $position, []) as $key => $js) {
391396
if (isset($jsBefore[$position][$key])) {
392397
continue;
393398
}

0 commit comments

Comments
 (0)