2017-08-04 5 views
0

以下の例でテンプレートをレンダリングする前にデータを変更する適切な方法は何ですか?私のルートからレンダリング前にデータを変更する

model()

私のテンプレートで次に
model({contractId}){ 
    const cities = this.store.query('city', {contractId}); 
    return Ember.RSVP.hash({ cities}); 
    } 

{{orders/order-form cities=model.cities}} 

そして最後に、私のコンポーネントで、私は都市で何かを変更したいのですが、私はで計算されたオブジェクトを作成しましたこの方法:

cityOptions: Ember.computed('cities', function(){ 
    return this.get('cities').map((data)=> ({key: data.id, value: data.name})); 
    }), 

しかし、this.get('cities')は次のようなものを返します。データの代わりにClass {modelName: "city", query: Object, store: Class, manager: RecordArrayManager…}誰もがthis.get('cities')から純粋なデータを取得するクールな方法を知っていますか?どんな助けもありがとう。

+0

プロパティを取得するには、常に 'get'メソッドを使用します。 'DS.RecordArray'を通常の配列に変換したい場合は、' toArray'を使うことができます – kumkanillam

答えて

1

これは動作するはずです:

import Ember from 'ember'; 

const {get} = Ember; 

export default Ember.Component.extend({ 

    cityOptions: Ember.computed('[email protected]', '[email protected]', function(){ 
    return this.get('cities').map((city) => ({ 
     key: get(city, 'id'), 
     value: get(city, 'name'), 
    })); 
    }), 
}); 

をここで重要なことはcitiesが燃えさしのデータオブジェクトの燃えさしの配列であること、です。通常のプレーンなJSオブジェクトではないので、obj.propの代わりにEmber.get(obj, 'prop')またはobj.get('prop')を使用する必要があります。

-1

あなたはいつものようにtoJSON()を使用することができます... this.get('cities').toJSON() 都市はあなたがPOJOをしたい場合、あなたはそれを変換する必要があり、DS.modelの配列です。

cityOptions: Ember.computed('cities', function(){ return 
    this.get('cities').map((aModel)=> ( 
    return aModel.toJSON().map((data)=> (
     return {key: data.id, value: data.name})) 
    )); 
}), 
+0

Ember.Component内ではできません –

+0

OKルートとテンプレートを記述して以来、あなたがコントローラにいたと思います。しかし、私はそれは問題ではないと思う。 あなたのモデルをあなたのコンポーネントに渡し、変換を行うことができます。 ' cityOptions:Ember.computed( 'cities'、function(){ return this.get( 'cities')。map((aModel)=>( )aModel.toJSON()。map((data = >))、 ' –

+0

isch書式設定のためごめんね –

関連する問題