samedi 16 janvier 2016

How do I make multiple requests with an Ember adapter?

I'm building an Ember app with Parse as my backend. Parse out of the box does something odd with relationships – they only work one way. :/ I've been down the road of trying to manually make relationships work both ways by saving both models in Ember, but that seems messy and can mean that data gets out of sync quite easily. Yuck. So now I'm looking for alternatives.

First, let me explain in a bit more detail.

Let's say I have a post and comments. Parse will store the relationship from comments -> post, but not the other way around. Or, I can manage a list of comments in a 'Pointer array', but not the one-to-one relationship from comments -> post.

I see two options:

  1. Modify the way the API works using their Cloud Code platform. This might be the easier route, but I'd rather address this in Ember so I can open source my adapter/serializer for others who run into this issue.
  2. Figure out a way to make multiple requests in my adapter. I imagine it would work something like this:

    a. this.store.findRecord('post', id) is called in the app code.

    b. Ember adapter sees that post has a hasMany relationship based on what's defined in the model.

    c. The adapter generates a url that looks for comments where the post's id matches the one I'm trying to find (pretty easy to do with Parse). Yes, this means multiple requests, but only 1 more (per relationship). It would be similar to what Ember does when there is a links attribute in the Payload, but we don't have that since Parse doesn't recognize the hasMany relationship`.

Looking for 2 things. First, any thoughts on if I'm thinking about this correctly, or suggestions on other options. Second, and probably most important, some ideas on what hook I can latch onto in my adapter to fire off a second request. My initial thought was something like this:

findRecord: function(store, type, id, snapshot) {
  snapshot.eachRelationship(relationship => {
    // Do something to build a url for each hasMany relationship
  });

  return Ember.RSVP.promise(...);
}

Any help with this is much appreciated.




Aucun commentaire:

Enregistrer un commentaire