yii2-codemirror
yii2-codemirror copied to clipboard
Getting Instance and Attaching Events
Is it possible to get the instance of the editor? The issue I'm having is that my JavaScript file is added to the page before the inline JS is added that creates the editor. I need to be able to get it's instance so that I can attach a changes
event to it.
Try to move your script to the end, using "depends" feature of the Yii assets.
I'll never be able to put my script file after it, according to Yii: http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJs()-detail
$js = "CodeMirror.fromTextArea(document.getElementById('$id'), $settings)";
$view->registerJs($js);
I can recommend you 2 ways:
- use registerJsFile with "depends" instead of registerJs.
- $js = "initCodemirror();" Define "initCodemirror" function somewhere in the document body. Use setTimeout function if there is no CodeMirror initialized yet.
I'm not using the registerJs
method, I'm using an Asset. The snippet above is from the CodemirrorWidget class, showing that it appends that part to the end of the document according to Yii's documenation.
The second option isn't viable either. Depending on network speed and the computer's performance that in essence creates a race. Where CodeMirror must be defined by the time the timer runs out.
I think the better option would be to allow setting some additional JavaScript that could be added after all of the CodeMirror injection and initialization is done. Then I could put a wrapper around my JS to be called once CodeMirror has actually initialized.
Also, is it possible to use this with asset bundling? It would be better to be able to be able bundle all of the JS files into one file to reduce HTTP requests.