diff --git a/app/routes/index.js b/app/routes/index.js index c4f6ea7..4194f31 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -1,5 +1,4 @@ import Ember from 'ember'; -import Contributor from 'kredits-web/models/contributor'; let fixtures = [ { github_username: "bumi", github_uid: "318", kredits: 18000 }, @@ -18,14 +17,10 @@ export default Ember.Route.extend({ kredits: Ember.inject.service(), model() { - let contributors = []; - this.get('kredits.contributors').forEach(obj => { - contributors.pushObject(Contributor.create(obj)); - }); - return { - contributors: contributors - }; + return Ember.RSVP.hash({ + contributors: this.get('kredits').getContributors() + }); } }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 7b9fb41..53fa2b4 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -1,6 +1,7 @@ import Ember from 'ember'; import Web3 from 'npm:web3'; import config from 'kredits-web/config/environment'; +import Contributor from 'kredits-web/models/contributor'; export default Ember.Service.extend({ @@ -44,21 +45,46 @@ export default Ember.Service.extend({ // return this.get('kreditsContract').totalSupply(); }.property('kreditsContract'), - contributorsCount: function() { - return this.get('kreditsContract').contributorsCount(); - }.property('kreditsContract'), + getValueFromContract(contractMethod, ...args) { + return new Ember.RSVP.Promise((resolve, reject) => { + this.get('kreditsContract')[contractMethod](...args, (err, data) => { + if (err) { reject(err); } + resolve(data); + }); + }); + }, - contributors: function() { - var c = []; - for(var i = 0; i < this.get('contributorsCount').toNumber(); i++) { - var address = this.get('kreditsContract').contributorAddresses(i); - var person = this.get('kreditsContract').contributors(address); - var balance = this.get('kreditsContract').balanceOf(address); - console.log(person); - c.push({address: address, github_username: person[1], github_uid: person[0], ipfsHash: person[3], kredits: balance.toNumber()}); - }; - return c; - }.property('kreditsContract', 'contributorCount'), + getContributors() { + return this.getValueFromContract('contributorsCount').then(contributorsCount => { + let contributors = []; + + for(var i = 0; i < contributorsCount.toNumber(); i++) { + contributors.push(new Ember.RSVP.Promise((resolve/*, reject*/) => { + let c = {}; + this.getValueFromContract('contributorAddresses', i).then(address => { + c.address = address; + this.getValueFromContract('contributors', address).then(person => { + 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) { return this.get('kreditsContract').balanceOf(address).toNumber();