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

[FEAT BUGFIX] adds adapterOptions ability to model.reload() #5414

Merged
merged 1 commit into from
Apr 4, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,13 @@ export default class InternalModel {
}
}

reload() {
reload(options) {
this.startedReloading();
let internalModel = this;
let promiseLabel = "DS: Model#reload of " + this;

return new Promise(function(resolve) {
internalModel.send('reloadRecord', resolve);
internalModel.send('reloadRecord', { resolve, options });
}, promiseLabel).then(function() {
internalModel.didCleanError();
return internalModel;
Expand Down
16 changes: 13 additions & 3 deletions addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,13 +816,23 @@ const Model = EmberObject.extend(Evented, {
```

@method reload
@return {Promise} a promise that will be resolved with the record when the
@param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter request

@return {Promise} a promise that will be resolved with the record when the
adapter returns successfully or rejected if the adapter returns
with an error.
*/
reload() {
reload(options) {
let wrappedAdapterOptions;

if (typeof options === 'object' && options !== null && options.adapterOptions) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rwjblue did you have a recommended way of making this more legible?

wrappedAdapterOptions = {
adapterOptions: options.adapterOptions
};
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to support this here or should we always require users to set adapterOptions?

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind this looks good. 👍


return PromiseObject.create({
promise: this._internalModel.reload().then(() => this)
promise: this._internalModel.reload(wrappedAdapterOptions).then(() => this)
});
},

Expand Down
8 changes: 4 additions & 4 deletions addon/-private/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ const DirtyState = {
internalModel.transitionTo('inFlight');
},

reloadRecord(internalModel, resolve) {
resolve(internalModel.store._reloadRecord(internalModel));
reloadRecord(internalModel, { resolve, options }) {
resolve(internalModel.store._reloadRecord(internalModel, options));
},

rolledBack(internalModel) {
Expand Down Expand Up @@ -580,8 +580,8 @@ const RootState = {
internalModel.transitionTo('updated.inFlight');
},

reloadRecord(internalModel, resolve) {
resolve(internalModel.store._reloadRecord(internalModel));
reloadRecord(internalModel, { resolve, options }) {
resolve(internalModel.store._reloadRecord(internalModel, options));
},

deleteRecord(internalModel) {
Expand Down
5 changes: 3 additions & 2 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,17 +1063,18 @@ Store = Service.extend({
@method reloadRecord
@private
@param {DS.Model} internalModel
@param options optional to include adapterOptions
@return {Promise} promise
*/
_reloadRecord(internalModel) {
_reloadRecord(internalModel, options) {
let { id, modelName } = internalModel;
let adapter = this.adapterFor(modelName);

assert(`You cannot reload a record without an ID`, id);
assert(`You tried to reload a record but you have no adapter (for ${modelName})`, adapter);
assert(`You tried to reload a record but your adapter does not implement 'findRecord'`, typeof adapter.findRecord === 'function' || typeof adapter.find === 'function');

return this._scheduleFetch(internalModel);
return this._scheduleFetch(internalModel, options);
},

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/records/reload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ test("When a single record is requested, the adapter's find method should be cal
});
});

test("When a single record is requested, the adapter's find method should be called unless it's loaded.", function(assert) {
let count = 0;
let reloadOptions = {
adapterOptions: {
makeSnazzy: true
}
};

env.adapter.findRecord = function(store, type, id, snapshot) {
if (count === 0) {
count++;
return resolve({ data: { id: id, type: 'person', attributes: { name: "Tom Dale" } } });
} else if (count === 1) {
assert.equal(snapshot.adapterOptions, reloadOptions.adapterOptions, 'We passed adapterOptions via reload');
count++;
return resolve({ data: { id: id, type: 'person', attributes: { name: "Braaaahm Dale" } } });
} else {
assert.ok(false, "Should not get here");
}
};

run(function() {
env.store.findRecord('person', 1).then(function(person) {
assert.equal(get(person, 'name'), "Tom Dale", "The person is loaded with the right name");
assert.equal(get(person, 'isLoaded'), true, "The person is now loaded");

let promise = person.reload(reloadOptions);

assert.equal(get(person, 'isReloading'), true, "The person is now reloading");

return promise;
}).then(function(person) {
assert.equal(get(person, 'isReloading'), false, "The person is no longer reloading");
assert.equal(get(person, 'name'), "Braaaahm Dale", "The person is now updated with the right name");
});
});
});

test("When a record is reloaded and fails, it can try again", function(assert) {
var tom;
run(function() {
Expand Down