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

[BUGFIX release] Add model hook in route blueprint #19860

Merged
merged 1 commit into from Nov 30, 2021
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
9 changes: 8 additions & 1 deletion 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;
},<%}%>
});
2 changes: 2 additions & 0 deletions blueprints/route/index.js
Expand Up @@ -75,6 +75,7 @@ module.exports = useEditionDetector({
let moduleName = options.entity.name;
let rawRouteName = moduleName.split('/').pop();
let emberPageTitleExists = 'ember-page-title' in options.project.dependencies();
let hasDynamicSegment = options.path && options.path.includes(':');

if (options.resetNamespace) {
moduleName = rawRouteName;
Expand All @@ -84,6 +85,7 @@ module.exports = useEditionDetector({
moduleName: stringUtil.dasherize(moduleName),
routeName: stringUtil.classify(rawRouteName),
addTitle: emberPageTitleExists,
hasDynamicSegment,
};
},

Expand Down
9 changes: 8 additions & 1 deletion 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;
}<%}%>
}
6 changes: 4 additions & 2 deletions node-tests/blueprints/route-test.js
Expand Up @@ -81,7 +81,7 @@ describe('Blueprint: route', function () {

it('route foo --path=:foo_id/show', function () {
return emberGenerateDestroy(['route', 'foo', '--path=:foo_id/show'], (_file) => {
expect(_file('app/routes/foo.js')).to.equal(fixture('route/route.js'));
expect(_file('app/routes/foo.js')).to.equal(fixture('route/route-with-dynamic-segment.js'));

expect(_file('app/templates/foo.hbs')).to.equal('{{outlet}}');

Expand Down Expand Up @@ -560,7 +560,9 @@ describe('Blueprint: route', function () {

it('route foo --path=:foo_id/show', function () {
return emberGenerateDestroy(['route', 'foo', '--path=:foo_id/show'], (_file) => {
expect(_file('app/routes/foo.js')).to.equal(fixture('route/native-route.js'));
expect(_file('app/routes/foo.js')).to.equal(
fixture('route/native-route-with-dynamic-segment.js')
);

expect(_file('app/templates/foo.hbs')).to.equal('{{outlet}}');

Expand Down
11 changes: 11 additions & 0 deletions node-tests/fixtures/route/native-route-with-dynamic-segment.js
@@ -0,0 +1,11 @@
import Route from '@ember/routing/route';

export default class FooRoute extends Route {
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;
}
}
11 changes: 11 additions & 0 deletions node-tests/fixtures/route/route-with-dynamic-segment.js
@@ -0,0 +1,11 @@
import Route from '@ember/routing/route';

export default Route.extend({
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;
},
});