Skip to content

Instantly share code, notes, and snippets.

@betocantu93

betocantu93/globals.js

Last active Jun 22, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.