Skip to content

Instantly share code, notes, and snippets.

@rossjha
Last active September 8, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossjha/347b4f6978a4c37461da7f806535f897 to your computer and use it in GitHub Desktop.
Save rossjha/347b4f6978a4c37461da7f806535f897 to your computer and use it in GitHub Desktop.
routes example
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany, belongsTo } from 'ember-data/relationships';
export const schema = {
// Attributes
name: attr('string')
// Associations
// Computed Properties
};
export default Model.extend(schema);
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('restricted-tasks', function() {
this.route('show', { path: '/:task_id' });
});
});
export default Router;
import Route from '@ember/routing/route';
export default Route.extend({
model() {
return [{
id: '1',
name: 'Task One'
}, {
id: '2',
name: 'Task Two'
}];
}
});
import Route from '@ember/routing/route';
export default Route.extend({
model({ task_id }) {
// stubbed task
return {
id: '1',
name: 'Task One'
}
}
});
<h1>Test Routes</h1>
{{#link-to "restricted-tasks"}}Restricted Tasks{{/link-to}}
{{outlet}}
<h1>Restricted Tasks (index)</h1>
{{#each model as |task|}}
{{!-- if you pass `task` (model) instead of `task.id` then `/restricted-tasks/show` won't invoke the model hook. Generally we want to do invoke... --}}
<div>{{link-to task.name "restricted-tasks.show" task.id}}</div>
{{/each}}
<h1>Restricted Task</h1>
<div>{{model.name}}</div>
<div>stubbing the model here, but you should see the URL with the task_id. Check the router to see this path structure i.e. { path: '/:task_id' }. You could do { path: '/:task_id/show' } for example</div>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "2.18.2",
"ember-template-compiler": "2.18.2",
"ember-testing": "2.18.2"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "2.18.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment