Skip to content

Instantly share code, notes, and snippets.

@alexmiddeleer
Last active July 19, 2018 20:35
Show Gist options
  • Save alexmiddeleer/202dbe7f3ec6352850d5863ced4db196 to your computer and use it in GitHub Desktop.
Save alexmiddeleer/202dbe7f3ec6352850d5863ced4db196 to your computer and use it in GitHub Desktop.
Example of using router service
import Ember from 'ember';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
router: service(),
// In a real app with a more complex nav panel, I would probably
// put this logic in its own service so I don't overcomplicate
// the application controller.
showNavButton: computed('router.currentRouteName', function() {
return(this.get('router.currentRouteName') === 'index');
})
});
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('home')
});
export default Router;
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<div class="nav-bar">
<p>{{#link-to "index"}}index{{/link-to}}</p>
<p>{{#link-to "home"}}home{{/link-to}}</p>
<p>current route name is: {{router.currentRouteName}}</p>
{{#if showNavButton}}
<button>Click Me</button> This button is only visible if on index route
{{/if}}
</div>
{{outlet}}
<br>
<br>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment