Refresh View in block using js I have two different blocks.
One has a form and the other one is a view with some files uploaded from the form above.

On submit of the form I need to update the view results.
I thought I could do it using js but I can't make it work.

Please check my code. Any advice would be helpful!

Drupal.behaviors.myViewsRefresh = {
    attach: function( context , settings) {
        var viewName = 'application_documents';

        // Your views should be Ajax enabled
        var instances = Drupal.views.instances;
        var myViews;

        // then simply need to iterate through the various
        // views instances name to find the view
        $.each( instances , function getInstance( index, element){
            if(element.settings.view_name == viewName ){
                myViews = '.view-dom-id-' + element.settings.view_dom_id;
            }
        });

        $(document).once('mysubmitbutton').ajaxComplete(function(event, xhr, settings){
            if(typeof myViews !== 'undefined'){
                $(myViews).trigger('RefreshView');
            }
        });

    }
}


I can get into the ajaxComplete but the view doesn't reload. The view id is correct.

Thank you in advance.
The way you construct your myViews variable seems to be the reason this doesn't work for you.

What worked for me was changing this:

            myViews = '.view-dom-id-' + element.settings.view_dom_id;


to

            myViews = '.js-view-dom-id-' + element.settings.view_dom_id;


I just tried a simple version of that without any form submiting, simply calling this from my browser's console:

jQuery('.js-view-dom-id-c58e9660da223aaf7c1645b2746e0b2e16f6a2f3e1de74c8533e6ad6117613db').trigger('RefreshView')


and it worked as expected. As a final remark and as is commented in the code you provided, your views should be Ajax enabled, otherwise, this will not work.

Good luck!

P.S.: The most relevant piece of information from the codebase (this comment says it all):

// Add a trigger to update this view specifically. In order to trigger a
// refresh use the following code.
//
// @code
// $('.view-name').trigger('RefreshView');
// @endcode


(http://cgit.drupalcode.org/drupal/tree/core/modules/views/js/ajax_view.es6.js#n94)