Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Last active February 9, 2021 10:15
Show Gist options
  • Select an option

  • Save izelnakri/e4145fdb8eedc4b1725a9882e309350c to your computer and use it in GitHub Desktop.

Select an option

Save izelnakri/e4145fdb8eedc4b1725a9882e309350c to your computer and use it in GitHub Desktop.
This optionally exposes the developer to some global utility functions on google chrome dev console: routes(), route(), model(), service('$serviceName') etc.
import Service from '@ember/service';
import ENV from 'frontend/config/environment';
import { getOwner } from '@ember/application';
export default class DevToolService extends Service {
owner = getOwner(this);
constructor() {
super(...arguments);
if (ENV.devTools && ENV.devTools.global) {
window.devTools = this;
const self = this;
['route', 'controller', 'model', 'service', 'router', 'routes', 'currentRouteName', 'lookup']
.forEach((property) => {
window[property] = function () {
return self[property].apply(self, arguments);
}
});
}
}
app(name = 'main') {
return this.lookup(`application:${name}`);
}
route(param) {
return this.lookup(`route:${param || this.currentRouteName()}`);
}
controller(param) {
return this.lookup(`controller:${param || this.currentRouteName()}`);
}
model(name) {
const controller = this.controller(name);
return controller && controller.get('model');
}
service(name) {
return this.lookup(`service:${name}`);
}
router() {
return this.lookup('service:router');
}
routes() {
return Object.keys(this.router()._router._routerMicrolib.recognizer.names);
}
currentRouteName() {
return this.service('router').currentRouteName;
}
lookup(name) {
return this.owner.lookup(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment