3

Template html

<div class="item" v-for="n, index in teamRoster">
   <span> {{ getFantasyScore(n.personId) }} </span>
</div>

Method

getFantasyScore(playerId) {
    if(playerId) {
        axios.get(config.NBLAPI + config.API.PLAYERFANTASYSCORE + playerId)
        .then( (response) => {
            if( response.status == 200 ) {
                console.log(response.data.total)
                return response.data.total;
            }
        });
    }
}

I'm trying to display the returned data to DOM but it doesnt display anything. But when I try to console log the data is displays. How can I be able to display it. What am I missing?

1
  • How are you populating the teamRoster object / array? Might be easier to just pre-fetch the fantasy-score data at the same time Commented Aug 2, 2017 at 5:39

2 Answers 2

3

Problem is, your getFantasyScore method doesn't return anything and even then, the data is asynchronous and not reactive.

I would create a component that loads the data on creation. Something like

Vue.component('fantasy-score', {
  template: '<span>{{score}}</span>',
  props: ['playerId'],
  data () {
    return { score: null }
  },
  created () {
    axios.get(config.NBLAPI + config.API.PLAYERFANTASYSCORE + this.playerId)
      .then(response => {
        this.score = response.data.total
      })
  }
})

and then in your template

<div class="item" v-for="n, index in teamRoster">
  <fantasy-score :player-id="n.personId"></fantasy-score>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't use methods for AJAX results because they are async. You could retrieve the full teamRoster object and then add this to your div:

<div class="item" v-for="fantasyScore in teamRoster" v-if="teamRoster">
   <span> {{ fantasyScore }} </span>
</div>

4 Comments

Where is getFantasyScoreByPerson called and how is the id passed to it?
@Phil, fixed that!
Not really. Where does this.id come from? How does it get the id from n.personId in the repeater?
I simplified it. Didn't read the requirements carefully. Just as an alternative solution to yours, which is the preferred one, @Phil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.