Skip to content

Instantly share code, notes, and snippets.

@JulianLeviston
Created September 17, 2019 23:30
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 JulianLeviston/0b24cc98412ac1f4786ef833c4b32e1b to your computer and use it in GitHub Desktop.
Save JulianLeviston/0b24cc98412ac1f4786ef833c4b32e1b to your computer and use it in GitHub Desktop.
Ember Twiddle Demo: Simple nested routes

Ember Twiddle Demo: Simple nested routes

Demo

Basic example of nested routes with some CSS to highlight the active route in the navigation links.

import Ember from 'ember';
export default Ember.Controller.extend({
actors: null, // this will be set by setupController only once, when the route is entered with an id.
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('movies', function () {
this.route('actors', { path: ':movie_id' });
});
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
const actorsByMovie = {
1: [
"Mark Hamill",
"Carrie Fisher"
],
2: [
"Liv Tyler",
"Elijah Wood"
],
3: [
"William H Macy",
"Frances McDormand"
]
};
return actorsByMovie[params.movie_id];
},
setupController(controller, model) {
this._super(controller, model);
// set the actors property of the controller
// if it hasn't been set - ie memoize
if(Ember.isEmpty(controller.get('actors'))) {
controller.set('actors', model)
}
},
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return [
{ id: 1, title: 'Star Wars' },
{ id: 2, title: 'Lord of the Rings' },
{ id: 3, title: 'Fargo' },
];
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.active {
font-weight: bold;
color: navy;
}
<h1>Welcome to {{appName}}</h1>
{{#link-to "movies"}}All movies{{/link-to}}
{{outlet}}
<h3>Actors</h3>
<ul>
{{#each actors as |actor|}}
{{log 'rendered' actor}}
<li>{{actor}}</li>
{{/each}}
</ul>
<h3>Movies</h3>
<ul>
{{#each model as |movie|}}
<li>
{{#link-to "movies.actors" movie.id}}
{{movie.title}}
{{/link-to}}
</li>
{{/each}}
</ul>
{{outlet}}
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.18.0",
"ember-data": "2.18.0",
"ember-template-compiler": "2.18.0",
"ember-testing": "2.18.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment