Can I tell if the view is being shown because the user went back in the show event?

2 posts, 0 answers
  1. Jonathan M
    Jonathan M avatar
    16 posts
    Member since:
    Sep 2012

    Posted 13 Nov 2012 Link to this post

    We've run into a situation where we need to be able to tell if a view is being shown because the user clicked 'back' (whether via an HTML button, or the back button on their device/browser). Is there any way to do this from within the view's show event?

    Thanks,
    Jonathan
  2. Jonathan M
    Jonathan M avatar
    16 posts
    Member since:
    Sep 2012

    Posted 12 Dec 2012 Link to this post

    I was able to get this working by adding the following code right after the app initialization:
    kendo.history.change(function (e) {
        app.isBrowserBack = true;
    });
     
    $(function () {
        var lastHistoryLength = app.pane.history.length;
     
        app.pane.bind('navigate', function (opt) {
            app.isBack = app.isBrowserBack || lastHistoryLength > app.pane.history.length;
            app.isBrowserBack = false;
            lastHistoryLength = app.pane.history.length;
        });
    });

    Now I can tell if a view is being shown due to clicking a back button like this:
    function show() {
        if (!app.isBack) {
             // Do stuff that we only want to do on forward navigation,
                // like load data from a web server
        }
        // Do common view show logic
    }

    This code has some limitations. The biggest being that it only works if you aren't using multiple panes (think phone apps and not tablet apps), but it works for our project. If anyone has any ideas for a more general-purpose solution, I'm all ears.

    Jonathan
  3. Kendo UI is VS 2017 Ready
Back to Top