From c0b5c36115a1c8dfe97585c7d5fb413dff9eac4a Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Mon, 16 Apr 2018 19:23:28 +0200 Subject: [PATCH] Don't inject kredits service in models --- app/components/add-proposal/component.js | 3 -- app/models/proposal.js | 10 ------ app/services/kredits.js | 45 ++++++++++++------------ tests/unit/controllers/index-test.js | 2 +- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/app/components/add-proposal/component.js b/app/components/add-proposal/component.js index 8edeac5..567107a 100644 --- a/app/components/add-proposal/component.js +++ b/app/components/add-proposal/component.js @@ -1,11 +1,8 @@ import Component from '@ember/component'; import { computed } from '@ember/object'; import { and, notEmpty } from '@ember/object/computed'; -import { inject as injectService } from '@ember/service'; export default Component.extend({ - kredits: injectService(), - // Default attributes used by reset attributes: { contributorId: null, diff --git a/app/models/proposal.js b/app/models/proposal.js index 5a48ebe..75d064f 100644 --- a/app/models/proposal.js +++ b/app/models/proposal.js @@ -1,12 +1,8 @@ import EmberObject from '@ember/object'; -import { computed } from '@ember/object'; import { alias } from '@ember/object/computed'; -import { inject as injectService } from '@ember/service'; import bignumber from 'kredits-web/utils/cps/bignumber'; export default EmberObject.extend({ - kredits: injectService(), - // Contract id: bignumber('idRaw', 'toString'), creatorAccount: null, @@ -26,10 +22,4 @@ export default EmberObject.extend({ details: {}, url: null, ipfsData: '', - - // Relationships - // TODO: Optimize it. We don't need to find the contributor every time we add a new one - contributor: computed('contributorId', 'kredits.contributors.[]', function() { - return this.get('kredits.contributors').findBy('id', this.contributorId); - }) }); diff --git a/app/services/kredits.js b/app/services/kredits.js index 90bdd65..464dce1 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -1,15 +1,17 @@ -import { getOwner } from '@ember/application'; -import ethers from 'npm:ethers'; -import Kredits from 'kredits-web/lib/kredits'; -import RSVP from 'rsvp'; import Service from '@ember/service'; import { computed } from '@ember/object'; import { alias } from '@ember/object/computed'; import { isEmpty, isPresent } from '@ember/utils'; +import RSVP from 'rsvp'; +import Kredits from 'kredits-web/lib/kredits'; +import Contributor from 'kredits-web/models/contributor' +import Proposal from 'kredits-web/models/proposal' +import ethers from 'npm:ethers'; import config from 'kredits-web/config/environment'; export default Service.extend({ + ethProvider: null, currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. currentUser: null, @@ -76,23 +78,20 @@ export default Service.extend({ proposals: [], loadContributorsAndProposals() { - return RSVP.hash({ - contributors: this.getContributors(), - proposals: this.getProposals(), - }).then(({ contributors, proposals }) => { - this.contributors.pushObjects(contributors); - this.proposals.pushObjects(proposals); - }); + return this.getContributors() + .then(contributors => this.contributors.pushObjects(contributors)) + .then(() => this.getProposals()) + .then(proposals => this.proposals.pushObjects(proposals)) }, // TODO: Only assign valid attributes - buildModel(name, attributes) { - console.debug('[kredits] build', name, attributes); - let model = getOwner(this).lookup(`model:${name}`); - - model.setProperties(attributes); - return model; - }, + // buildModel(name, attributes) { + // console.debug('[kredits] build', name, attributes); + // let model = getOwner(this).lookup(`model:${name}`); + // + // model.setProperties(attributes); + // return model; + // }, addContributor(attributes) { console.debug('[kredits] add contributor', attributes); @@ -100,7 +99,7 @@ export default Service.extend({ return this.kredits.Contributor.add(attributes) .then((data) => { console.debug('[kredits] add contributor response', data); - return this.buildModel('contributor', attributes); + return Contributor.create(attributes); }); }, @@ -108,7 +107,7 @@ export default Service.extend({ return this.kredits.Contributor.all() .then((contributors) => { return contributors.map((contributor) => { - return this.buildModel('contributor', contributor); + return Contributor.create(contributor); }); }); }, @@ -119,7 +118,8 @@ export default Service.extend({ return this.kredits.Operator.addProposal(attributes) .then((data) => { console.debug('[kredits] add proposal response', data); - return this.buildModel('proposal', attributes); + attributes.contributor = this.contributors.findBy('id', attributes.contributorId); + return Proposal.create(attributes); }); }, @@ -127,7 +127,8 @@ export default Service.extend({ return this.kredits.Operator.all() .then((proposals) => { return proposals.map((proposal) => { - return this.buildModel('proposal', proposal); + proposal.contributor = this.contributors.findBy('id', proposal.contributorId.toString()); + return Proposal.create(proposal); }); }); }, diff --git a/tests/unit/controllers/index-test.js b/tests/unit/controllers/index-test.js index 228ee21..cd5f8e4 100644 --- a/tests/unit/controllers/index-test.js +++ b/tests/unit/controllers/index-test.js @@ -1,7 +1,7 @@ import { isEmpty, isPresent } from '@ember/utils'; -import Contributor from 'kredits-web/models/contributor'; import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; +import Contributor from 'kredits-web/models/contributor'; module('Unit | Controller | index', function(hooks) { setupTest(hooks);