Search code examples
javascriptember.jsember-cli

Why ember-cli uses extend instead of create?


In a new ember App you write first:

var App = Ember.Application.create({
    test: 'foo',
    ...
});

In a new ember-cli App you write first:

var App = Ember.Application.extend({
    test: 'foo',
    ...
});

Why?

( In the second case, I can't read a global property (App.test) from a controller. !? )


Solution

  • This question actually has a lot to do with Ember.Object.

    .extend() creates a new class that extends the old one, with class-level properties defined in the hash that is passed in.

    .create() create a new instance of the class, with object-level properties defined in the hash that is passed in.

    This is why you cannot read the property in the second case. If you want to do that, you will need to do:

    var App = Ember.Application.extend({});
    App.test = 'foo';
    

    In a plain Ember app, you can create an instance of the App object, because you are going to use it directly.

    In an ember-cli generated Ember app, you do not create an instance of the App object, you merely define its class (using .extend()). This is because you do not use it directly, as ember-cli wants the class, so that it may do its own things to it, before it internally instantiates it.