I have a route with this code to set a body class. I will use this code on a couple of routes but not all. I was wondering if it’s possible to share this code between routes?
TIA
-Emmanuel
import Ember from 'ember';
export default Ember.Route.extend({
activate () {
Ember.$('body').addClass('loading-authentification');
},
deactivate () {
Ember.$('body').removeClass('loading-authentification');
}
});
Mixins are largely an anti-pattern now. Make sure you understand exactly how Ember’s mixin works and how the order that you mix these mixins actually results in different behavior.
On the larger perspective, it’s always good to know the inner working of Ember’s object model. Namely how all these works: .reopen, .reopenClass, .extend, Ember.mixin (lower case mixin).
What would you use the place of the mixin here? A service is the first thing that pops into my mind but that seems overkill for this. You could use an exported class or a simple function either but they don’t feel right compared to the mixin which is built into the framework.
Fair point but a component comes with a lot more cognitive overhead than a mixin (not to mention number of files). Most Mixins should only describe a simple self contained behaviour or action but don’t necessarily have lifecycles themselves or work with the DOM. In that case does a mixin or classical inheritance not make more sense than a component?
Rather than using imperative logic to set a body style, you can also add that to the template and use data mapping from a property to set the class. In particular if this is for navigation the link-to can be used to set the style if the url matches the target of the link-to which allows easy management of the navigation links common in web ui and bootstrap in particular.