简体   繁体   English

Ember JS如何设置应用程序

[英]Ember JS how to set up Application

I am an Ember noob and am trying to get it to work; 我是Ember noob,我正努力让它上班; however I am confused about the App.initialize() method. 但我对App.initialize()方法感到困惑。

It throws errors (it can't find the object App) if I use the following code: 如果我使用以下代码,它会抛出错误(它无法找到对象App):

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? 做这个的最好方式是什么?

The Application no longer provides the initialize method. Application不再提供initialize方法。 Instead you should use Application#deferReadiness and Application#advanceReadiness combined. 相反,您应该使用Application#deferReadinessApplication#advanceReadiness组合。

Example extracted from Ember's source code: 从Ember的源代码中提取的示例:

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

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

Additionally, check the sample in jsfiddle : 另外,检查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);

First, You have to understand the difference between create() and extend(). 首先,您必须了解create()和extend()之间的区别。 Easy way to understand is extend() method just extends the class of Ember.Application but create() method creates the instance of Ember.Application(). 简单的理解方法是extend()方法只是扩展了Ember.Application的类,但create()方法创建了Ember.Application()的实例。 While creating the instance it runs the constructor. 在创建实例时,它运行构造函数。 There are 3 ways to create the Ember.App and run it. 有三种方法可以创建Ember.App并运行它。

1 1

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

2. 2。

var App = Ember.Application.create()

This initialises as soon as u create object. 一旦你创建对象,这就会初始化。

3 3

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

To understand Ember Objects more go through this link. 要了解Ember Objects,请访问此链接。 Understanding Ember.Object 了解Ember.Object

Just create your application and let Ember initialize it. 只需创建您的应用程序,让Ember初始化它。

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). 它至少等待DOM准备就绪,并且等待其他类的定义(等待控制从当前执行的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. 这将等待启动应用程序,直到/boot Ajax调用返回。

Just have a look here how to do this stuff: 看看这里怎么做:

http://emberjs.com/documentation/#toc_creating-a-namespace 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. 在没有使用ember.js的情况下,我建议创建和初始化都进行初始化,这就是为什么你得到后者的错误告诉你它被引入了两次。

And your first version is trying to extend the Application object, that is you create new functionality. 您的第一个版本正在尝试扩展Application对象,即您创建新功能。

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: Ember“create”方法接受无参数或包含值的对象来初始化新实例化的对象,因此您可能也会如下所示:

 var appConfig = {
        Token: token;
    };

 App = Ember.Application.create(appConfig);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM