From 69915f3a53dd0f31d76def7f7870a76a1cc1a464 Mon Sep 17 00:00:00 2001 From: Matthew Beale Date: Tue, 30 Nov 2021 14:08:49 -0500 Subject: [PATCH] [BUGFIX release] Add model hook in route blueprint When generating a route with a dynamic segment, say via: ember g route foo --path="bar/:buzz_id" The default empty route definition will cause an awkward assertion to be thrown. * In 3.28 without any data layer, the user is prompted via assertion to implement a model hook. * In 3.28 with Ember Data, an implicit fetch via Ember Data happens. * In 4.0 without any data layer, the user would be prompted via assertion to implement a model hook. * In 4.0 with Ember Data, the user would be prompted via assertion to either add a `find` method (old assertion) or to implement a model hook (new assertion via https://github.com/emberjs/ember.js/pull/19858). It is doubtless that many users will still encounter these behaviors, but updating the blueprints to generate a model hook by default improves on the happy path. In theory this could do back to 3.28, however the value there is somewhat less since Ember Data's implicit store injection remains in that version (and therefore the assertions/messages are less confusing). --- blueprints/route/files/__root__/__path__/__name__.js | 11 +++++++++-- blueprints/route/index.js | 1 + .../route/native-files/__root__/__path__/__name__.js | 11 +++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/blueprints/route/files/__root__/__path__/__name__.js b/blueprints/route/files/__root__/__path__/__name__.js index 6c74252aa1b..7ff415537db 100644 --- a/blueprints/route/files/__root__/__path__/__name__.js +++ b/blueprints/route/files/__root__/__path__/__name__.js @@ -1,4 +1,11 @@ import Route from '@ember/routing/route'; -export default Route.extend({ -}); +export default Route.extend({<% if (hasDynamicSegment) {%> + model(params) { + /** + * This route was generated with a dynamic segment. Implement data loading + * based on that dynamic segment here in the model hook. + */ + return params; + } +<%}%>}); diff --git a/blueprints/route/index.js b/blueprints/route/index.js index 50b10685a4b..d45306a9a2b 100644 --- a/blueprints/route/index.js +++ b/blueprints/route/index.js @@ -84,6 +84,7 @@ module.exports = useEditionDetector({ moduleName: stringUtil.dasherize(moduleName), routeName: stringUtil.classify(rawRouteName), addTitle: emberPageTitleExists, + hasDynamicSegment: options.entity.path?.includes(':'), }; }, diff --git a/blueprints/route/native-files/__root__/__path__/__name__.js b/blueprints/route/native-files/__root__/__path__/__name__.js index 508c4dbe00c..ea35f564e3f 100644 --- a/blueprints/route/native-files/__root__/__path__/__name__.js +++ b/blueprints/route/native-files/__root__/__path__/__name__.js @@ -1,4 +1,11 @@ import Route from '@ember/routing/route'; -export default class <%= classifiedModuleName %>Route extends Route { -} +export default class <%= classifiedModuleName %>Route extends Route {<% if (hasDynamicSegment) {%> + model(params) { + /** + * This route was generated with a dynamic segment. Implement data loading + * based on that dynamic segment here in the model hook. + */ + return params; + } +<%}%>}