wbraganca/yii2-tagsinput

registerJS View::POS_HEAD incompatible with Bloodhound

Opened this issue · 0 comments

When used in conjunction with a typeahead engine, the order of the registered JS is a problem:

<?php
$engine = new \dosamigos\typeahead\Bloodhound([
    'name' => 'countries',
    'clientOptions' => [
        'datumTokenizer' => new JsExpression('Bloodhound.tokenizers.whitespace'),
        'queryTokenizer' => new JsExpression('Bloodhound.tokenizers.whitespace'),
        'local' => new JsExpression(json_encode($countries))
    ]
]);
$this->registerJs($engine->getClientScript(), $this::POS_END);
?>
<?= $form->field($model, 'inclusive_countries')->widget(TagsinputWidget::classname(), [
        'clientOptions' => [
            'trimValue' => true,
            'allowDuplicates' => false,
            'freeInput' => false,
            'tagClass' => 'label label-warning',
            'typeaheadjs' => [
                'source' => $engine->getAdapterScript()
            ]
        ],
])?>

Causes this error: Uncaught ReferenceError: countries is not defined

also, trying to register the engine in POS_HEAD causes this error: Uncaught ReferenceError: Bloodhound is not defined because usually all JS assets in Yii2 are loaded at the end of the body.

So... we have a small egg chicken problem here, was wondering if you can just exclude the position from your registerJs call and everything will be positioned automatically by yii in the right order in the footer <script> tag.

Thank you!