Created
September 14, 2016 18:50
-
-
Save rohmann/1df36a63f2c3905bc52ae373e771a129 to your computer and use it in GitHub Desktop.
Add class to Ember Application (after 2.7.0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// instance-initializers/app-view.js | |
// With an instance initializer, we can register a component for Ember to use at the top level. | |
// Not ideal, but it works in the meantime until routable components drop. | |
import Ember from 'ember'; | |
const AppView = Ember.Component.extend({ | |
classNames: ['my-app'], | |
}); | |
export function initialize( appInstance ) { | |
appInstance.register('view:toplevel', AppView); | |
} | |
export default { | |
name: 'app-view', | |
initialize | |
}; |
If it's only needed to set a class based on the route (that was my use-case) this may do as an alternative:
import Ember from 'ember';
const NAME_SUFFIX = '-route';
export default Ember.Controller.extend({
_updateRouteClass: function() {
let routeName = this.get('currentRouteName');
if (routeName) {
let name = routeName.dasherize().replace(/\./g, '-') + NAME_SUFFIX;
let bodyEl = document.querySelector('body');
Array.from(bodyEl.classList).forEach((klass) => {
if (klass.endsWith(NAME_SUFFIX)) {
bodyEl.classList.remove(klass);
}
});
bodyEl.classList.add(name);
}
}.observes('currentRouteName').on('init'),
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After some back and forth it seems that this implementation no longer works on 2.10.0-beta.2