V
V
Vasily2016-12-14 06:07:09
JavaScript
Vasily, 2016-12-14 06:07:09

How to reload a file with JavaScript when re-AJAX-rendering the same view-file containing a link to this JavaScript-file?

I made a modal window, wrapped its contents in . Inside this block, I implemented navigation between three pages (view-files): view1 view2 view3 Moving forward through the pages is done by clicking on elements (buttons or selecting an element from the list), in the opposite direction - using the elements at the top of the page. To move from one page to another, an ajax request is generated to a specific controller action, and renderAjax is returned, which replaces the data inside the block with a view file . In the displayed views there are JavaScript scripts connected as directly:<div id='modalContent'>
7a138ecf6c764ae39552958d0f9d4b93.JPG
a38e5310dbe3473c8362767039e460db.JPG
af20596c98ab4b1da69fe4f3af5ccb65.JPG
<div id='modalContent'>

$script = <<< JS
$('.res').click(function(event) {        
    $.ajax({
        type: "GET",
        url: "load-resources?kind_of_res_id="+$(this).data("action"),
        success: function(data) {
            $("#modalContent").html(data)
        }
    })
});
JS;
$this->registerJs($script);


and as a file, like this:
$this->registerJsFile(
    '/basic_yii/web/scripts/resource/computer.js',
    ['depends'=>'app\assets\AppAsset']
);


The problem is that when any view file is rendered for the first time, the scripts from the file connected to this view work fine (they are connected by a GET request via ajax, this can be seen in the browser debugger), and if, for example, you make an Ajax transition to the previous view (click on top) and load the current one again (click on the form), local scripts work, but scripts from the file no longer exist (and the file is not reloaded, there is no GET request, although the element with the address of this site is present on the page). What could be the problem? How to force the file to load again when returning to the page? <script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Karavaev, 2016-12-14
@parazitl2

Do not try to use your options for loading dynamic content in Yii2. There is pjax, which will take care of everything. Read and try to screw it for your purpose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question