Skip to content

Instantly share code, notes, and snippets.

@magistrula
Last active August 12, 2019 23:26
Show Gist options
  • Save magistrula/7f0dfadcd277d1d1ef535beb10542ac7 to your computer and use it in GitHub Desktop.
Save magistrula/7f0dfadcd277d1d1ef535beb10542ac7 to your computer and use it in GitHub Desktop.
LinkTo Active Styling
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
import { BARS } from 'twiddle/routes/my-bar-route/my-bar-child-route';
export default Ember.Controller.extend({
bars: null,
init() {
this._super(...arguments);
this.setProperties({ bars: BARS });
}
});
import Ember from 'ember';
import { timestamps } from 'twiddle/routes/my-bar-route/my-bar-child-route';
export default Ember.Controller.extend({
timestamps: null,
afterModelBeforeModelDiff: Ember.computed(
'timestamps.afterModel',
'timestamps.beforeModel',
function() {
return (
this.get('timestamps.afterModel') -
this.get('timestamps.beforeModel')
);
}
),
init() {
this._super(...arguments);
this.set('timestamps', timestamps);
}
});
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('my-foo-route');
this.route('my-bar-route', function() {
this.route('my-bar-child-route', { path: ':bar_id' });
});
this.route('my-foo-related-route');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
});
import Ember from 'ember';
const BAR1 = { id: 'bar-1', name: 'Bar 1' };
const BAR2 = { id: 'bar-2', name: 'Bar 2' };
export const BARS = [BAR1, BAR2];
export const timestamps = {};
export default Ember.Route.extend({
beforeModel(model) {
Ember.set(timestamps, 'beforeModel', Date.now());
return new Ember.RSVP.Promise((resolve) => {
Ember.run.later(resolve, 100);
});
},
model(params) {
const id = params.bar_id;
return new Ember.RSVP.Promise((resolve) => {
const bar = BARS.findBy('id', id);
Ember.run.later(() => resolve(bar), 100);
});
},
afterModel(model) {
Ember.set(timestamps, 'afterModel', Date.now());
}
});
import Ember from 'ember';
export default Ember.Route.extend({
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
a {
text-decoration: none;
color: gray;
}
a.active {
color: dodgerblue;
}
<code>Ember link-to .active styling</code>
<p>
{{link-to "Go to Foo" "my-foo-route"
class="NavItem"
current-when="my-foo-route my-foo-related-route"
}}
</p>
<p>
{{link-to "Go to Bar" "my-bar-route" class="NavItem"}}
</p>
{{outlet}}
This is the BAR route
{{#each bars as |bar|}}
<p>
{{link-to
bar.name
"my-bar-route.my-bar-child-route"
bar
}}
(model)
<span>---</span>
{{link-to bar.name
"my-bar-route.my-bar-child-route"
bar.id
}}
(model.id)
</p>
{{/each}}
{{outlet}}
<p>
I am the route for {{model.name}}
</p>
<p>
<code>beforeModel</code> ran at {{timestamps.beforeModel}}
</p>
<p>
<code>afterModel</code> ran at {{timestamps.afterModel}}
</p>
<p>
<code>afterModel</code> - <code>beforeModel</code> {{afterModelBeforeModelDiff}}
</p>
This is the FOO route
<p>
{{link-to "Go to Foo-Related Page" "my-foo-related-route"}}
</p>
{{outlet}}
{
"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": "2.12.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment