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

Improve current-user service guides #1139

Merged
merged 1 commit into from
Dec 16, 2016

Conversation

juanazam
Copy link
Contributor

This PR aims to improve current-user service guides a bit, including the case when using a dedicated endpoint but including the actual current using id in the response instead of a special value (e.g.: me).

The newly added section explains why using EmberData'sfind method with id me and returning an actual id in the response is a problem. For more information, please check: emberjs/data#4414.

It also closes a stalled issue and PR:
#1064
#1065

@marcoow marcoow requested a review from pangratz December 15, 2016 14:05
@marcoow
Copy link
Member

marcoow commented Dec 15, 2016

@pangratz: can you review?


Using the `queryRecord` method and overriding the adapter avoid this problem.
The adapter override will generate `api/users/me` when the `me` query param is
present.

Choose a reason for hiding this comment

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

Let's change the ending to something like this:

... when store.queryRecord() is invoked with a query where the me param is present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

});
let userId = this.get('session.data.authenticated.user_id');
if (!isEmpty(userId)) {
return this.get('store').find('user', userId).then((user) => {

Choose a reason for hiding this comment

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

store.findRecord is the public way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


If the `find` method is called with id `me` and the response includes the actual
user id, ember data will create an empty record with id `me` and all other model
attributes empty.
Copy link

@pangratz pangratz Dec 16, 2016

Choose a reason for hiding this comment

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

find is actually private, I think we should use store.findRecord here as it is more idiomatic ember-data use, even though it has been mentioned before.

I am not sure we should state here why findRecord('user', 'me') is the wrong choice and instead say why we are using store.queryRecord(): because it's the way to fetch a record in ember data when the id is not known beforehand.

And then maybe outline with a Note: why we are not using findRecord("user", "me"): because ember data expects the returned model to have the same id, as otherwise an unused empty record with id of me is in the store.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, I'll reword it :)

@pangratz
Copy link

@juanazam overall this looks great! Thanks so much for tackling this!

@juanazam juanazam force-pushed the improve_current_user_guides branch from cdb86cc to 81bd8f6 Compare December 16, 2016 13:49
@juanazam
Copy link
Contributor Author

@pangratz thanks for reviewing and commenting! I have updated my changes with your suggestions, hope it looks ok now.

Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

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

Thanks for the effort!

@@ -64,14 +64,57 @@ export default Ember.Service.extend({

load() {
if (this.get('session.isAuthenticated')) {
return this.get('store').find('user', 'me').then((user) => {
return this.get('store').findRecord('user', 'me').then((user) => {
Copy link
Member

Choose a reason for hiding this comment

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

We should probably have queryRecord and make what is described in "Using a dedicated endpoint and returning actual user id in response" below the default recommendation as findRecord will lead to problems and thus shouldn't be in the guides at all.

Copy link
Contributor Author

@juanazam juanazam Dec 16, 2016

Choose a reason for hiding this comment

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

So, if I understood correctly, we should remove the using a dedicated endpoint section right? or use that title with the code used in the queryParams example.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, replace this sample with the one using queryRecord you added.

unused empty record with `id` of `me` is in the store.

The adapter override will generate `api/users/me` when `store.queryRecord` is
invoked with a query param where the `me` param is present.
Copy link
Member

Choose a reason for hiding this comment

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

We should put the explanation before the code sample so the guide is easier to follow when reading through it top-down.

@juanazam juanazam force-pushed the improve_current_user_guides branch from 81bd8f6 to c6aafe2 Compare December 16, 2016 14:14
@juanazam
Copy link
Contributor Author

@marcoow thanks for your suggestions, I have included those changes too.

@marcoow marcoow merged commit d253c7e into mainmatter:master Dec 16, 2016
@juanazam
Copy link
Contributor Author

🎉

let userId = this.get('session.data.authenticated.user_id');
if (!isEmpty(userId)) {
return this.get('store').findRecord('user', userId).then((user) => {
this.set('user', user);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't there be another return Ember.RSVP.resolve(); here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants