Search code examples
javascriptjqueryasynchronoussetintervalvisual-studio-lightswitch

Execute LightSwitch Javascript Asynchronously


I have a Back Button within my Lightswitch Application which I need to link directly to a specific screen. The linked screen requires a ProjectID value which I supply from the page which you are clicking Back on.

The problem is that when I click back, I am brought to the right page but no records display until after a page refresh. I need to refresh the page AFTER the load is complete

I have been messing with setTimeout and setInterval with no luck. Can this be done within my button click, or should I be using a different navigation method entirely?

Code with the setTimeout (doesn't navigate backward):

myapp.ViewRecordDetails.BackButton_execute = function (screen) {
    // window.history.back();
    showDetails = function () {
        setTimeout(function () {
            window.location.reload(true);
        }, 0)
    }
    window.location.reload(true);
};

Code which navigates back but needs the refresh to show data:

myapp.ViewRecordDetails.BackButton_execute = function (screen) {
    // window.history.back();
    myapp.showViewProjectDetails(screen.TBG_V_TimeLog_Detail), function () {
    }//Need to execute reload AFTER show screen has completed
};

You may notice that I have commented out widow.history.back();... this was working in the first iteration of my application, but there are now two different ways to land on the page so History is unreliable


Solution

  • Not sure how I didn't get this before..

    BobbyJ's response got me thinking about commitChanges which got me thinking about skipping javascript tricks, and using what I already had.

    This code works:

    myapp.ViewRecordDetails.BackButton_execute = function (screen) {
        // window.history.back();
        myapp.showViewProjectDetails(screen.TBG_V_TimeLog_Detail).then(function(){
             myapp.commitChanges()
                }).then(function () {
                    window.location.reload(true);//Need to execute reload AFTER show screen has completed
        })
    };
    

    This is all that I actually needed though:

    myapp.ViewRecordDetails.BackButton_execute = function (screen) {
        myapp.showViewProjectDetails(screen.TBG_V_TimeLog_Detail).then(function(){
                    window.location.reload(true);
        })
    };
    

    Just utilizing the .then() functionality. Pretty nifty