From dac888ce1f1b3613e4b9d1f0d4cd8c449329443b Mon Sep 17 00:00:00 2001 From: pangratz Date: Mon, 4 May 2015 20:51:25 +0200 Subject: [PATCH] Fix bug where record rejected via `find` stayed in loading state `store.getById(typeClass, id)` is used inside the rejection handler of `find` to get the record and invoke the corresponding hooks. This is buggy since the record is not loaded yet into the store - a successful `find` would mark it as such - and hence the `store.getById` returned `null`. That's why the hooks are not called and the record stays in the `loading` state, having the `isLoaded` flag still being set to `false`. The fix is to reuse the `record` parameter, which is passed to the `find` method. By this, the hooks are invoked and the record is passed transitioned out of the `loading` state. --- .../ember-data/lib/system/store/finders.js | 10 ++--- .../tests/integration/records/load-test.js | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 packages/ember-data/tests/integration/records/load-test.js diff --git a/packages/ember-data/lib/system/store/finders.js b/packages/ember-data/lib/system/store/finders.js index 53fee781c22..4697be92462 100644 --- a/packages/ember-data/lib/system/store/finders.js +++ b/packages/ember-data/lib/system/store/finders.js @@ -29,13 +29,11 @@ export function _find(adapter, store, typeClass, id, record) { return store.push(typeClass, payload); }); }, function(error) { - var record = store.getById(typeClass, id); - if (record) { - record.notFound(); - if (get(record, 'isEmpty')) { - store.unloadRecord(record); - } + record.notFound(); + if (get(record, 'isEmpty')) { + store.unloadRecord(record); } + throw error; }, "DS: Extract payload of '" + typeClass + "'"); } diff --git a/packages/ember-data/tests/integration/records/load-test.js b/packages/ember-data/tests/integration/records/load-test.js new file mode 100644 index 00000000000..368e4641ff4 --- /dev/null +++ b/packages/ember-data/tests/integration/records/load-test.js @@ -0,0 +1,39 @@ +var hasMany = DS.hasMany; +var Post, Comment, env; +var run = Ember.run; + +module("integration/load - Loading Records", { + setup: function() { + Post = DS.Model.extend({ + comments: hasMany({ async: true }) + }); + + Comment = DS.Model.extend(); + + Post.toString = function() { return "Post"; }; + Comment.toString = function() { return "Comment"; }; + + env = setupStore({ post: Post, comment: Comment }); + }, + + teardown: function() { + run(env.container, 'destroy'); + } +}); + +test("When loading a record fails, the isLoading is set to false", function() { + env.adapter.find = function(store, type, id, snapshot) { + return Ember.RSVP.reject(); + }; + + run(function() { + env.store.find('post', 1).then(null, function() { + // store.recordForId is private, but there is currently no other way to + // get the specific record instance, since it is not passed to this + // rejection handler + var post = env.store.recordForId('post', 1); + + equal(post.get("isLoading"), false, "post is not loading anymore"); + }); + }); +});