Ember-cli App.get / App.set throw undefined

I am converting a globals based Ember app to an es6 based app that uses ember-cli. In my application, I need to know the current route quite often. In the globals version I was doing this.

Global bindings pattern

var MyApp = Ember.Application.create({
    currentRoute : ''
});

MyApp.Route = Ember.Route.extend({
    actions : {
        didTransition : function () {
            MyApp.set('currentRoute', this);
        }
    }
});

      

Then I could make MyApp.get('currentRoute')

out of my session or standalone controllers when determining how / where to go when certain events happened.

When using ember-cli, I import my app so I can reference it with the controllers I need.

import MyApp from "../app";

      

But it turns out that MyApp.currentRoute

, MyApp.get

and MyApp.set

everything is undefined .

Part of me thinks it is a bug in ember-cli that the application instance no longer has getters and setters associated. Part of me understands that it is not a good practice to store things in an application instance.

I could work around this problem by converting all instances to MyApp.get

and MyApp.set

to Ember.get(MyApp, ...)

and Ember.set(MyApp, ...)

respectively, but I thought I'd ask this first as it looks like a problem with Ember -Cli or whatever there is a better recommended way to achieve what I need need to.

+3


source to share


1 answer


If you look at app.js

(what you are importing) this is not your application instance, it is exporting a subclass Ember.Application

. That's why get

et al are not available on it.

var App = Ember.Application.extend({   <----- NOT create
    modulePrefix: config.modulePrefix,
    podModulePrefix: config.podModulePrefix,
    Resolver: Resolver
});

export default App;

      



To get the actual application instance from your route use:

this.container.lookup('application:main')

      

+6


source







All Articles