1

I am working on a check in app, that uses a SQL server database to store and get the People if the app is online, and uses local storage to store people when it is offline , which will push these changes into the database once it comes back online.

my controller currently goes like this

function DemoController($scope,localStorageService,$http,$resource,$interval)

{

$scope.online = "";

$scope.checkConnection=function()

       {
           check=window.navigator.onLine;
           if(!check)
           {
             $scope.online="offline";
             return false;
           }

           else
           {
             $scope.online="online";
             return true;
           }

       }
if($scope.checkConnection() == true){

$scope.Get = function(){

  $http.get(url).success{function(res)
{
    $scope.People = res;

    $scope.setPeople();

       }

    }

//basically the edit and delete function for those people

$scope.setPeople = function() {};
$scope.deletePeople = function(){};


}
else if ($scope.checkConnection == false)
{

// use same methods/functions but with local storage

  $scope.pushAll = function (){ %http.put(url)}

  //here is the part i do not understand 

 if checkConnection switches from offline to online 

   {
     $scope.pushAll();
   }
}

$scope.intervalCheck = function()

 {
     var intervalPromise;
      intervalPromise = $interval($scope.checkConnection(), 250);

    };
$scope.intervalCheck();
}

And the other problem is that even if it switches from online to offline or vise-versa I doesn't switch the methods it uses (online/offline methods). I am not sure if there is a proper way to actually write this but i Hope there is, if this is too vague I can edit the post with more details.

  • Did you try to use a watcher on the onLine value ? It should be possible to use the Angular watchers to automatically switch. Or is it not your problem ? – sam Oct 13 '14 at 15:01
  • @sam Watchers require an input which this webapp does not use(there is no value that changes outside of the controller itself in order for the watcher to trigger.), so using watchers really does nothing for me I tried going that route, unless i've missed something. – Angel Petrov Oct 14 '14 at 14:20
  • This post seems to answer to your question http://stackoverflow.com/questions/3181080/how-to-detect-online-offline-event-cross-browser . It doesn't seem to exist any solution to automatically switch when the browser becomes offline. However nothing stops you to do a test before every request to check the connectivity and act accordingly. Not the best solution but a working one. – sam Oct 14 '14 at 15:15

1 Answers1

0

I think you should try using cookies for that. to store offline data but large data in cookies, is discouraged so be careful.