Promise me this works

This commit is contained in:
2017-02-04 22:04:20 +08:00
parent 9120d67ad1
commit f1ea335f9d
2 changed files with 43 additions and 22 deletions
+3 -8
View File
@@ -1,5 +1,4 @@
import Ember from 'ember'; import Ember from 'ember';
import Contributor from 'kredits-web/models/contributor';
let fixtures = [ let fixtures = [
{ github_username: "bumi", github_uid: "318", kredits: 18000 }, { github_username: "bumi", github_uid: "318", kredits: 18000 },
@@ -18,14 +17,10 @@ export default Ember.Route.extend({
kredits: Ember.inject.service(), kredits: Ember.inject.service(),
model() { model() {
let contributors = [];
this.get('kredits.contributors').forEach(obj => {
contributors.pushObject(Contributor.create(obj));
});
return { return Ember.RSVP.hash({
contributors: contributors contributors: this.get('kredits').getContributors()
}; });
} }
}); });
+40 -14
View File
@@ -1,6 +1,7 @@
import Ember from 'ember'; import Ember from 'ember';
import Web3 from 'npm:web3'; import Web3 from 'npm:web3';
import config from 'kredits-web/config/environment'; import config from 'kredits-web/config/environment';
import Contributor from 'kredits-web/models/contributor';
export default Ember.Service.extend({ export default Ember.Service.extend({
@@ -44,21 +45,46 @@ export default Ember.Service.extend({
// return this.get('kreditsContract').totalSupply(); // return this.get('kreditsContract').totalSupply();
}.property('kreditsContract'), }.property('kreditsContract'),
contributorsCount: function() { getValueFromContract(contractMethod, ...args) {
return this.get('kreditsContract').contributorsCount(); return new Ember.RSVP.Promise((resolve, reject) => {
}.property('kreditsContract'), this.get('kreditsContract')[contractMethod](...args, (err, data) => {
if (err) { reject(err); }
resolve(data);
});
});
},
contributors: function() { getContributors() {
var c = []; return this.getValueFromContract('contributorsCount').then(contributorsCount => {
for(var i = 0; i < this.get('contributorsCount').toNumber(); i++) { let contributors = [];
var address = this.get('kreditsContract').contributorAddresses(i);
var person = this.get('kreditsContract').contributors(address); for(var i = 0; i < contributorsCount.toNumber(); i++) {
var balance = this.get('kreditsContract').balanceOf(address); contributors.push(new Ember.RSVP.Promise((resolve/*, reject*/) => {
console.log(person); let c = {};
c.push({address: address, github_username: person[1], github_uid: person[0], ipfsHash: person[3], kredits: balance.toNumber()}); this.getValueFromContract('contributorAddresses', i).then(address => {
}; c.address = address;
return c; this.getValueFromContract('contributors', address).then(person => {
}.property('kreditsContract', 'contributorCount'), c.person = person;
this.getValueFromContract('balanceOf', c.address).then(balance => {
c.balance = balance;
let contributor = Contributor.create({
address: c.address,
github_username: c.person[1],
github_uid: c.person[0],
ipfsHash: c.person[3],
kredits: c.balance.toNumber()
});
console.log(contributor);
resolve(contributor);
});
});
});
}));
}
return Ember.RSVP.all(contributors);
});
},
balanceOf: function(address) { balanceOf: function(address) {
return this.get('kreditsContract').balanceOf(address).toNumber(); return this.get('kreditsContract').balanceOf(address).toNumber();