Hello, first post and Ember n00b here. Thank you in advance for your input.
My objective is to visually indicate in the UI which section my user is in. I’m able to solve this, but it seems clunky so I’m guessing there’s a better way.
First, I understand that {{#link-to ...}
automatically renders class="active"
when the rendered link matches the current route, but my problem is that {{#link-to}}
always renders an <a>
tag while my CSS is looking for <li class="active"><a>...</li></a>
and adjusting CSS is not an option here.
Here’s how I managed to solve this, but it’s a little ugly. First, here is my routes file:
Router.map(function() {
this.resource("standardShell", { path: "/" }, function() {
this.resource("reminders", { path: "/reminders" }, function() {
this.route("dashboard");
}
}
}
I am visiting the URL http://myApp/reminders/dashboard
.
I want my reminders
template to indicate that it’s on the dashboard
page. My reminders.hbs
template is as follows:
<li {{bind-attr class="isActiveSectionIsDashboard:active"}}>{{#link-to "reminders.dashboard"}}Dashboard{{/link-to}}</li>
The isActiveSectionIsDashboard
is calling my RemindersController
which is defined as follows:
var RemindersController = Ember.Controller.extend({
activeSection: "",
// Set the active navigation section. Called by child view.
setActiveSection: function( activeSection ) {
this.set('activeSection', activeSection);
},
// Boolean functions for Handlebars to know which section is active
isActiveSectionIsDashboard: function() {
return this.get('activeSection') === "dashboard";
}.property('activeSection'),\
});
Now here’s where it gets messy. The RemindersController
needs its activeSection
property set from the child controller/view/route. So, here’s my DashboardController
:
var DashboardController = Ember.Controller.extend({
needs: "reminders",
sectionName: "dashboard"
});
and finally here’s my DashboardView
:
var DashboardView = Ember.View.extend({
didInsertElement: function() {
var controller = this.get('controller.controllers.reminders');
controller.setActiveSection( this.get('controller').sectionName );
}
});
This just seems like an incredible amount of work just for the child page to set a property at the parent page level. Am I missing something, or is this the best way to handle this?
Thanks again for your input!
Josh