Skip to content

Instantly share code, notes, and snippets.

@hjdivad
Created April 8, 2014 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjdivad/10081896 to your computer and use it in GitHub Desktop.
Save hjdivad/10081896 to your computer and use it in GitHub Desktop.

TL;DR

  1. user.get('posts') should resolve as soon as the ids are known, possibly before the records are loaded;
  2. user.get('posts').objectAt(0) should return a PromiseObject and also fetch that object;
  3. The fetches from #2 should be coalesced and;
  4. user.get('posts').load() should fetch the records and return a promise that resolves when they are all loaded.

HasMany promise semantics

We want to be able to modify hasMany memberhips without loading the records, at least in the non-link case. For instance

user.get('posts').then(function (posts) {
  posts.pushObject(post);
})

should not need to actually fetch all of the user's posts.

To this end, the PromiseArray returned by a hasMany CP should:

  1. Only guarantee that ids and length are loaded upon fulfillment (not necessarily the records themselves) and 2
  2. Return PromiseObjects on objectAt and fetch those objects. These fetches would need to be coalesced to avoid n+1 queries.
{{!-- This loop would call `objectAt` n times, but they would be coalesed into a
single request for the posts --}}

{{#each user.posts}}
  {{#link-to 'post', this}}{{title}}{{/link-to}}
{{/each}}
user.get('posts').then(function (posts) {
  posts.objectAt(0).get('isFullfilled') // => false
  posts.objectAt(0).then(function (post) {
    // post is loaded
  });
});

To make it easier to deal with cases where the entire association is needed, we propose adding load on PromiseArray which is analogous to RSVP's all (it is only not called all as its semantics differ enough from store.all that this would be confusing).

// load will actually fetch the posts
user.get('posts').load().then(function (posts) {
  posts.objectAt(0).get('isFulfilled') // => true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment