1)あなたのルートファイル - ルートモデルフックの内側Ember.RSVP.hash
の使用>私は本を想定/ new.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
newBook : this.store.createRecord('book'),
authors : this.store.findAll('author')
});
},
actions: {
save() {
this.modelFor(this.routeName).newBook.save();
}
}
});
およびテンプレート内で、あなたはmodel.authors
を使用してauthors
にアクセスすることができます。そして、著者モデルデータとしてエブラヒムが提案のように、あなたが必要なコントローラで以下のコードを持つことができmodel.newBook.title
<form {{action "save" on="submit"}}>
{{input value=model.newBook.title placeholder="Title"}}<br>
{{#power-select class="select"
selected=model.newBook.author
options=model.authors
onchange=(action (mut model.newBook.author)) as |author|}}
{{author.name}}
{{/power-select}}
<input type="submit" value="Save">
</form>
2を使用することによりtitle
)、
authors: Ember.computed(function(){
return this.store.findAll('author');
})
3)は著者のためのデータモデルを共有しようとしています、本、本。新しいルート。サービスを継続して、必要なすべてのルートからアクセスできます。
著者-service.js - >サービス
import Ember from 'ember';
export default Ember.Service.extend({
store:Ember.inject.service(),
authors: undefined,
init(){
this._super(...arguments);
this.get('store').findAll('author', { reload: true }).then(function(results){
this.set('authors',results); //As this is going to be Ember.enumerables, you can iterate and get the data.
});
}
});
であなたはそれを authorsService:Ember.inject.service()
を注入することにより、任意の場所にauthors-service.js
からauthors
にアクセスすることができます。私はあなたのケースでは、あなたがbooks/new.hbs
テンプレート、
冊/ new.js用コントローラbooks/new.js
を作成する必要があると思います - >コントローラファイル
ブック/新しいインサイド
import Ember from 'ember';
export default Ember.Controller.extend({
authorsService:Ember.inject.service(),
authors: Ember.computed.alias('authorsService.authors');
});
。hbsテンプレートauthors
プロパティにアクセスすることができます。
私は見るので、そのルートのモデルデータを読み込むためのルートを使用してください。しかし、他のモデルからデータをロードする場合は、コントローラを使用しますか? – PCR
@PCR No. 'Ember.RSVP.hash'を使って' model'フックルートに 'authors'をロードすることもできます。しかし、「著者」はあなたのルートが扱うリソースではありません。私は経路があなたの事例の中の資源を管理する責任があることを意味します。著者は選択ボックスのコレクションに過ぎず、フォーム内では変更されません。これが役に立ったらいいですね。 –