简体   繁体   中英

Ember JS : How to reuse ember data while calling?

I have two models to call. In the first model I include somedate like this.

this.store.query('comment',{
   include : 'person,address'
});

And in the second call I include the same details that already stored in the store.

this.store.query('post',{
   include : 'person,address'
});

So, the API call takes a lot of time to resolve. Is there any way I can use the first API include data in the second API call to create a relationship between those two models(person, address). This would save a lot of time for me.

Note: Examples are testing purpose only.

You are using the query() method of Ember Data's store . It expects two arguments: the model name as first argument and the query as a second argument. Last one is directly passed to your backend as part of the request. The responsible code is quite simple: https://github.com/emberjs/data/blob/v3.10.0/addon/adapters/rest.js#L535-L560

If you are using default JSONAPIAdapter the requests executed by your method calls look like this:

this.store.query('comment', { include: 'person,address' });
=> GET /comments?include=person,address

this.store.query('post', { include: 'person,address' });
=> GET /posts?include=person,address

The API does not know from that request that the client already has some of the person and address records cached locally. Ember Data does not include that information by default. You could customize your Adapter two do it but I wouldn't recommend so - especially cause that may blow up the request size and reduce the cache hit rate by a fair amount. Also you may want to reload the locally cached records.

If you expect two have most of the related records already cached locally, you may simply not want to ask the server to include them? In that case it might be cheaper to load them afterwards in a coalesced request .

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