11 Replies Latest reply on Jan 9, 2013 8:12 AM by Benedict Kennedy

    check for ET1 Network connection on document ready

      I am using the following code to check network connection:

       

      var isOnline;

       

       

      network.networkEvent = "onNetworkEvent(%json)";

          network.start();

      alert(isOnline);

       

      // following function is written in a seperate JS file, which I am using in my index.html page.

      function onNetworkEvent(jsonObject){

      Update(jsonObject.connectionInformation == "Connected");

       

      }

       

      function Update(connected){

      if(connected){

        isOnline= true;

      }

      }

       

       

      I am setting the isOnline to true in onNetworEvent. but as it is an asynchronous call, isOnline is undefined as it being called before the json call is complete. I need to use callback function but not able to identify where to use the callback.

        • Re: check for ET1 Network connection on document ready
          Benedict Kennedy

          Hi Yogesh,

           

          If I understand you correctly, you want an alert to pop up on page load saying whether the device is connected or not? If so, the simplest way would be to do this:

           

          var isOnline;
          
          network.networkEvent = "onNetworkEvent(%json)";
          network.start();
          //alert(isOnline); move this line into the onNetworkEvent function
          
          // following function is written in a seperate JS file, which I am using in my index.html page.
          function onNetworkEvent(jsonObject){
               var isFirstTime = false;
          
               if(isOnline == undefined) { // if this is yet to be set, then this is the first time
                    isFirstTime = true;
               }
          
               Update(jsonObject.connectionInformation == "Connected");
          
               if(isFirstTime) { // if first time, then alert result
                    alert(isOnline);
               }
          }
          
          function Update(connected){
               if(connected){
                     isOnline= true;
               }
          }
          

           

          Here, I have moved the alert line into the (asynchronous) onNetworkEvent function and added some conditional code so that it only fires once.

           

          Is that what you were looking for?

          Regards,

          Ben

            • Re: check for ET1 Network connection on document ready

              Thank you for the reply.

              Actually I amchecking the network connection on document.ready. I am using the isOnline varible.

              as the call to network API is asychronous I am not able to get the varible isOnline, I need to wait till my callback is returned.

               

              var isOnline;

              $(document).ready

                   (

                        initdevice(); // using ET1 device....

                   if (isOnline)

                        {

                       syncdata();  // here I am synchronizing data with server when network existst

                        }

                     );

                        function initdevice() // function to intialize ET1 device.

                {

                 initDatabase(); // database intialization

                

                  if((navigator.userAgent.match(new RegExp("MotorolaWebKit"))))

                 {

                  isRhoElements = true;

                 }  

                 if(isRhoElements) // if device is ET1

                 {

                  network.networkEvent = "onNetworkEvent(%json)"; // here i am setting isOnline to true in network.js file. but I am not able to get isOnline value as it is asychronous call.

              // I need to use a callback but not able to.

                  network.start();

                 }

                 else

                   {

                  isOnline =  navigator.onLine; // when using HTML browser isOnline is true.

                 }

                }

               

              please kindly help me on the same.

               

              Thanks,

              Yogesh

                • Re: check for ET1 Network connection on document ready
                  Benedict Kennedy

                  A simple way would be to move the syncdata() call (if in RhoElements) into the onNetworkEvent function like this:

                   

                  var isOnline;
                  $(document).ready(function()
                  {
                      initdevice(); // using ET1 device....
                      if (!isRhoElements && isOnline)
                      {
                          syncdata();  // here I am synchronizing data with server when network existst
                      }
                  });
                  
                  function initdevice() // function to intialize ET1 device.
                  {
                      initDatabase(); // database intialization
                  
                      if((navigator.userAgent.match(new RegExp("MotorolaWebKit"))))
                      {
                          isRhoElements = true;
                      }  
                      if(isRhoElements) // if device is ET1
                      {
                          network.networkEvent = "onNetworkEvent(%json)"; // here i am setting isOnline to true in network.js file. but I am not able to get isOnline value as it is asychronous call.
                  //        I need to use a callback but not able to.
                          network.start();
                      }
                      else
                      {
                          isOnline =  navigator.onLine; // when using HTML browser isOnline is true.
                      }
                  }
                  
                  /* In the other file */
                  function onNetworkEvent(jsonObject) {
                       if(isOnline == undefined) { // if this is yet to be set, then this is the first time  
                            isFirstTime = true;  
                       }
                      Update(jsonObject.connectionInformation == "Connected");
                       if(isFirstTime && isOnline) { // if first time and online then sync data  
                            syncdata();
                       }
                  }
                  
                  function Update(connected){
                       if(connected){
                            isOnline= true;
                  }
                  

                   

                  Regards,

                    • Re: check for ET1 Network connection on document ready

                      I dont want to call syncdata() in the other js file.

                      I want to wait till my network.networkEvent call is returend. I need to get the callback but I am not able to get it.

                       

                        • Re: check for ET1 Network connection on document ready
                          Benedict Kennedy

                          Synchronizing an asynchronous JavaScript event is generally not done as JavaScript shares the single thread of the HTML User Interface. Blocking the thread by waiting for a JavaScript event to fire will block the rendering and user interaction. There are third part JavaScript libraries that can synchronize Asynchronous calls, but this seems a heavy handed approach to what you are trying to do.

                           

                          A way you can achieve what you want, and by keeping the external js file generic (by not adding the syncdata() call) you can fire an HTML event when the networkEvent is fired:

                           

                          function onNetworkEvent(jsonObject) {
                                    Update(jsonObject.connectionInformation == "Connected");
                          }
                          
                          function Update(connected) {
                                    var updateRequired = false;
                          
                                    if(connected != isOnline){
                                              updateRequired = true;
                                    }
                          
                                 if(connected) {
                                              isOnline= true;
                                 }
                          
                                    if(updateRequired) {
                                              fireEvent('connectionStatusChanged');
                                    }
                          }
                          
                          function fireEvent(eventName) {
                                    var event;
                                    if (document.createEvent) {
                                              event = document.createEvent('HTMLEvents');
                                              event.initEvent(eventName, true, true);
                                    }
                                    else {
                                              event = document.createEventObject();
                                              event.eventType = eventName;
                                    }
                          
                                    event.eventName = eventName;
                          
                                    if (document.createEvent) {
                                              document.dispatchEvent(event);
                                    }
                                    else {
                                              document.fireEvent("on" + event.eventType, event);
                                    }
                          }
                          

                           

                          Then in the main JavaScript, you can subscribe to the event:

                          var isOnline;
                          var isRhoElements = false;
                          
                          $(document).ready( function () {
                              initdevice(); // using ET1 device....
                              if (!isRhoElements && isOnline)
                              {
                                  syncdata();  // here I am synchronizing data with server when network existst
                              }
                          });
                          
                          function initdevice() // function to intialize ET1 device.
                          {
                                    initDatabase(); // database intialization
                          
                                    if((navigator.userAgent.match(new RegExp("MotorolaWebKit"))))
                                    {
                                              isRhoElements = true;
                                    }  
                                    if(isRhoElements) // if device is ET1
                                    {
                                             document.addEventListener('connectionStateChanged', checkSync);
                                              network.networkEvent = "onNetworkEvent(%json)";
                                              network.start();
                                    }
                                    else
                                    {
                                              isOnline =  navigator.onLine; // when using HTML browser isOnline is true.
                                    }
                          }
                          
                          
                          function checkSync() {
                                    if(isOnline)
                                    {
                                              syncData();
                                    }
                                    //remove the event listener
                                    document.removeEventListener('connectionStateChanged', checkSync);
                          }
                          

                           

                          Regards,

                          Ben

                          1 of 1 people found this helpful
                  • Re: check for ET1 Network connection on document ready

                    Yogesh, you can use set_network_status_notify() ruby function to overcome this situation if you are using native application. call back attached with this function will return network states either 'connected' or 'disconnected' and you can call syncData() function when network states is only connected.

                     

                    Please check the rhomobile docs for set_network_status_notify() function.