Skip to content

Commit

Permalink
Merge pull request #4153 from sly7-7/multiple-reloads-one-request
Browse files Browse the repository at this point in the history
calling reload multiple times on a has many triggers only one request
  • Loading branch information
bmac committed Feb 17, 2016
2 parents f4d3b9e + d0334a6 commit dc5197e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
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;
}
};

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 @@ -617,6 +617,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 @@ -647,6 +679,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

0 comments on commit dc5197e

Please sign in to comment.