2

Premise:

i know that there are similar questions on StackOverflow, if i am asking this is because that solutions are not working in my case (or i am not able to apply them properly).

Situation:

I have an Angular js app with a map and a series of markers, which data and location are retrieved from a database. The app is working fine. I am using Angular-gm module: https://github.com/dylanfprice/angular-gm and i like it very much. When i click on the nav link i am redirected to the proper page with the map and i am seeing all the markers on the map.

The problem is that when i refresh the page, i loose all the markers, because that markers are retrieved with an $http call.

The code:

   // Initialising the markers array.
  $scope.markers= [];

  // Getting the list of markers through $http call.
  $http({
    url: 'main/json_get_markers',
    method: "POST",
   }).success(function (data) {
        $scope.markers = data;
     });

Possible solutions:

Searching in StackOverflow and google i have found some possible solutions:

  1. Using $route.reload();

    If it is the right solution, then the question is: How can I exactly apply $route.reload(); in this case? Because i don't understand where exactly i have to put it. If i put it in the controller it generate an infinite loop, in the meaning that the page is continuosly refreshing.. So where have to be putted exactly?

  2. Using ngStorage module: http://ngmodules.org/modules/ngStorag I see that is working fine in the demo, but i have tried to apply it in my case and was not able to obtain any result. So if this is the proper solution, then the question would be: How can i rewrite the $http call in order to save the results into the $localStorage?

FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • Have you tried creating a service that get the marker data and then inject it into your controller? Remember a service is a singleton and should have the ability to maintain the retrieved data. – Jared Reeves May 08 '14 at 13:46
  • 1
    `$route.reload` will not work in case of deep reloading (e.g `f5`); `localStorage` can only contain text data, so you have to use `JSON.stringify`/`JSON.parse`. – artur grzesiak May 08 '14 at 13:46
  • Thanks @JaredReeves for the tip. I am trying right now to do something with service but i am not able to make it working. Can you be so kind to make a code example for this particular case? – FrancescoMussi May 08 '14 at 13:56
  • thanks @arturgrzesiak. Can you please rewrite the code with your solution? I have tried $scope.markers = JSON.stringify(data); but is noe the right way.. – FrancescoMussi May 08 '14 at 14:06
  • here is an [example](http://www.ng-newsletter.com/posts/beginner2expert-services.html) that might help you out to develop a service for your particular case. I wont have time until later this evening to provide an example. – Jared Reeves May 08 '14 at 14:20
  • Ok, I am studying service right now. Thanks for the tips. I am thinking it can be the right way to approach this problem. If i will be able to implement a solution i will post as an answer, otherwise i will wait for help! – FrancescoMussi May 08 '14 at 14:25

2 Answers2

4

Ok, I think this if you make these changes it should fix the remaining issues when it wont always load the data.

myApp.factory('markersRepository', function($http){ 
    var markersRepository = {
       getAllMarkers: function(){
           var url = "main/json_get_markers";
           var promise = $http.get(url).then(function(response){
               return response.data;
           });
           return promise;
       }
    };
    return markersRepository;
});  

function MapController ($scope, markersRepository){
    markersRepository.getAllMarkers().then(function(markerData){
        $scope.markers= markerData
    });
});

I hope this helps --- Cheers!

Jared Reeves
  • 1,390
  • 2
  • 15
  • 29
  • Thank you very much for help! It works perfectly the 100% of times! I did not knew about promise. Just one thing: Is missing a { in the first line, after ($http). I have tried to edit but the edit couldn't be submitted. Thanks again! – FrancescoMussi May 08 '14 at 20:23
  • 1
    Sorry about that I made that edit ({). Good to hear that it is working for you. The promises are very useful, as are services. If you are new to angular I suggest that you really learn about services, directives and routing as they are some of the most powerful features in angular. – Jared Reeves May 08 '14 at 20:40
1

Ok, following the suggestion of @jaredReeves i have come out with this solution:

I have declared a service:

myApp.factory('markersRepository', function($http) 
{
  return 
  {
    getAllMarkers: function() 
    {
        var url = "main/json_get_markers";
        return $http.get(url);
    }
  };
});

And inject it into the Controller:

function MapController ($scope, markersRepository) 
{
  markersRepository.getAllMarkers().success(function(markers) {$scope.markers= markers});
}

It is working. When i refresh the page the markers are still there!

To be honest it is working the 95% of the times, sometimes i refresh and there are no markers. In this case i have to let pass some seconds and refresh again. The markers will then appear (but i don't know why..)

Anyway the problem i had is basically resolved. I don't know if this is the best solution. If you have better solutions i will mark your answer as correct.

FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178
  • 1
    on the cases where it is not working it might be because the data is returned after the page loads and angular does not update. You can either force an update with apply, or better yet convert the service to use promises like [THIS](http://stackoverflow.com/questions/12505760/angularjs-processing-http-response-in-service) read up on $q here [AngularDoc](https://docs.angularjs.org/api/ng/service/$q) – Jared Reeves May 08 '14 at 15:41