6

I am an Ember noob and am trying to get it to work; however I am confused about the App.initialize() method.

It throws errors (it can't find the object App) if I use the following code:

App = Ember.Application.extend()
App.initialize()

However if I use the following code; it says initialize is being called twice.

App = Ember.Application.create()
App.initialize()

What is the best way to do this?

5 Answers 5

9

The Application no longer provides the initialize method. Instead you should use Application#deferReadiness and Application#advanceReadiness combined.

Example extracted from Ember's source code:

App = Em.Application.create();
App.deferReadiness();

jQuery.getJSON("/auth-token", function(token) {
  App.token = token;
  App.advanceReadiness();
});

Additionally, check the sample in jsfiddle:

window.App = Em.Application.create();

App.deferReadiness();

window.setTimeout(function() {
  Em.TEMPLATES["application"] = Em.Handlebars.compile('<h1>App</h1> this is a template');
  App.advanceReadiness();
}, 1500);
Sign up to request clarification or add additional context in comments.

3 Comments

This answer makes the returned value a global variable (App.token). How do you do this putting the value into a controller? It seems in the latest Ember there is no easy way to access a given controller from within an App.
@KevinPauli if token was in a controller, we can use needs (from another controller) or controllerFor (from a route)
But in the jQuery callback above we are not in a controller or a route, which would let me access it via 'this'. In this case we are at the global Javascript space. Every example I see of defer/advance readiness seems to follow this pattern and sticks the AJAX result directly into App as a global. I went ahead and asked this as its own question: stackoverflow.com/questions/16205615/…
3

First, You have to understand the difference between create() and extend(). Easy way to understand is extend() method just extends the class of Ember.Application but create() method creates the instance of Ember.Application(). While creating the instance it runs the constructor. There are 3 ways to create the Ember.App and run it.

1

var App= Ember.Application.extend()
App.initialize()

2.

var App = Ember.Application.create()

This initialises as soon as u create object.

3

var App= Ember.Application.extend()
App.create()

To understand Ember Objects more go through this link. Understanding Ember.Object

Comments

3

Just create your application and let Ember initialize it.

All you need to do is:

App = Ember.Application.create()

The App will not be initialized immediately. It waits, at least, for DOM readiness and for the rest of your classes to be defined (by waiting until control is returned to the browser from the currently executed JavaScript).

If you want to defer it for other reasons, do something like this:

App.deferReadiness();
$.getJSON("/boot", function() { App.advanceReadiness(); });

This will wait to boot the app until the /boot Ajax call returns.

1 Comment

How to put the returned value from the AJAX call into a controller, rather than in a global variable in App? I'm trying to avoid global variables. The jquery call is happening outside of any Ember Object, and the global "App" namespace doesn't give a way to get at an individual controller that I can see...
1

Just have a look here how to do this stuff:

http://emberjs.com/documentation/#toc_creating-a-namespace

How to bootstrap:

window.App = Ember.Application.create();

Without ever using ember.js, I would suggest that create and initialize both do initialization, that's why you get the latter error telling you it's inited twice.

And your first version is trying to extend the Application object, that is you create new functionality.

Comments

1

Ember "create" method accepts either no arguments, or an object containing values to initialize the newly instantiated object with, so you might also go like this below:

 var appConfig = {
        Token: token;
    };

 App = Ember.Application.create(appConfig);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.