check connection loop

4 posts, 0 answers
  1. André
    André avatar
    16 posts
    Member since:
    May 2015

    Posted 24 Jun 2015 Link to this post

    Hi, I'm developing a hybrid app and when no connection is detected, it redirect users to a view that should handle offline state.

    This is the code, what I'm doing wrong here? because I can't navigate after check the connection with sucess..no error is trhown..just nothing happens..

     CODE:

     

     <div data-role="view" id="vOffline" style="width: 100%; height: 100%" class="blackbg" data-before-show="connectionLoop"></div>

    <script>

     function connectionLoop() {

        if (navigator.connection.type != Connection.NONE) {

            kendo.mobile.application.navigate("#home");

           //also does not work using #:back..actually sometimes works and sometimes don't..I can't figure it out why..
            return;
        } else {
            setTimeout(function () {
                navigator.notification.alert("It seems that is no connection available. \nLets try again?", connectionLoop, username, "YEP!");
            }, 500)
        }
    }

    </script>

  2. André
    André avatar
    16 posts
    Member since:
    May 2015

    Posted 24 Jun 2015 Link to this post

    I've been trying other solutions with no luck..

    I think maybe it's because this "offline" view is called after a preventDefault on the initial view of app..

     Somehow I can't navigate out of it...

  3. André
    André avatar
    16 posts
    Member since:
    May 2015

    Posted 24 Jun 2015 Link to this post

    yeah, now I'm sure..if I execute e.preventDefault in some view, I  can't navigate to that view anymore using app.navigate..

  4. Martin Yankov
    Admin
    Martin Yankov avatar
    177 posts

    Posted 29 Jun 2015 Link to this post

    Hello Andre,

    Firstly, let me clarify that you don't need to use a loop to check connection yourself. If you enable the Network Connection plugin for your project, it provides two events - 'offline' and 'online'. You can simply use them instead of checking yourself every half a second. Here is an example:
    document.addEventListener('offline', function () {
        _onConnectionLost();
    }, false);

    Of course, you need to add this event listener after the deviceready event has fired.

    Regarding your problem with the navigation, can you please explain what you mean by "executing e.preventDefault in a view"? Do you mean in any event handler in the view? Can you please provide some code from a view that breaks the navigation, so that I can try to reproduce this?

    Thanks in advance for the cooperation.

    Regards,
    Martin Yankov
    Telerik
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
Back to Top