Why ember-cli uses extend instead of create?

為{幸葍}努か 提交于 2019-12-09 11:15:51

问题


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. !? )


回答1:


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.



来源:https://stackoverflow.com/questions/24914677/why-ember-cli-uses-extend-instead-of-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!