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?

AbdulFattah Popoola
  • 949
  • 2
  • 13
  • 22

5 Answers5

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);
MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
  • 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. – Kevin Pauli Apr 25 '13 at 02:26
  • @KevinPauli if `token` was in a controller, we can use [`needs`](http://emberjs.com/guides/controllers/dependencies-between-controllers/) (from another controller) or [`controllerFor`](http://emberjs.com/guides/routing/setting-up-a-controller/) (from a route) – MilkyWayJoe Apr 25 '13 at 13:59
  • 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: http://stackoverflow.com/questions/16205615/in-ember-how-to-defer-readiness-and-put-ajax-result-into-a-controller – Kevin Pauli Apr 27 '13 at 16:38
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

thecodejack
  • 12,689
  • 10
  • 44
  • 59
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.

Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
  • 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... – Kevin Pauli Apr 25 '13 at 02:32
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.

akohout
  • 1,802
  • 3
  • 23
  • 42
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);