0

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. 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. I want my view to be loaded and then js file which makes db call to bind data. something like below -

If(partialview is loaded) load js file , which will make ajax call and db eventually to bind data.

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.

    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);
        }
    });
}
6
  • how you are loading your partial view? Can you post the code?? Commented May 25, 2015 at 9:17
  • Hello Guruprasad, Sure i am going to edit my question with the code. which will help u understand Commented May 25, 2015 at 10:48
  • did you try $.when and .then functionality?? Commented May 25, 2015 at 11:06
  • No i didn't. Do i need to use this for loading the view. Coz all my js files are on partial view and loads accordingly to bind the data as and when it is loaded. Commented May 25, 2015 at 11:12
  • won't you to move your js files from partial views to bundle? Commented May 26, 2015 at 7:28

1 Answer 1

1

If I understand correctly, js events aren't firing for html inside your partial views (those of which are ajaxed)

This is because the html being loaded onto the page is coming after the js (which is binding your events).

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello John, I agree, but client's requirement is there no button at all and everything happens in background :-(. else your option would have been perfect.
ok, i see, then how about creating an Init() function for all things happening in the page load in your js and calling it on successful response of the ajax call?
Hello John, I have spent lot of time and concluded that actually my server side call made from ajax to load the view is taking time and that's why view take long time to show. Any reason why its taking time. Its just a simple jquery ajax call to call partialview?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.