locked
LightSwitch 2013 - Navige inside 'created' function RRS feed

  • Question

  • Hey,

    I'm building a LightSwitch 2013 HTML application.

    I have 2 screens in my Screens folder:

    1. FirstScreen

    2. SecondScreen

    In the FirstScreen's created function i'm checking if the user has permission to see this screen:

    If he has permission --> continue.

    If he doesn't have permission --> navigate to SecondScreen by doing myapp.showSecondScreen();

    The problem is that if the user doesn't have permission, the navigation doesn't work - "Cannot perform this action while a navigation is in progress."

    Can i navigate to SecondScreen from the FirstScreen's created function in some way?

    It's really important for me to do that in the created function...

    Thanks.

    Sunday, March 27, 2016 10:21 AM

Answers

  • Hi Yaron,

    If you're happy to delve into extending the LightSwitch library, you could use the second technique I cover in the following StackOverflow post: -

    LightSwitch Tabbed screen in Browse template

    This will allow you to implement the following event in your screen:

    myapp.FirstScreen.navigationComplete = function (screen, navigationUnit, intialNavigation, selectedTab) {
        if (intialNavigation) {
            if (true === true) /* Replace with your permission check */ {
                myapp.showSecondScreen();
            }
        }
    };

    You can also use the pagechange approach covered at the start of the StackOverflow post by introducing a short delay as follows: 

    myapp.FirstScreen.created = function (screen) {
        // Write code here.
        $(window).one("pagechange", function (e, data) {
            setTimeout(function () {
                if (true === true) /* Replace with your permission check */ {
                    myapp.showSecondScreen();
                }
            }, 250);
        });
    };
    

    This 250 millisecond delay should allow the navigation to the first page to fully complete.

    HTH,

    Chris

    • Marked as answer by Angie Xu Tuesday, April 5, 2016 8:56 AM
    Monday, March 28, 2016 12:55 PM

All replies

  • Hi Yaron Y,

    According to your description, I try navigate to the second screen with this.navigate() method and this.window.location.href, but both of them are failed.

    But if I replace the parameter of this.navigate() to www.microsoft.com, it can navigate to the url successful. Then I find in LightSwitch, if we navigate to another screen, the URL will add a "#ScrondScreen/[[dee7135da]]", which is post a new request. This is why it will tell us "Cannot perform this action while a navigation is in progress".

    So I suggest you use window.open() method to open a new windows, which is create a new process and then close previous window. Please refer to following code.

    myapp.BrowseTasks.created = function (screen) {
       //open a new windows to navigate to the screen you want
        window.open("your screen url");
        //close current window
        window.close();
    };

    Best Regards,
    Weiwei


    Monday, March 28, 2016 9:22 AM
    Moderator
  • Hey Weiwei,

    Thank you for your reply.

    I am using myapp.showSecondScreen(); and it doesn't work for me (of course - inside the created funciton).

    I'v also tried with this.navigate() and it also doesn't work.

    Is there an other way?

    Thanks.

    Monday, March 28, 2016 10:19 AM
  • Hi Yaron,

    If you're happy to delve into extending the LightSwitch library, you could use the second technique I cover in the following StackOverflow post: -

    LightSwitch Tabbed screen in Browse template

    This will allow you to implement the following event in your screen:

    myapp.FirstScreen.navigationComplete = function (screen, navigationUnit, intialNavigation, selectedTab) {
        if (intialNavigation) {
            if (true === true) /* Replace with your permission check */ {
                myapp.showSecondScreen();
            }
        }
    };

    You can also use the pagechange approach covered at the start of the StackOverflow post by introducing a short delay as follows: 

    myapp.FirstScreen.created = function (screen) {
        // Write code here.
        $(window).one("pagechange", function (e, data) {
            setTimeout(function () {
                if (true === true) /* Replace with your permission check */ {
                    myapp.showSecondScreen();
                }
            }, 250);
        });
    };
    

    This 250 millisecond delay should allow the navigation to the first page to fully complete.

    HTH,

    Chris

    • Marked as answer by Angie Xu Tuesday, April 5, 2016 8:56 AM
    Monday, March 28, 2016 12:55 PM
  • For a little background as to why this is necessary...

      When the _created method fires, the app is in the process of navigation to that screen.  Attempts to navigate will fail until the current navigation completes.  The jQuery  .one() function registers a 'one-time' event handler to the specified event ("pagechange" event in this case).  This will fire the code in your callback function once (next time pagechange event occurs) then dispose of the event handler so window object is not polluted, I believe. 

    Apparently like many other, cases in msls, a small setTimeout is a safety measure ensuring 'other stuff' is completed too.

    Chris' advanced technique fixes the deficiencies of _created method by introducing a navigationCompleted event to the screen.

    HTH,

    Josh

    Monday, March 28, 2016 1:36 PM