2016-08-08 6 views
0

RSVPハッシュを使用して私のルートの2つのモデルから2つのモデルのデータを取得し、それらの結果をコントローラで結合してパワーソートの選択。しかし、何かが働いていないようです。ember - 2つのモデルのデータを1つの結果にまとめて出力する

私のルートは、次のようになります。

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model() { 
     return Ember.RSVP.hash({ 
      newBook : this.store.createRecord('book'), 
      authors : this.store.findAll('author'), 
      publishing_houses : this.store.findAll('publishing-house')  
     }); 
    }, 

    setupController(controller, model) { 
     this._super(...arguments); 
     Ember.set(controller, 'authors', model.authors); 
     Ember.set(controller, 'publishing_houses', model.publishing_houses); 
    }, 

    actions: { 
     save() { 
      this.modelFor(this.routeName).get('newBook').save(); 
     } 
    }  
}); 

私のテンプレートは次のようになります。

<form {{action "save" on="submit"}}> 
    {{input value=model.newBook.title placeholder="Title"}}<br> 
    {{input value=model.newBook.price placeholder="Price"}}<br> 

    {{#power-select class="select" 
     selected=model.newBook.author 
     options=model.authors 
     onchange=(action (mut model.newBook.author)) as |author|}} 
     {{author.name}} 
    {{/power-select}} 

    {{#power-select class="select" 
     selected=model.newBook.publisher 
     options=model.publishers 
     onchange=(action (mut model.newBook.publisher)) as |publisher|}} 
     {{publisher.name}} 
    {{/power-select}} 

    <input type="submit" value="Save"> 
</form> 

と私は問題だと思う私のコントローラは、次のようになります。

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    publishers: function() { 
     var authors = this.get("authors"); 
     var publishingHouses = this.get("publishing_houses"); 
     return authors.concat(publishingHouses); 
    } 
}); 

私はまだコントローラの使い方を考えています。コントローラーでモデルデータに正しくアクセスしていますか?これは、テンプレートで使用するプロパティを作成する適切な方法ですか?

答えて

0

setupControllerフックでは、デフォルトでスーパーファンクションコールによって設定されるため、authorspublishing_housesを明示的に設定する必要はありません。お使いのコントローラで

は、あなたがget機能せずに直接アクセスする必要があり、newBook

ルートからRSVPリターンモデルにアクセスするには、this.get("model.authors")のように、その他の特性publishing_housesのために同じ方法でそれにアクセスしてみてくださいすることができます。あなたはhttp://emberjs.com/api/classes/Ember.Enumerable.html

reduceを参照してください連結し、他のもののために

`this.modelFor(this.routeName).newBook.save()

は、あなたのニーズを、スイートかもしれません。

関連する問題