locked
How to run multiple screen query using promise in LightSwitch html client? RRS feed

  • Question

  • I have a LightSwitch html screen that has a number of queries being added through Add Data Item. This is how I understand, and correct me if I am wrong.

    For each query added to screen, there will be getXXX() method generated on screen parameter in create method such as "myapp.Home.created = function (screen) {...}", Each  getXXX() will return a promise object that I can get results asynchronously.

    My questions how can run these getXXX() methods in parallel and folk into one final method when all these getXXX() methods finished or time out?  


    jl

    Thursday, August 22, 2013 4:34 AM

Answers

  • You should be ale to put a Promise inside a ".then(..."

    LightSwitch uses WinJs library for the Promise objects, so this may help:

    http://fknet.wordpress.com/2013/02/08/winjs-promises-lessons-learned/


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Joe Lee Friday, August 23, 2013 4:07 AM
    Thursday, August 22, 2013 12:54 PM
  • Thanks for the link. Spot on, I resolved the issue with more maintainable code as follow:

        var p2 = screen.getX1().then(function (results) {
            myapp.X1 = results.data;
        });
        var p3 = screen.getX2().then(function (results) {
            myapp.X2 = results.data[0];
        });
        var p6 = myapp.activeDataWorkspace.ApplicationData.Table1().execute().then(function (results) {
            myapp.HasRecord = results.results.length > 0;
        });

        WinJS.Promise.join([p6, p2,p3]).then(function (args) {
            resetShowingTabs(screen);
        }, function (er) {
            var e = er;
        });

    Let me know if you think I have done anything silly in the code


    jl

    • Marked as answer by Joe Lee Friday, August 23, 2013 4:07 AM
    Friday, August 23, 2013 4:07 AM

All replies

  • You should be ale to put a Promise inside a ".then(..."

    LightSwitch uses WinJs library for the Promise objects, so this may help:

    http://fknet.wordpress.com/2013/02/08/winjs-promises-lessons-learned/


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Joe Lee Friday, August 23, 2013 4:07 AM
    Thursday, August 22, 2013 12:54 PM
  • The bigger question being, is a promise chain the best way to perform multiple queries?  Each get...() promise call requires a round-trip to the server, and when structured properly has to do it sequentially, taking longer and longer with each added promise and slowing down your application.

    So if you were thinking of doing something like this:

    var result = screen.getX( ... ).then(function() { screen.getY( ... ) }).then(function() { screen.getZ( ... ) }) etc.

    Don't do it!

    Instead, comprise your View so that LightSwitch obtains the information you need for you in 1 server trip, or if that is not possible, use LightSwitch's query syntax with .expand() or use the $expand option in a $.get() or OData.read() to eager load your query results in one trip.

    Thursday, August 22, 2013 3:21 PM
  • Thanks for the link. Spot on, I resolved the issue with more maintainable code as follow:

        var p2 = screen.getX1().then(function (results) {
            myapp.X1 = results.data;
        });
        var p3 = screen.getX2().then(function (results) {
            myapp.X2 = results.data[0];
        });
        var p6 = myapp.activeDataWorkspace.ApplicationData.Table1().execute().then(function (results) {
            myapp.HasRecord = results.results.length > 0;
        });

        WinJS.Promise.join([p6, p2,p3]).then(function (args) {
            resetShowingTabs(screen);
        }, function (er) {
            var e = er;
        });

    Let me know if you think I have done anything silly in the code


    jl

    • Marked as answer by Joe Lee Friday, August 23, 2013 4:07 AM
    Friday, August 23, 2013 4:07 AM