I am working with localForage and I have one good thing and another very bad that I've been trying to resolve the last 2 hours, I thought it was easier but that's why I am here
I have an array named sports with the option to checked or unchecked them, once you unchecked or checked = false
-whatever you want to call it- those items are saved in a DB, but they supposed to be saved in a localForage while the user is within the session first.
The good part:
if you checked = false
any item on the array and log out, and them log in, the item you set unchecked is still there, that part is working great.
The bad part:
if you checked = false
any item on the array and you refresh the page, the item checked = false
return back to checked = true
.
So it means that after refreshing the page, the data is not persisting, only if you log out and log in again.
I am using something named forceTrip
which you will see in the code below.
Everything is related to the service, look
angular.module('myApp')
.factory('SportsFactory', function($http, $q, AuthFactory,
LocalForageFactory, LeaguesFactory, CONSTANT_VARS) {
return {
getSports: function(customer, forceTrip) {
forceTrip = forceTrip || false;
var defer = $q.defer(),
_this = this;
if (forceTrip) {
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS)
}
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS)
$http.get(CONSTANT_VARS.BACKEND_URL + '/lines/sports/' + customer.agent)
.success(function(sports) {
sports = _.sortBy(sports, function(sport) {
return sport.priority;
});
//HERE I CALL GETSPORTCHECKED();
_this.getSportChecked(customer).then(function(sportChecked) {
var sportIds = _.pluck(sports, 'id'),
intersectedSports = _.intersection(sportIds, sportChecked.sport);
if (sports.length) {
sports = _.map(sports, function(sport) {
sport.checked = !_.includes(intersectedSports, sport.id);
return sport;
});
}
});
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS, sports);
defer.resolve(sports);
})
.error(function(err) {
defer.reject(err);
});
}
});
return defer.promise;
},
setSportChecked: function(params) {
var defer = $q.defer();
$http.post(CONSTANT_VARS.BACKEND_URL + '/sports/checked', params)
.success(function(sportChecked) {
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED, params);
defer.resolve(sportChecked);
})
.error(function(err) {
console.log(err);
defer.reject(err);
});
return defer.promise;
},
getSportChecked: function(customer, forceTrip) {
forceTrip = forceTrip || false;
var defer = $q.defer(),
user,
rejection = function(err) {
defer.reject(err);
};
if (forceTrip) {
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED);
}
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED)
.then(function(sportChecked) {
user = customer.customer;
$http.get(CONSTANT_VARS.BACKEND_URL + '/sports/getChecked/' + user)
.success(function(sportChecked) {
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED, sportChecked);
defer.resolve(sportChecked);
})
.error(rejection);
}
}, rejection);
return defer.promise;
}
};
});
the function setSportChecked
performs a post request once you checked = false
any of the items, and getSportChecked
makes the get request to get checked = false
items, and in getSports
you can see that I am calling _this.getSportChecked(customer)
, so what do you think I can do here ?