Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {Inject} from 'di/index';
import {JFHttp} from 'lib/network/JFHttp';
import {JFChallenge} from 'lib/api/challenges/JFChallenge';
@Inject(JFHttp)
export class JFChallenges {
constructor(http){
this.http = http;
this.allChallenges = [];
}
loadChallenges(options){
return this.http.get('/v2/challenges',{params: options || {}})
.then(parseChallengeResponse.bind(this));
}
getChallengeById(id){
let existingChallenge = this.allChallenges.find(function(challenge){
return challenge.id === id;
});
if(existingChallenge) return Promise.resolve(existingChallenge);
return Promise.reject(false);
}
}
function parseChallengeResponse(response){
let self = this;
var challenges = response.challenges.map(function(challenge){
return self.getChallengeById(challenge.id)
.then(null,function(){
var newChallenge = new JFChallenge(self,challenge);
self.allChallenges.push(newChallenge);
return newChallenge;
})
.then(function(challengeRecord){
challengeRecord.updateValues(challengeRecord);
return challengeRecord;
});
});
return Promise.all(challenges);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.