javascript - How do I make multiple requests with an Ember adapter? -


i'm building ember app parse backend. parse out of box odd relationships – they work 1 way. :/ i've been down road of trying manually make relationships work both ways saving both models in ember, seems messy , can mean data gets out of sync quite easily. yuck. i'm looking alternatives.

first, let me explain in bit more detail.

let's have post , comments. parse store relationship comments -> post, not other way around. or, can manage list of comments in 'pointer array', not one-to-one relationship comments -> post.

i see 2 options:

  1. modify way api works using cloud code platform. might easier route, i'd rather address in ember can open source adapter/serializer others run issue.
  2. figure out way make multiple requests in adapter. imagine work this:

    a. this.store.findrecord('post', id) called in app code.

    b. ember adapter sees post has hasmany relationship based on what's defined in model.

    c. adapter generates url looks comments post's id matches 1 i'm trying find (pretty easy parse). yes, means multiple requests, 1 more (per relationship). similar ember when there links attribute in payload, don't have since parse doesn't recognize hasmany relationship`.

looking 2 things. first, thoughts on if i'm thinking correctly, or suggestions on other options. second, , important, ideas on hook can latch onto in adapter fire off second request. initial thought this:

findrecord: function(store, type, id, snapshot) {   snapshot.eachrelationship(relationship => {     // build url each hasmany relationship   });    return ember.rsvp.promise(...); } 

any appreciated.

so, you're trying override default findrecord behaviour of ember-data.

your current approach sounds right. can create adapter , provide custom definition methods such findrecord, findall, query , on.

a typical code example can :

findrecord(store, type, id, snapshot) {     let data = {};     const traditional = true;       return new ember.rsvp.hash({         news: ember.$.ajax({ url: wpurl + 'post', datatype: "jsonp", type: 'get' }),         events: ember.$.ajax({ url: wpurl + 'comments', datatype: "jsonp", type: 'get' })     }); }); 

the code snippet vague, hope got point..


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -