Skip to content

Instantly share code, notes, and snippets.

@betocantu93
Last active June 22, 2020 20:03
Show Gist options
  • Select an option

  • Save betocantu93/631456f4e867bd4ef73e296524485ba5 to your computer and use it in GitHub Desktop.

Select an option

Save betocantu93/631456f4e867bd4ef73e296524485ba5 to your computer and use it in GitHub Desktop.

Revisions

  1. betocantu93 revised this gist Jun 20, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion globals.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    //instance-initializers/globals.js
    import config from "ember-get-config"; //ember-get-config addon

    export function initialize(application) {
    let { environment } = application.resolveRegistration('config:environment');
  2. betocantu93 revised this gist Jun 20, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion globals.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@
    import config from "ember-get-config"; //ember-get-config addon

    export function initialize(application) {
    if (config.environment !== "production") {
    let { environment } = application.resolveRegistration('config:environment');
    if (environment !== "production") {

    /**
    This basically exposes the application, pretty useful because
  3. betocantu93 created this gist Jun 20, 2020.
    67 changes: 67 additions & 0 deletions globals.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    //instance-initializers/globals.js
    import config from "ember-get-config"; //ember-get-config addon

    export function initialize(application) {
    if (config.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();
    }
    }