0

I am using a factory to get JSON data after a user logs in. If the username and password is correct it should pull JSON data from the api link. I have several controllers in my application that will need to use this data. I want to avoid sharing this factory with all my controllers because I do not want to keep "GETTING" data. I only want this factory to be used with the login controller.

How can I save the data from 'dummyData' and then all the other controllers can use that data without having to make a 'GET' request every time?

angular.module('ciscoImaDashboardApp').factory('dummyData', ['$q', '$http', function($q, $http) {
    var apiServices = {};

    apiServices.saveData = function(user,password) {
        var deferred = $q.defer();

        var req = $http({
            method: 'GET',
            url: 'http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/login/login/?username='+user+'&password='+password
        });

        req.success(function(response) {
            deferred.resolve(response);
        });

        req.error(function(err) {
            console.log("there was an error: "+err);
            deferred.reject(err);
        });

        return deferred.promise;    
    }

    return apiServices;

}]);
Neha Sohail
  • 239
  • 3
  • 19
  • You can use the [$cacheFactory](https://docs.angularjs.org/api/ng/service/$cacheFactory) and save the results after the first call – Alon Eitan Jan 06 '16 at 17:12
  • Have a look at my answer in this post: http://stackoverflow.com/questions/33968655/how-can-i-use-my-json-without-triggering-a-new-api-request-everytime-i-want-to-u/33969521#33969521 – sjokkogutten Jan 06 '16 at 17:15
  • can you use [`$rootScope`](http://astutejs.blogspot.in/2015/07/angularjs-what-is-rootscope.html)? an example [SO post](http://stackoverflow.com/questions/21444100/angularjs-how-to-http-get-data-and-store-it-in-service) – Navoneel Talukdar Jan 06 '16 at 17:15

2 Answers2

1

Create another value in your factory and store the data you GET. Then you can access that data from any controller by using dummyData.user or something similar.

Following best practices, you can do something like this:

function userService() {
    var service = {
        user: null,
        authenticateUser: authenticateUser
    };

    return service;

    function authenticateUser() {
        // your GET logic here

        // then set service.user to the successful response data
    };
}

So anywhere you inject userService you can do userService.user and you'll receive the user's data without having to make a second GET. It will essentially be stored in the current session. For example:

function dashboardController(userService) {
    var vm = this;
    vm.currentUser = userService.user;
}

Caveats

The problem with this approach is that if a user refreshes, that data from the service will be lost.

You can move around this leveraging the power of browser tools -- namely, local storage. So instead of setting service.user to the successful response data, you would do something like localStorage.setItem('user', response.data) and you can access that throughout your controllers as localStorage.getItem('user'), which would be an object or whatever you save it as. That would allow it to be in the browser until removed or expired, so even if the user refreshes his current session that user data will still exist for him. That also requires only one GET request.

Refer to these docs for some info on localStorage. Works for basically all modern browsers and down to IE 8.

You can also use $cacheFactory as one of the comments suggested.

Lansana Camara
  • 9,497
  • 11
  • 47
  • 87
  • No problem. Remember about the caveats though.. people refresh screens a lot and you don't want them losing their user data and having a broken app. local storage or the $cacheFactory would be a better approach. – Lansana Camara Jan 06 '16 at 20:43
0

You have to create a service ( who is a singleton ) and inject it every where you want to access to the login data. on the first $http.get() you write the data in the service. and in other controller you read the data

AlainIb
  • 4,544
  • 4
  • 38
  • 64