简体   繁体   English

如何检查是否加载了部分视图,然后加载javascript

[英]how to check if partial view is loaded and then load javascript

I have a query regarding an issue. 我有关于一个问题的查询。 As my application using Partial views and these partial views are loaded by ajax call and each partial view usage javascript and include js file, which eventually calls database to bind the data for that particular view. 当我的应用程序使用部分视图时,这些部分视图通过ajax调用以及每个部分视图的用法javascript和include js文件加载,该文件最终会调用数据库为该特定视图绑定数据。 Now, loading the view is taking more time than expected, as it loads js file and that js file makes another call to server to pull the records. 现在,加载视图所花费的时间比预期要多,因为它加载了js文件,并且该js文件再次调用服务器以提取记录。 I want my view to be loaded and then js file which makes db call to bind data. 我希望先加载我的视图,然后再加载js文件,该文件使数据库调用绑定数据。 something like below - 如下所示-

If(partialview is loaded) load js file , which will make ajax call and db eventually to bind data. 如果(partialview已加载)加载js文件,这将使ajax调用和db最终绑定数据。

This will at least load the view and user will have something to see instead of waiting for blank background with loader. 这至少将加载视图,并且用户将看到一些东西,而不是等待加载器的空白背景。 Below is the script through which i am loading PartialView. 以下是我加载PartialView的脚本。

    function loadview(action, hassubmenu, _fromtab) {
    if (hassubmenu == 'true') {
        return false;
    }
    if (!_fromtab) {
        $('.process').show();
        $('.mainbody').html('');
    }
    // call ajax to load view
    $.ajax({
        url: urlheader + "Home/LoadView/",
        type: 'POST',
        data: {
            'view': action
        },
        success: function (data) {
            // This outputs the result of the ajax request                       
            $('.process').hide();
            if (!_fromtab) {
                $('.mainbody').html('').html(data);
                // disable appfilter from start screen
                var ostype = $('select#selection').find('option:selected').data('ostype');
                var did = $('select#selection').find('option:selected').val();
                if (ostype.toLowerCase() == 'ios' && action == 'Start') {
                    $('div.appfilter').hide();
                    $('#liAppFilter').hide();
                }
                getsyncinfo(did, false);
                if (_currenttab != undefined) {
                    reloadCurrentFilterTab(_currenttab);
                }
            }
            else
                $('.chicoAppsUrlsDetails').html('').html(data);
        },
        error: function (errorThrown) {
            console.log(errorThrown);
        }
    });
}

If I understand correctly, js events aren't firing for html inside your partial views (those of which are ajaxed) 如果我理解正确,部分事件中的html不会触发js事件(这些事件会被取消)

This is because the html being loaded onto the page is coming after the js (which is binding your events). 这是因为正在加载到页面上的html在js之后(绑定了您的事件)。

You need to place any events on doms inside the partial view onto the document element instead with the jquery on method to specify a selector. 您需要将部分视图中dom上的所有事件放置在document元素上,而不是使用jquery on方法来指定选择器。

eg. 例如。

$(document).on('click', '.some-class', function(){
  //do stuff
});

this way, you can add partials to the page with ajax and the doms within the partial will still fire events. 这样,您可以使用ajax向页面中添加局部对象,并且局部对象中的dom仍然会触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM