简体   繁体   English

如何使用任何后端的Ember Data处理嵌套数据?

[英]How do I handle nested data with Ember Data with any backend?

I'm going a bit crazy trying to figure out the best way to implement nested resources in an application I'm writing with Ember/Ember Data as the front end. 试图找出在以Ember / Ember Data作为前端编写的应用程序中实现嵌套资源的最佳方法,我有点发疯。

To start with, here are my Models: 首先,这是我的模型:

App.Gathering = DS.Model.extend({
    name: DS.attr('string'),
    events: DS.hasMany('event', { asynch: true })
});

App.Event = DS.Model.extend({
    name: DS.attr('string'),
    startDate: DS.attr('string'),
    endDate: DS.attr('string'),
    gathering: DS.belongsTo('gathering', { asynch: true })
});

Other than very basic routes, that's about all I have so far - my issues come in when I start working with the backend. 除了非常基本的路线外,到目前为止,这还只是我的全部-当我开始使用后端时,就会遇到我的问题。 I'm writing both front and back end, so at this stage, I can really implement the back end however would be best. 我正在编写前端和后端,因此在此阶段,我可以实现后端,但最好。 I feel like I've scoured the Internet trying to find a solid, up-to-date way to get Event objects associated with a Gathering using Ember Data. 我觉得我已经搜寻了Internet,试图找到一种可靠的,最新的方式来获取与使用Ember Data进行收集相关联的Event对象。 I've had no issues getting all data for either a Gathering or Event, but have had a heck of a time figuring out how - given a particular Gathering (id) - to query only for Events related to that Gathering without having to load every event ever from the server. 我没有为聚会或事件获取所有数据的问题,但是花了一点时间来弄清楚如何-给定特定的聚会(id)-仅查询与该聚会有关的事件,而不必加载每个事件来自服务器的事件。 In particular, later on down the road, there will be objects nested inside of Events, so I'm aiming for a viable long(ish)-term solution to this problem. 特别是,在稍后的过程中,将在“事件”内部嵌套一些对象,因此我的目标是为该问题寻求可行的长期解决方案。

I've tried using the "links" attribute on JSON returned from the server, looked up 10 different sites regarding query parameters, and pretty much have spent the past three days trying to figure this out. 我尝试使用服务器返回的JSON上的“链接”属性,查找了10个有关查询参数的不同站点,并且过去三天中,几乎已经花了很多时间来解决这个问题。 I could use custom AJAX queries, but since this project is young, I'd like to implement it in a way that's as Ember friendly as possible since I'm not really subject to many constraints. 我可以使用自定义AJAX查询,但是由于这个项目还很年轻,所以我希望以对Ember友好的方式实现它,因为我并没有受到太多限制。

I'm pretty new to Ember/Ember Data (and front-end development in general), but I've been coding in Java for several years and have a pretty solid foundation in OOP/MVC/IOC, etc. 我刚接触Ember / Ember Data(通常是前端开发),但是我从事Java编程已经有几年了,并且在OOP / MVC / IOC等方面拥有扎实的基础。

Thanks in advance! 提前致谢!

Update 更新

I think what I was mostly lacking was knowledge on how to actually access those nested resources; 我认为我最缺乏的是如何实际访问这些嵌套资源的知识。 the example in the posted answered cleared that up for me just splendidly. 发布的答案中的示例清楚地说明了这一点。 Finding a complete, working example of RESTAdapter/Ember-Data has been a slight nightmare, so that's greatly appreciated! 寻找一个完整的,有效的RESTAdapter / Ember-Data示例一直是一个噩梦,对此深表感谢!

Just for completeness, Ember works just fine without Ember Data. 仅出于完整性考虑,如果没有Ember Data,Ember可以正常工作。 You can use POJOs with Ember if you don't feel like using Ember Data. 如果您不想使用Ember Data,可以将POJO与Ember一起使用。

Additionally you need to use async instead of asynch . 另外,您需要使用async而不是asynch

For async records using links is a totally appropriate way to handle the relationships. 对于使用links异步记录,这是一种处理关系的完全合适的方法。

App.Gathering = DS.Model.extend({
    name: DS.attr('string'),
    events: DS.hasMany('event', { async: true })
});

App.Event = DS.Model.extend({
    name: DS.attr('string'),
    startDate: DS.attr('string'),
    endDate: DS.attr('string'),
    gathering: DS.belongsTo('gathering', { async: true })
});

This is totally opinion at this point, but I might do something like this if I were querying for gathering with the id 1 . 这是完全意见,但是如果我要查询id为1收集,我可能会这样做。

To query 查询

this.store.find('gathering', 1);

JSON response JSON回应

{
  gathering: {
    id: 1,
    name: 'foo',
    links: {
      events: '/events?gathering_id=1'
    }
  }
}

When you access the events relationship it would call '/events?gathering_id=1' 当您访问事件关系时,它将调用“ / events?gathering_id = 1”

Example: http://emberjs.jsbin.com/OxIDiVU/906/edit 示例: http//emberjs.jsbin.com/OxIDiVU/906/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM