Skip to content

Instantly share code, notes, and snippets.

@betocantu93
Last active June 22, 2020 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betocantu93/631456f4e867bd4ef73e296524485ba5 to your computer and use it in GitHub Desktop.
Save betocantu93/631456f4e867bd4ef73e296524485ba5 to your computer and use it in GitHub Desktop.
Adds some runtime globals for making Ember.js developing a breeze
//instance-initializers/globals.js
export function initialize(application) {
let { environment } = application.resolveRegistration('config:environment');
if (environment !== "production") {
/**
This basically exposes the application, pretty useful because
you can use stuff like this from the console.
App.lookup('route:some-route').actions.doSomething();
*/
window.App = application;
/*
This will gives us access to the store easily, to make fast queries or checks!
Fast and easy:
var s = App.store.peekRecord('some-model', 1);
App.store.createRecord('some-model', {name: 'Alberto'})
*/
window.App.store = application.__container__.lookup("service:store");
//shortcuts for every emberjs base type lookup
//App.controller('auth.some')
let objects = [
'service',
'controller',
'route',
'model'
];
objects.forEach(type => {
window.App[type] = function(name) {
return application.lookup(`${type}:${name}`)
}
})
/*
Use a class for ergonimics with getters, grab the current model, route or controller based
on the current active route!
*/
class CurrentContext {
get model() {
return application.lookup(
`controller:${application.lookup("service:router").currentRouteName}`
).model;
}
get controller() {
return application.lookup(
`controller:${application.lookup("service:router").currentRouteName}`
);
}
get route() {
return application.lookup(
`route:${application.lookup("service:router").currentRouteName}`
);
}
}
window.App.ctx = new CurrentContext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment