EmberJS:如何在同一路径上加载多个模型?

编程入门 行业动态 更新时间:2024-10-08 01:29:36
本文介绍了EmberJS:如何在同一路径上加载多个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 虽然我不是Web开发的新手,但我对于客户端的MVC框架来说也是一个新鲜事。我做了一些研究,决定和EmberJS一起去。我经历了TodoMVC指南,对我有意义。

我已经设置了一个非常基本的应用程序;索引路线,两个模型和一个模板。我有一个服务器端php脚本运行,返回一些db行。

有一件令我非常混乱的事情是如何在同一路径上加载多个模型。我已经阅读了有关使用setupController的一些信息,但我仍然不清楚。在我的模板中,我有两个表,我试图加载与不相关的数据库行。在一个更传统的Web应用程序中,我刚刚发布到sql语句并循环遍历它们来填充行。我很难把这个概念翻译成EmberJS。

如何在同一路径上加载多个不相关的数据模型?

我正在使用最新的Ember和Ember Data库。

更新

虽然第一个答案提供了一种处理它的方法,但第二个答案解释了何时适用,何时不适用的方法。

解决方案

您可以使用 Ember.RSVP.hash 加载几个模型:

app / routes / index.js

导出默认值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',modelpanies); } });

在您的模板中,您可以参考人和公司以获取加载的数据:

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: ember-twiddle/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', modelpanies); } });

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: ember-twiddle/c88ce3440ab6201b8d58

更多推荐

EmberJS:如何在同一路径上加载多个模型?

本文发布于:2023-11-30 18:28:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   路径   模型   加载   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!