2017-12-12 10 views
0

私はAxiを通じてAPI要求によって生成された動的リストを持っています。このリストでは、要素が生成された後に生成される要素のクラスがあります。Vue.js:v-bind:Axiosリクエスト後のクラス

だからここに私がこれまで持っているものです。

<template> 
    <div> 
     <div v-if="article_count > 0" class="table-responsive"> 

      <table class="table table-striped"> 
       <thead> 
       <tr> 
        <th>state</th> 
        <th>created at</th> 
        <th>headline</th> 
       </tr> 
       </thead> 
       <tbody> 
       <tr v-for="article in articles"> 
        <td class="status"> 
         <!-- the class of this element should change --> 
         <i class="fa fa-cog fa-spin fa-fw" v-bind:class="getArticleStatus(article.id)"></i> 
        </td> 
        <td>{{ article.created_at }}</td> 
        <td><strong>{{ article.headline }}</strong></td> 
       </tr> 
       </tbody> 
      </table> 

     </div> 

     <div v-else class="text-center">no articles found</div> 
    </div> 
</template> 

<script> 
    export default { 
     data() { 
      return { 
       article_count: 0, 
       articles: [] 
      } 
     }, 
     methods: { 
      // get the articles 
      getArticles() { 
       axios.get('/api/get_articles'     ) 
       .then((response) => { 
        this.article_count = response.data.article_count; 
        this.articles = response.data.articles; 
       }) 
      }, 
      // get the article's state 
      getArticleStatus(article_id) { 
       axios.get('/api/article_status/' + article_id) 
       .then((response) => { 
        switch(response.data) { 
         case 'ONL': 
          console.log('online'); // this works 
          return 'status online'; // this does not work... 

         case 'WAIT': 
          return 'status waiting'; 

         case 'OFFL': 
          return 'status offline'; 

         default: 
          return 'status unknown'; 
        } 
       }).catch((error) => { 
        console.error(error); 
       }) 
      } 
     }, 
     mounted() { 
      this.getArticles() 
     } 
    } 
</script> 

私は、コンソール(ネットワーク]タブ)で見ることができるように、APIコールは "/ API/article_status/{のarticle_id}" 作品なので、AJAX呼び出しに働くしかし、return文はv-bindには届きません:class ...

+0

何がエコー場合、 '{{getArticleStatus(article.id)}}'あなたのV-ためのループ内側:

methods: { getArticles() { axios.get( '/api/get_articles' ).then( response => { this.article_count = response.data.article_count this.articles = response.data.articles } ) }, getArticleState (id) { axios.get( '/api/article_status' + id, ).then( response => { this.articles.forEach(function (article) { if(article.id === id){ switch(response.data) { case 'ONL': article.class = 'status waiting' } } }) } ) } } 

は今、あなたが必要とする唯一のものは、このようなクラスをバインドしていますか?また、これはステータスを取得するための非常に悪い方法です。基本的には、v-forループでサーバーに要求しています。 – samayo

+0

'axios'リクエストは' then'コールバックの戻り値ではなくPromiseを返します。 'async'関数(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)を使うか、記事オブジェクトに返された値を保存することを検討するべきです –

+0

@samayo問題ここでは、記事のステータスのリクエストに約2秒かかる(サードパーティー、私はこれを改善できません)、私は50以上の記事を持っているとき、ユーザーは疲れているかもしれません。だから私はリストをレンダリングした後にステータスをチェックするのです。 – DaFunkyAlex

答えて

1

コメントに記載されているように、関数から要素へPromiseをバインドすることはできません。さらに、もっと良い選択肢があります。このようなものを使うことができます。

<tr v-for="article in articles"> 
    <i class='fa fa-cog' :class='article.class'> 
</tr> 
+0

実際には、foreachは必要ありません、あなたは 'findIndex'を使って正確な記事を見つけ出し、そのクラスを注入することができます – samayo

+0

あなたは私に正しいヒントを教えてくれました。 – DaFunkyAlex