2

I have some javascript files in my Symfonyt2 project that load some reources dynamically from the javascript file.
for example:

$('.records_list').DataTable({
    "language": {
        "url": "../shared/js/datatables.persian.json"
    }
});

the url ../shared/js/datatables.persian.json works in pages with url like /test but in pages with url ike /test2/action it fails.
How can i fix this?
Is there a tool like cssrewrite filter for assetic?
or can I make routes for such urls?

4

1 回答 1

1

如果您没有太多这样的情况,您可以使用 twig 将文件路径存储在隐藏的 div 中,例如:

<div id="file-store" style="display:none;" data-value="{{ asset('shared/js/persian.json') }}"></div>

并在 js 中检索它:

var sharedFile = $('#file-store').attr('data-value');
$('.records_list').DataTable({
    "language": {
        "url": sharedFile
    }
});

如果你有很多这样的情况,那么你可以使用FOSJsRoutingBundle

https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

于 2014-11-04T23:30:33.503 回答