Skip to content

Refactor "Specifying a route's model" to be agnostic of data library choice - #708

Merged
jenweber merged 12 commits into
masterfrom
route-models
May 1, 2019
Merged

jenweber merged 12 commits into
masterfrom
route-models

Conversation

@jenweber

Copy link
Copy Markdown
Contributor

See #20 for the motivation. In short, in order to make Ember Data guides a standalone resource and learning concept, we need to refactor how we teach data fetching in other parts of the app. I've made this article longer, and I don't take that lightly, but based on my experience teaching others, we can't skimp on this. Understanding models is one of the greatest new learner pain points, so more examples and simpler language are critical.

If there are controversial refactors, I can break this into multiple PRs, but I think it's best evaluated as a whole.

This PR is a broad refactor of https://guides.emberjs.com/release/routing/specifying-a-routes-model/

Here's how I recommend reviewing:

  • Read this entire PR comment first
  • Read my version and see if it checked all the boxes that the old version did (the diff is going to be hard to read)
  • Scrutinize the code samples for mistakes
  • See if the raw new Promise example makes any sense. I am skeptical.
  • Look for typos

Changes

Here's a summary of the things I changed and the things I didn't.

  • I did not destroy or edit any heading anchors, aside from making one header one level deeper
  • I moved "multiple models" higher up the page
  • I added two new heading anchors within the existing content, "Linking to a dynamic segment" and "Creating a dynamic segment" since this article is so long
  • I did not remove any learning concept scope
  • I added more examples of fetching data in the model
  • I created a debugging section
  • I wrote a proper intro. The article introduced models like someone already knows what they are and what they do.
  • incorporated generating a dynamic segment using the Ember CLI
  • strengthen the "why" for using dynamic segments

Rationale for the biggest changes

The biggest changes are:

  • Adding more (non-Ember-Data) examples for what to return from the model hook.
    • This is motivated by Quest - Improvements to teaching Ember Data #20, the desire to make Ember Data learnable as a stand-alone resource. It's still worth mentioning, I think.
    • The ember-ajax example is provided based on discussion in Quest - Improvements to teaching Ember Data #20.
    • The "static data" example is based on trying to help a new JS dev who was stuck for a week on models because they couldn't tell whether they were doing async wrong or doing something wrong in the template.
    • The raw Promise example is to drive home that you can do whatever you want in the model hook when it comes to async.
  • Adding a debugging section.
    • This is based on personal experience teaching other devs, who needed help every time they were writing a model hook from scratch. I would have loved to send them a link to this.
  • Adding a summary statement of the powers of model hooks.
    • This is providing some rationale for loading data in the route instead of hand-rolling async stuff in components.
  • I changed the dynamic segment example to use :id instead of :photos_id
    • The underscore will make a modern Ember app's linter yell at you. Dashes are pain.
  • Clarified that dynamic segments can have any key name that you feel like in params. It's not something magical.

Learning topics that are covered

In the old version, the following learning topics were covered. This refactor fulfills the scope of the old version as follows:

  • One job of a route is to display data in the model
  • Show example router.js
  • Show a model hook in use, which returns and Ember Data promise
  • describe that a model hook can return any Promise
  • say that the model is received by a Controller and then provided to a template
  • show what a dynamic segment looks like and why it should be used
  • Explain gotchas for link-tos and dynamic segments
  • handling multiple models (totally unchanged except moving it up the page)
  • Reusing route context (totally unchanged)

@acorncom acorncom left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jenweber this is a GREAT start. There are a few items I think it's worth us cleaning up, but after we do that, I'm firmly in favor of merging this and not letting it sit for debate for too long. We can always improve it in future PRs

@locks locks self-assigned this Mar 30, 2019
@mike-north

Copy link
Copy Markdown
Contributor

Extracting ember-data out into an independent topic would be a huge positive change for the beginning of the learning experience. I hope we can continue moving in this direction, and ultimately land at a place where the first time readers experiment with Ember, it's in the simplest context possible, involving only the most important entities and concepts.

@lisaychuang lisaychuang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Initial review as requested by @jenweber :)

Thank you for your hard work updating this guide! It's past midnight so I'll look through the rest tomorrow!

@lisaychuang lisaychuang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Added more comments with questions, typos noted etc.

I find the section on Reusing Route Context highly confusing. Since this section was not edited by @jenweber , I'll suggest reviewing the section to determine if it should be removed or updated.

Thank you!

Sometimes you need to fetch a model, but your route doesn't have the parameters, because it's
a child route and the route directly above or a few levels above has the parameters that your route
needs.
You might run into this if you have a URL like `/photos/4/comments/18`, and when you're in the comments route, you need a photo ID.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

when you're in the comments route, you need a photo :id

In this example, you are using the convention of mode_id instead of just plain :id ( i.e. album_id), this might be confusing for readers as the previous examples above are using :id?

Since the code example uses song and album, change the URL example to match that instead (e.g. /albums/4/asongs/18)


The section below was not edited so I can't add comments inline:

Firstly, talking to @mike-north he said that trying to get query params from the parents' route is an advanced concept, and an uncommon thing to do. If we want to include the most important 20% of content that people need 80% of the time, this section does not pass the 80/20 rule. This looks like an an edge case and have "code smell".

Specific comments on why this section is confusing:

  • Line 305 - 306:

This is guaranteed to work because the parent route is loaded. But if you tried to do paramsFor on a sibling route, you wouldn't have the results you expected.

Q1: Why do we have a guarantee with the parent route? Simply stating a guarantee is in one place but not the other doesn't help me form a mental model.
Q2: Why is using this method with a sibling route undesirable?

  • Line 308

This is a great way to use the parent context to load something that you want.
Q: What does the generic term "something that you want" mean?
In the example of song loading something from album, are we referring to data on the album e.g. album name?

  • Line 309 - 311

Using paramsFor will also give you the query params defined on that route's controller.
This method could also be used to look up the current route's parameters from an action
or another method on the route, and in that case we have a shortcut: this.paramsFor(this.routeName).

😩I have no idea what this paragraph means.

  • Line 313

In our case, the parent route had already loaded its songs, so we would be writing unnecessary fetching logic.

There has been no explanation on the parent route and how it loads data so far, so how did we arrive at this conclusion that songs are already loaded?

  • Line 329

In the case above, the parent route looked something like this
Q1: Again, I'm still confused about what this example is
Q2: Why do we need to use RSVP.hash in the parent route?

I think either this entire section needs to be eliminated, or it need to be reviewed and updated to a sensible order:

  • Start explaining from the "child route" (e.g. song) and move upwards (e.g. `album)
  • Start explaining from the parent route and move downwards to child route

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree on a lot of these points. I didn't touch this section because it was super messy :P I would like to break this out into a new Issue/PR

@jenweber
jenweber requested a review from acorncom April 25, 2019 00:22
@jenweber

Copy link
Copy Markdown
Contributor Author

@mike-north @lisaychuang @zachgarwood and @acorncom, thanks so much for the reviews! This is such an important part of understanding Ember, and I'm thankful we all invested the time to make it better :)

@acorncom will you do one more read through? I think I have addressed all the comments, except about how reusing a route's context is super confusing. I agree and would like to address it in a separate PR.

@jenweber

Copy link
Copy Markdown
Contributor Author

Also please squash & merge this when it's ready, or our git history will be ruined forever, haha

@jenweber

jenweber commented Apr 25, 2019 •

Copy link
Copy Markdown
Contributor Author

Wow something bad happened with the GitHub diff... locally git diff --name-only master shows only two files changed. @acorncom any ideas? I think it's this issue because it's an old PR. If you can explain it, you can merge this, but if there's any doubt, here's a clean PR #765

@mansona mansona left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some things (like links) that really should be changed, one or two minor suggestions that can be ignored and a few discussion points 👍


If you decide to pass the entire model, be sure to cover this behavior in your acceptance tests.

If a route you are trying to link to has multiple dynamic segments, like `/photos/4/comments/18`, be sure to specify all the necessary information for each segment:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so this isn't 100% true 🤔 and to be honest I don't know how you would teach the alternative because it's quite a weird behaviour.

So first of all this is true if you're anywhere in the app not under the photos route. But if you're on /photos/4 you can link to

{{#link-to "photos.photo.comments.comment" 18}}
  link text to display
{{/link-to}}

and it will keep you within the first dynamic segment and use 18 for the second 🙈 kinda strange but c'est la vie 🤷‍♂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the interest of teaching the happy path in the Guides, I think we should keep it as-is.

needs.
You might run into this if you have a URL like `/photos/4/comments/18`, and when you're in the comments route, you need a photo ID.

In this scenario, you can use the `paramsFor` method to get the parameters of a parent route.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel like we should use album_id and comment_id in this example to make it more realistic 🤔 also aren't you saying that we're in the comments route? shouldn't the file name for this be app/routes/photo/comment.js or something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and to that point... if the code examples are talking about albums and songs shouldn't our in-prose example also be using the same model types?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, however I would like to address this in a future PR. This discrepancy is present in the current guides: https://guides.emberjs.com/release/routing/specifying-a-routes-model/

@jenweber
jenweber requested a review from mansona May 1, 2019 00:47

@mansona mansona left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have reviewed this extensively and double checked all the links and I'm happy that we can now merge it 👍 there are a few things that could still be improved (as ever) but at this stage, your work is a significant improvement on the previous page that we need to get this merged ASAP.

Instead of suggesting my changes I pushed one commit with the last fixes. If you are happy with those changes then push that merge button 🎉 :shipit:

@mansona
mansona dismissed acorncom’s stale review May 1, 2019 08:43

significant reviews since this last review 👍

@jenweber
jenweber merged commit 8a1b1d4 into master May 1, 2019
@mansona
mansona deleted the route-models branch May 1, 2019 13:06
@mansona

mansona commented May 1, 2019

Copy link
Copy Markdown
Member

🎉 congratulations @jenweber

@jenweber

jenweber commented May 1, 2019

Copy link
Copy Markdown
Contributor Author

Thanks everyone for the reviews! Merged.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants