Skip to content
This repository

Fix async belongsTo to return the record when data it's already loaded #1320

Merged
merged 1 commit into from 7 months ago

5 participants

Andrei Mirestean Stefan Penner Bradley Priest Alex White Yehuda Katz
Andrei Mirestean

If the record referenced by a belongsTo async relationship is already loaded in the store, record.get('...') currently returns null.

Stefan Penner

I am currently of the mindset, that these associations should always be async, and should always return a thenable proxy. @wycats we should hash out these async things soon.

Bradley Priest
Collaborator

Dup of #1314, but this one has a test.

Yehuda Katz wycats merged commit f60a600 into from
Yehuda Katz wycats closed this
Alex White

Let me know if there is anything I can do to get this working. I have manually modified ember data to get this working in my project for now.

Bradley Priest
Collaborator

@abobwhite this fix has been merged, are you still seeing issues on Canary?

Alex White

@bradleypriest The latest canary is working splendidly for me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
3  packages/ember-data/lib/system/relationships/belongs_to.js
@@ -19,7 +19,8 @@ function asyncBelongsTo(type, options, meta) {
19 19
     belongsTo = data[key];
20 20
 
21 21
     if(!isNone(belongsTo)) {
22  
-      return store.fetchRecord(belongsTo);
  22
+      var promise = store.fetchRecord(belongsTo) || Ember.RSVP.resolve(belongsTo);
  23
+      return DS.PromiseObject.create({promise: promise});
23 24
     } else {
24 25
       return null;
25 26
     }
25  packages/ember-data/tests/unit/model/relationships_test.js
@@ -516,6 +516,31 @@ test("async belongsTo relationships work when the data hash has not been loaded"
516 516
   }));
517 517
 });
518 518
 
  519
+test("async belongsTo relationships work when the data hash has already been loaded", function() {
  520
+  var Tag = DS.Model.extend({
  521
+    name: DS.attr('string')
  522
+  });
  523
+
  524
+  var Person = DS.Model.extend({
  525
+    name: DS.attr('string'),
  526
+    tag: DS.belongsTo('tag', { async: true })
  527
+  });
  528
+
  529
+  var env = setupStore({ tag: Tag, person: Person }),
  530
+      store = env.store;
  531
+
  532
+    store.push('tag', { id: 2, name: "friendly"});
  533
+    store.push('person', { id: 1, name: "Tom Dale", tag: 2});
  534
+
  535
+    store.find('person', 1).then(async(function(person) {
  536
+        equal(get(person, 'name'), "Tom Dale", "The person is now populated");
  537
+        return get(person, 'tag');
  538
+    })).then(async(function(tag) {
  539
+        equal(get(tag, 'name'), "friendly", "Tom Dale is now friendly");
  540
+        equal(get(tag, 'isLoaded'), true, "Tom Dale is now loaded");
  541
+  }));
  542
+});
  543
+
519 544
 test("calling createRecord and passing in an undefined value for a relationship should be treated as if null", function () {
520 545
   var Tag = DS.Model.extend({
521 546
     name: DS.attr('string'),
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.