Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49167

Update data in a factory

$
0
0

@Vieroli wrote:

Hi guys,

In my app, I use the facebook connect API.

I store the userID in localstorage, I need the userID to display data, data that concerns each user.

In my case, I need to update the userID in a factory in the case where the user might want to change of facebook account.

Here is my factory :

app.factory('FavoritesArtists',['$http', '$q', 'UserService', function($http, $q, UserService){
  var deferred = $q.defer();
  var userId = UserService.getUser().userID;
  console.log('FavArtistsFactory '+userId);

  $http.get('https://homechefhome.fr/rise/favorites-artists.php?user_id='+userId)
      .success(function(data) {
          deferred.resolve(data);
 })
 .error(function(err) {
         deferred.reject(err);
 });
  return deferred.promise;
}]);`

I need this variable to update :
var userId = UserService.getUser().userID;

UserService.getUser().userID is in localstorage

Here is the code :

app.factory('UserService', function() {
    console.log('UserService');

    var setUser = function(user_data) {
        localStorage.starter_facebook_user = JSON.stringify(user_data);
    };

    var getUser = function(){
        return JSON.parse(localStorage.starter_facebook_user || '{}');
    };

    var clearData = function(user_data){
        localStorage.starter_facebook_user = JSON.stringify(user_data);
    };

    return {
        getUser: getUser,
        setUser: setUser,
        clearData: clearData
    };
});

For now, my probleme is that when the user change of facebook account, it use the old userID..

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49167

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>