我被深深地抛弃而不确定我在看什么,但是我真的想了解为什么下面代码中的本地缓存没有被更新。
这是我的控制者:
scope.assessment = assessment;
assessment.possibleScore = 0;
assessment.score = 0;
assessment.test.items.forEach(function (item) {
assessment.possibleScore += item.possibleScore;
assessment.score += item.mark;
});
scope.percentageScore = Math.floor(assessment.score / assessment.possibleScore * 100);
AssessmentService.updateUserAssessment(assessment);
这是在AssessmentService中调用的updateUserAssessment函数:
/**
* Given a userAssessment object, update the cache and server.
* @param userAssessment - The object we want updating
* @returns a promise which resolves to the updated user assessment, if successful.
*/
self.updateUserAssessment = function (userAssessment) {
delete userAssessment.$resolved;
delete userAssessment.$promise;
if (self.assessmentCache[userAssessment._id]) {
_.assign(self.assessmentCache[userAssessment._id], userAssessment);
} else {
self.assessmentCache[userAssessment._id] = userAssessment;
self.assessmentCache[userAssessment._id].dirty = true;
}
var items = self.assessmentCache[userAssessment._id].test.items;
return UserAssessment
.nestedUpdateById({
userId: userAssessment.user,
assessmentId: userAssessment._id
}, self.assessmentCache[userAssessment._id])
.$promise
.then(function (assessment) {
_.assign(self.assessmentCache[userAssessment._id], assessment);
self.assessmentCache[userAssessment._id].dirty = false;
self.assessmentCache[userAssessment._id].test.items = items;
self.saveCache();
return self.assessmentCache[userAssessment._id];
});
};
上面的函数应该更新服务器以及localcache,但似乎没有这样做。
作为一个FYI,这就是self.assessmentCache持有者:
self.loadAssessmentCache = function () {
var cachedAssessments = window.localStorage.getItem('assessments');
self.assessmentCache = cachedAssessments ? JSON.parse(cachedAssessments) : {};
};
**更新***
self.saveCache = function () {
window.localStorage.setItem('assessments', JSON.stringify(self.assessmentCache));
};