2016-11-02 8 views
0

サービスを使用してデータを要求した後、サービスがコンポーネントに挿入されます。データはEmber Mirageから取得されます。テンプレートでAPI(Store)からデータを表示 - 検索されたレコードをレンダリングできません

私の問題は、このプロパティがcomputedPropertyで使用されていても、computedPropertyが正しく計算して表示するにもかかわらず、データをプロパティとして表示できないことです。

エンブレムにおけるコンポーネントテンプレート:

.notifications-number {{notificationService.countUnread.content}} 
each notificationService.notifications.content as |notification| 
    span {{notification.text}} 

通知サービス:

import Ember from 'ember'; 

export default Ember.Service.extend({ 
    store: Ember.inject.service('store'), 

    // render bugs out 
    notifications: function() { 
    return this.get('store').findAll('notification') 
    }.property(), 

    // renders correctly 
    countUnread: function() { 
    return DS.PromiseObject.create({ 
     promise: this.get('notifications').then((notifications) => { 
     return notifications.get('length') 
     }) 
    }); 
    }.property(), 
}); 

ミラージュ設定:

this.get('/notifications',() => { 
    return { 
     data: [ 
     {id: 1, type: 'notification', text: 'hi im notification', link: ''}, 
     {id: 2, type: 'notification', text: 'hi im notification 2', link: 'google.com'}    
     ] 
    }; 
    }); 

Iは、取り出されたデータではない別のサービス及びコンポーネントと同様の問題を抱えています配列ではなくオブジェクトです。

  • {{myService.myProperty}}<(subclass of Ember.ObjectProxy):ember404>
  • {{myService.myProperty.content}}をレンダリング<[email protected]:myModel::ember580:1>
  • {{myService.myProperty.content.myModelField}}は何もレンダリングしないレンダリングします。

実際に非同期リクエストがAPIモックに送信されたときに、私は手動で値をapp initにストアするときにすべてうまく動作しましたが、実際の非同期リクエストがAPIモックに送信されたときは機能しません。

+0

'countUnread'は正しく動作しますか?あなたは 'PromiseObject'を使っていますが、実際には値(' length'は数字です)を返しています。値をプロキシすることはできません。あなたは '{value:notifications.get( 'length')}'のようなものを返さなければなりません。テンプレート内で '{{countUnread.value}}'を実行します。 – locks

+0

これはMirageペイロードの正しい形式ですか? – locks

+0

私はなぜそれが本当に適切に機能しているかはわかりませんが。たぶん正しい数字が偶然に計算されたのでしょうか?とにかく、あなたは正しいです、主な問題は間違ったJSON形式でした。私は今日後で答えを加えます。 – Senthe

答えて

0

ここでの問題は、私のアダプタ用のJSON形式が間違っていたことです(一部の変数は下線の代わりにハイフネーションされていると思います)。

これは、APIからサービスにデータを読み込む正しい方法ではありません。今私はルート/ application.jsモデル()フックにデータをロードし、setupController()のサービスにプッシュすることをお勧めします。これは、今どのように見えるかです:

// routes/application.js 

    model() { 
     return Ember.RSVP.hash({ 
     user: this.get('store').queryRecord('user', {}), 
     notifications: this.get('store').findAll('notification', { reload: true }) 
     }) 
    }, 

    setupController(controller, model) { 
    this._super(...arguments) 

    if (model) 
     this.set('notificationService.notifications', model.notifications.sortBy('createdAt').reverse()) 
    }, 

そしてサービスでは、計算されたプロパティに頼って、どんな約束なしで使用することができます。いずれにも使用する必要が.content

// services/notificationService.js 

    countUnread: function() { 
    let notifications = this.get('notifications') 
    if (notifications) 
     return notifications.filterBy('read', false).get('length') 
    }.property('notifications') 

そしてもちろんありませんコントローラまたはテンプレート。

+0

誰かがより良い答えを持っているなら、私はそれについて読むことをうれしく思うでしょう。 – Senthe

関連する問題