Extract data loading from fetch methods
This commit is contained in:
+19
-11
@@ -225,12 +225,9 @@ export default Service.extend({
|
||||
console.debug(`[kredits] Fetching all contributors from the network`);
|
||||
return this.kredits.Contributor.all()
|
||||
.then(contributors => {
|
||||
return contributors.map(data => {
|
||||
const contributor = Contributor.create(processContributorData(data));
|
||||
const loadedContributor = this.contributors.findBy('id', contributor.id);
|
||||
if (loadedContributor) { this.contributors.removeObject(loadedContributor); }
|
||||
this.contributors.pushObject(contributor);
|
||||
return contributor;
|
||||
return contributors.forEach(data => {
|
||||
this.loadContributorFromData(data);
|
||||
return;
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
@@ -238,6 +235,13 @@ export default Service.extend({
|
||||
});
|
||||
},
|
||||
|
||||
loadContributorFromData(data) {
|
||||
const contributor = Contributor.create(processContributorData(data));
|
||||
const loadedContributor = this.contributors.findBy('id', contributor.id);
|
||||
if (loadedContributor) { this.contributors.removeObject(loadedContributor); }
|
||||
this.contributors.pushObject(contributor);
|
||||
},
|
||||
|
||||
async cacheLoadedContributors () {
|
||||
for (const c of this.contributors) {
|
||||
await this.browserCache.contributors.setItem(c.id, c.serialize());
|
||||
@@ -274,11 +278,7 @@ export default Service.extend({
|
||||
return this.kredits.Contribution.all(options)
|
||||
.then(contributions => {
|
||||
return contributions.map(data => {
|
||||
const contribution = Contribution.create(processContributionData(data));
|
||||
contribution.set('contributor', this.contributors.findBy('id', data.contributorId.toString()));
|
||||
const loadedContribution = this.contributions.findBy('id', contribution.id);
|
||||
if (loadedContribution) { this.contributions.removeObject(loadedContribution); }
|
||||
this.contributions.pushObject(contribution);
|
||||
loadContributionFromData(data);
|
||||
return contribution;
|
||||
});
|
||||
})
|
||||
@@ -292,6 +292,14 @@ export default Service.extend({
|
||||
});
|
||||
},
|
||||
|
||||
loadContributionFromData(data) {
|
||||
const contribution = Contribution.create(processContributionData(data));
|
||||
contribution.set('contributor', this.contributors.findBy('id', data.contributorId.toString()));
|
||||
const loadedContribution = this.contributions.findBy('id', contribution.id);
|
||||
if (loadedContribution) { this.contributions.removeObject(loadedContribution); }
|
||||
this.contributions.pushObject(contribution);
|
||||
},
|
||||
|
||||
async cacheLoadedContributions () {
|
||||
for (const c of this.contributions) {
|
||||
await this.browserCache.contributions.setItem(c.id, c.serialize());
|
||||
|
||||
Reference in New Issue
Block a user