Skip to content

Instantly share code, notes, and snippets.

@rileyhilliard
Last active January 28, 2021 21:52
Show Gist options
  • Save rileyhilliard/c8ca21d0ecc5ee026756a042ceeb052c to your computer and use it in GitHub Desktop.
Save rileyhilliard/c8ca21d0ecc5ee026756a042ceeb052c to your computer and use it in GitHub Desktop.
router.transitionTo() Twiddle
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
import Controller from '@ember/controller';
export default Controller.extend({
queryParams: ['sickParam']
});
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('parent', function() {
this.route('child-route');
});
});
export default Router;
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default Route.extend({
router: service('router'),
actions: {
loadChild() {
this.router.transitionTo('parent.child-route', {
queryParams: { sickParam: 'iller-value' },
});
}
}
});
import Route from '@ember/routing/route';
export default Route.extend({
model() {
console.log('child route model hook loaded')
}
});
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default Route.extend({
router: service('router'),
model() {
console.log('parent route model hook')
},
actions: {
loadChild() {
this.router.transitionTo('parent.child-route', {
queryParams: { sickParam: 'iller-value' },
});
}
}
});
<h1>Welcome to {{this.appName}}</h1>
<p>Check the console.logs: there is a console log in the model hook of the parent route and the child-route. Navigating to the child route via router.transitionTo() does not reload the parent route (the parent routes model() hook does not fire again)</p>
<br>
{{#link-to 'parent'}}link-to parent{{/link-to}}
{{!-- <button {{action "loadChild"}}>transitionTo() child</button><br> --}}
<br>
{{outlet}}
<br>
<br>
Child Route Loaded
{{outlet}}
Parent Route loaded<br>
{{!-- {{#link-to 'parent.child-route'}}link-to child{{/link-to}}<br> --}}
<button {{action "loadChild"}}>transitionTo() child</button><br>
{{outlet}}<br>
{
"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": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment