Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-CLI - How to access application classes

Previously when I developed ember application I used App as my global object and every class was stored in this big object.

Like this

window.App.MyModel = Em.Object.extend({});

And in the browser console, I was able to do

App.MyModel.create();

So it was really easy for me to access MyModel class.

Now I started experiments with Ember-CLI, I don't have much experience with this kind of tools. I followed the documentations and I created my model Service like this.

var Service = Ember.Object.extend({});
export default Service

But now, how to access Service class from browser console? Only way I found was:

App.__container__.resolve('model:service')

But I don't like it much. Is there better way? Btw, can you please explain how export works? Or is there some source (documentation, article) where I can study it?

Thanks a lot for response.

like image 905
Robin Bortlík Avatar asked Apr 16 '14 16:04

Robin Bortlík


People also ask

How do I open Ember command line?

Open http://localhost:4200 in your browser of choice. You should see an Ember welcome page and not much else. If you are having trouble getting this running, other Ember developers would be happy to help! Visit The Ember Community Page to join chat groups or forums.

What is an Ember CLI build?

Ember CLI, Ember's command line interface, provides a standard project structure, a set of development tools, and an addon system. This allows Ember developers to focus on building apps rather than building the support structures that make them run.

What is Ember CLI Babel?

Ember CLI by default uses Babel. js to allow you to use tomorrow's JavaScript, today. It will ensure that you can use the newest features in the language and know that they will be transformed to JavaScript that can run in every browser you support.

What is Ember application?

Ember. js is a productive, battle-tested JavaScript framework for building modern web applications. It includes everything you need to build rich UIs that work on any device.


1 Answers

If you're aiming to have something available in most places throughout your application you'll typically want to register it on the container.

There are multiple ways to access the container, and you're correct in that the one you found was less than ideal. The running joke is "any time you directly access __container__ we need to add more underscores."

To directly control how things are registered on the container and injected into the container you typically want to use an initializer. Using initializers in ember-cli is super-easy, they're executed for you automatically.

Checking out the documentation you can see that you get access to the application's container as an argument which allows you to manipulate it in a safe manner.

Once you have access to the container you can use register and inject to make content easily available in particular locations. This was introduced here. One note, accessing things inside of the container from outside the context of your app (browser console) will require the usage of App.__container__ and that is the expected use pattern.

And the export you're running into is an ES6 module system construct, it's not Ember-specific. Playing with the ES6 module transpiler can give you a good sense of what goes in and what comes out in "we can do this today" type of JavaScript.

like image 62
nathanhammond Avatar answered Sep 27 '22 21:09

nathanhammond