EmberJS:如何在同一路径上加载多个模型? [英] EmberJS: How to load multiple models on the same route?
问题描述
我已经设置了一个非常基本的应用程序;索引路线,两个模型和一个模板。我有一个服务器端php脚本运行,返回一些db行。
有一件令我非常混乱的事情是如何在同一路径上加载多个模型。我已经阅读了有关使用setupController的一些信息,但我仍然不清楚。在我的模板中,我有两个表,我试图加载与不相关的数据库行。在一个更传统的Web应用程序中,我刚刚发布到sql语句并循环遍历它们来填充行。我很难把这个概念翻译成EmberJS。
如何在同一路径上加载多个不相关的数据模型?
我正在使用最新的Ember和Ember Data库。
更新
虽然第一个答案提供了一种处理它的方法,但第二个答案解释了何时适用,何时不适用的方法。
您可以使用 Ember.RSVP.hash 加载几个模型:
app / routes / index.js
<从ember导入Ember pre>
导出默认值Ember.Route.extend({
model(){
return Ember.RSVP.hash({
people:this.store.findAll('个人'),
公司:this.store.findAll('company')
});
},
setupController(控制器,模型){
this._super(... arguments);
Ember.set(controller,'people',model.people);
Ember.set(controller,'companies',model.companies);
}
});
在您的模板中,您可以参考人
和公司
以获取加载的数据:
app / templates / index.js
< h2>人物:< / h2>
< ul>
{{#each people as | person |}}
< li> {{person.name}}< / li>
{{/ each}}
< / ul>
< h2>公司:< / h2>
< ul>
{{#each companies as | company |}}
< li> {{company.name}}< / li>
{{/ each}}
< / ul>
这是一个带有此示例的Twiddle: https://ember-twiddle.com/c88ce3440ab6201b8d58
While I am not new to web development, I am quite new to to client-side MVC frameworks. I did some research and decided to give it a go with EmberJS. I went through the TodoMVC guide and it made sense to me...
I have setup a very basic app; index route, two models and one template. I have a server-side php script running that returns some db rows.
One thing that is very confusing me is how to load multiple models on the same route. I have read some information about using a setupController but I am still unclear. In my template I have two tables that I am trying to load with unrelated db rows. In a more traditional web app I would have just issued to sql statements and looped over them to fill the rows. I am having difficulty translating this concept to EmberJS.
How do I load multiple models of unrelated data on the same route?
I am using the latest Ember and Ember Data libs.
Update
although the first answer gives a method for handling it, the second answer explains when it's appropriate and the different methods for when it isn't appropriate.
You can use the Ember.RSVP.hash to load several models:
app/routes/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
people: this.store.findAll('person'),
companies: this.store.findAll('company')
});
},
setupController(controller, model) {
this._super(...arguments);
Ember.set(controller, 'people', model.people);
Ember.set(controller, 'companies', model.companies);
}
});
And in your template you can refer to people
and companies
to get the loaded data:
app/templates/index.js
<h2>People:</h2>
<ul>
{{#each people as |person|}}
<li>{{person.name}}</li>
{{/each}}
</ul>
<h2>Companies:</h2>
<ul>
{{#each companies as |company|}}
<li>{{company.name}}</li>
{{/each}}
</ul>
This is a Twiddle with this sample: https://ember-twiddle.com/c88ce3440ab6201b8d58
这篇关于EmberJS:如何在同一路径上加载多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!