简体   繁体   中英

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.

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. 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. 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. 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.

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.

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!

Just for completeness, Ember works just fine without Ember Data. You can use POJOs with Ember if you don't feel like using Ember Data.

Additionally you need to use async instead of asynch .

For async records using links is a totally appropriate way to handle the relationships.

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 .

To query

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

JSON response

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

When you access the events relationship it would call '/events?gathering_id=1'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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