Skip to content

Instantly share code, notes, and snippets.

@les2
Last active February 20, 2020 17:57
Show Gist options
  • Select an option

  • Save les2/1ed55a29915fba2e31dc4a5de95d02e6 to your computer and use it in GitHub Desktop.

Select an option

Save les2/1ed55a29915fba2e31dc4a5de95d02e6 to your computer and use it in GitHub Desktop.
link-to current-when question
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
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('dashboards', { path: '/dashboards' }, function() {
this.route('go', { path: '/go/:name' });
this.route('view', { path: '/:id' });
});
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model({ name }) {
const nameMap = {
'one': '123'
};
this.replaceWith('dashboards.view', nameMap[name]);
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model({ id }) {
return {
'id': id,
'name': `Mr. ${id}`
};
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.nav-link {
text-decoration: none;
color: black;
cursor: pointer;
}
.nav-link.active {
text-decoration: underline;
}
<h1>Welcome to {{appName}}</h1>
{{#link-to 'dashboards.view' '26' class='nav-link'}}Mr. 26{{/link-to}} |
{{#link-to 'dashboards.go' 'one' current-when='dashboards.go dashboards.view' class='nav-link'}}Dashboards{{/link-to}}
<br>
<br>
{{outlet}}
<br>
<br>
<h1>Instructions</h1>
<ol>
<li>Click the nav links for Dashboards or Mr. 26</li>
<li>Notice that when Mr. 26 is clicked, the active class is applied to the link (it will be underlined)</li>
<li>When Dashboards is clicked, the `.active` class is not applied even though `current-when='dashboards.view'` is specified.</li>
<li>Notice that the `link-to` component used for the Dashboards button uses a route -- dashboards/go -- which redirects using "replaceWith" to dashboards/view route!</li>
</ol>
<p>
</p>
<p>
Why doesn't the "Dashboards" link get the `.active` class applied even thought it uses this code:
</p>
<p>
<code><pre>
//#link-to// 'dashboards.go' 'one'
current-when='dashboards.go dashboards.view' class='nav-link'// Dashboards //link-to//
</pre>
</code>
</p>
<p>Is it specific to a particular model?</p>
My name is: {{model.name}}
<br>
<br>
My id is: {{model.id}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment