Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling reload multiple times on a has many triggers only one request #4153

Merged
merged 1 commit into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions addon/-private/system/relationships/state/has-many.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from "ember-data/-private/debug";
import { PromiseManyArray } from "ember-data/-private/system/promise-proxies";
import { PromiseManyArray, promiseManyArray } from "ember-data/-private/system/promise-proxies";
import Relationship from "ember-data/-private/system/relationships/state/relationship";
import OrderedSet from "ember-data/-private/system/ordered-set";
import ManyArray from "ember-data/-private/system/many-array";
Expand Down Expand Up @@ -99,7 +99,6 @@ ManyRelationship.prototype.notifyRecordRelationshipAdded = function(record, idx)
};

ManyRelationship.prototype.reload = function() {
var self = this;
var manyArrayLoadedState = this.manyArray.get('isLoaded');

if (this._loadingPromise) {
Expand All @@ -112,13 +111,13 @@ ManyRelationship.prototype.reload = function() {
}

if (this.link) {
return this.fetchLink();
this._loadingPromise = promiseManyArray(this.fetchLink(), 'Reload with link');
return this._loadingPromise;
} else {
return this.store.scheduleFetchMany(this.manyArray.toArray()).then(function() {
//Goes away after the manyArray refactor
self.manyArray.set('isLoaded', true);
return self.manyArray;
});
this._loadingPromise = promiseManyArray(this.store.scheduleFetchMany(this.manyArray.toArray()).then(() => {
return this.manyArray;
}), 'Reload with ids');
return this._loadingPromise;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pangratz would you mind adding a test to ensure this else case is working correctly as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly delegating to @sly7-7 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Sorry about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 No problem, I update this today.

}
};

Expand Down
65 changes: 65 additions & 0 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,38 @@ test("A hasMany relationship can be directly reloaded if it was fetched via link
});
});

test("Has many via links - Calling reload multiple times does not send a new request if the first one is not settled", function(assert) {
assert.expect(1);
let done = assert.async();

Post.reopen({
comments: DS.hasMany('comment', { async: true })
});

env.adapter.findRecord = function(store, type, id) {
return Ember.RSVP.resolve({ id: 1, links: { comments: "/posts/1/comments" } });
};

let count = 0;
env.adapter.findHasMany = function(store, record, link, relationship) {
count++;
return Ember.RSVP.resolve([
{ id: 1, body: "First" },
{ id: 2, body: "Second" }
]);
};
run(function() {
env.store.findRecord('post', 1).then(function(post) {
post.get('comments').then(function(comments) {
Ember.RSVP.all([comments.reload(), comments.reload(), comments.reload()]).then(function(comments) {
assert.equal(count, 2, "One request for the original access and only one request for the mulitple reloads");
done();
});
});
});
});
});

test("A hasMany relationship can be directly reloaded if it was fetched via ids", function(assert) {
Post.reopen({
comments: DS.hasMany('comment', { async: true })
Expand Down Expand Up @@ -657,6 +689,39 @@ test("A hasMany relationship can be directly reloaded if it was fetched via ids"
});
});

test("Has many via ids - Calling reload multiple times does not send a new request if the first one is not settled", function(assert) {
assert.expect(1);
let done = assert.async();

Post.reopen({
comments: DS.hasMany('comment', { async: true })
});

env.adapter.findRecord = function(store, type, id, snapshot) {
return Ember.RSVP.resolve({ id: 1, comments: [1,2] });
};

let count = 0;
env.adapter.findMany = function(store, type, ids, snapshots) {
count++;
return Ember.RSVP.resolve([
{ id: 1, body: "FirstUpdated" },
{ id: 2, body: "Second" }
]);
};

run(function() {
env.store.findRecord('post', 1).then(function(post) {
post.get('comments').then(function(comments) {
Ember.RSVP.all([comments.reload(), comments.reload(), comments.reload()]).then(function(comments) {
assert.equal(count, 2, "One request for the original access and only one request for the mulitple reloads");
done();
});
});
});
});
});

test("PromiseArray proxies createRecord to its ManyArray once the hasMany is loaded", function(assert) {
assert.expect(4);

Expand Down