为什么ember-cli使用扩展而不是创建? [英] Why ember-cli uses extend instead of create?

查看:109
本文介绍了为什么ember-cli使用扩展而不是创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在一个新的ember-cli App中,你先写:

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

为什么?



(在第二个情况下,我无法从控制器中读取全局属性(App.test)!!)

解决方案

Ember.Object 有很大关系。



.extend()创建一个扩展旧的类,其中通过传入的哈希值中定义的类级属性。



.create()创建一个新的类实例,在传入的哈希中定义的对象级属性。



这就是为什么在第二种情况下无法读取该属性的原因。如果你想这样做,你需要做:

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

在一个简单的Ember应用程序中,您可以创建一个 App 对象,因为你要直接使用它。



在一个ember-cli生成的Ember应用程序中,你不会创建一个 App 对象,您只需定义其类(使用 .extend())。这是因为你不直接使用它,因为ember-cli想要该类,所以它可以在它内部实例化之前自己做一些事情。


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

解决方案

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.

这篇关于为什么ember-cli使用扩展而不是创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆