Is there any way to manipulate js file through asset bundling process?
I rather to generate URLs in js files with Url::to() helper or manipulate the script using php.
I need such thing but in js file :
<script>
$(this).click(function(){
var foo = foo;
var url = <?= Url::to(someURL) ?>
.
.
});
.
.
</script>
softark
(Softark)
August 12, 2015, 8:14am
2
IMO modifying js file in an asset bundle per request is not a very good idea. It’s better to keep your js file in an asset bundle static, just like the most of the js libraries like jQuery, jQueryUI, bootstrap … etc.
Note that you may call a function in your js library with dynamic parameters specifying urls and/or html elements.
For example, in your static js file:
function clickEventHandler(element, url) {
$('body').on('click', element, function(event) {
$.ajax({
url: url,
...
});
});
}
And in the inline javascript that you will register in your view using "$this->registerJs()":
...
clickEventHandler($('some-selector'), '<?= Url::to('someUrl') ?>');
softark:
IMO modifying js file in an asset bundle per request is not a very good idea. It’s better to keep your js file in an asset bundle static, just like the most of the js libraries like jQuery, jQueryUI, bootstrap … etc.
Note that you may call a function in your js library with dynamic parameters specifying urls and/or html elements.
For example, in your static js file:
function clickEventHandler(element, url) {
$('body').on('click', element, function(event) {
$.ajax({
url: url,
...
});
});
}
And in the inline javascript that you will register in your view using "$this->registerJs()":
...
clickEventHandler($('some-selector'), '<?= Url::to('someUrl') ?>');
Thank you. It was very helpful.