mercredi 24 février 2016

in Ember.js 2.3, how do I compile a hasMany async call into one call in ember instead of several?

I'm upgrading to ember-cli and ember 2.3. Say I have a model called User and a model called Post , and a user ...

posts: DS.hasMany('post', {async:true})

Now, this works the way I expect it to, lazily loading data and not loading posts unless it is required in either the .js or the template. So when I do

{{#each user.posts as |post|}}
  {{post.title}}
{{/each}}

I get each post to render its title without a problem. However, in my server logs, I see this:

GET /posts/2
GET /posts/7
GET /posts/13

where the numbers are the post ids. This is to be expected, as when I return a user instance from the server, I return a list of the ids as the parameter 'posts'. So the user instance has:

...
'posts': '2,7,13'
...

in its data.

Now my question is this: way back when, when I used ember-data 1.0 (pre ember-cli and pre ember 1.13), I remember this call being made to the database instead for the same use case:

GET /posts?ids=2&7&13

or something along that line. I can't remember the exact format, but then, I could access the list of ids on the server side using this line of code:

var ids = req.query.ids.toString();

which gave me a comma separated list of ids (in string format). I would then convert this into the sql statement

SELECT * from posts where id in (2,7,13)

This SQL call was interpreted as a manyArray, I think, on the Ember Side and easily behaved as you would expect an Ember Array would.

How can I get this to happen again? I am quite confident that I am missing something and that I don't have to 'hack' ember-data; I would very much like to compress these calls into one instead of having an individual call to the database for each 'post'.

I should also mention that I am not looking to make {async:false} for these calls.




Aucun commentaire:

Enregistrer un commentaire