如何和何时使用Ember.Application注册和注入方法? [英] How and when to use Ember.Application register and inject methods?

查看:113
本文介绍了如何和何时使用Ember.Application注册和注入方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何使用Ember.Application 注册& 注入方法

I'm trying to understand how to use Ember.Application register & inject methods

这些函数的用例是什么?
他们如何使用和什么时候?

我真的很想知道!

推荐答案

默认情况下,Ember在使用主要约定启动应用程序时执行依赖注入,例如,如果您使用ember-data,那么 store 类注入到您的应用程序中的每个路由控制器中,以便稍后获取通过在任何路由或控制器内简单地执行 this.get('store')的引用。

Ember by default does dependency injection when it boots your application using mostly conventions, for example if you use ember-data then an instance of the store class is injected in every route and controller in your application, so you can later get a reference by simply doing this.get('store') inside any route or controller.

是一个代码提取,其中默认的存储获取注册(取自

For example here is a code extract where the default store get's registered (taken from the source)

Ember.onLoad('Ember.Application', function(Application) {
  Application.initializer({
    name: "store",

    initialize: function(container, application) {
      application.register('store:main', application.Store);
      ...
    }

    container.lookup('store:main');
  }
});

然后注入(

Application.initializer({
  name: "injectStore",

  initialize: function(container, application) {
    application.inject('controller', 'store', 'store:main');
    application.inject('route', 'store', 'store:main');
    application.inject('dataAdapter', 'store', 'store:main');
  }
  ...
});

换句话说,注册 inject 是注册依赖关系的方法,并自己注入。

In other words register and inject are methods to register dependencies and inject them yourself.

我们假设你有一个 Session 对象,您在应用程序启动后的服务器请求中填充对象,并且您希望在每个控制器中引用该对象,可以执行以下操作:

Let's assume you have a Session object which you populate after a server request on application start, and which you want to have a reference to in every controller, you could do something like this:

var App = Ember.Application.create({
  ready: function(){
    this.register('session:current', App.Session, {singleton: true});
    this.inject('controller', 'session', 'session:current');
  }
});

App.Session = Ember.Object.extend({
  sessionHash: ''
});

此代码将设置会话每个控制器实例到一个单例实例 App.Session ,所以你可以在任何控制器中执行 this.get('session')并获得引用,并且由于它被定义为单例,它将始终是相同的会话对象。

This code would set the session property of every controller instance to a singleton instance of App.Session, so you could in any controller do this.get('session') and get a reference to it, and since it's defined as a singleton it would be always the same session object.

使用注册可以注册控制器,模型,视图或任意对象类型。 inject ,另一方面,可以注入给定类的所有实例。例如 inject('model','session','session:current')也会注入 session 会话:当前实例到所有模型。要注入会话对象,让我们说到 IndexView 你可以做 inject('view :index','session','session:current')

With register you can register controllers, models, views, or any arbitrary object type. inject, in the other hand, can inject onto all instances of a given class. For example inject('model', 'session', 'session:current') would also inject the session property with the session:current instance into all models. To inject the session object, let's say onto the IndexView you could do inject('view:index', 'session', 'session:current').

尽管注册 inject 非常强大,你应该明智地使用它们,只有在你真的知道没有其他方法来实现你的目标的情况下,我猜缺乏文档一个沮丧的指标。

Although register and inject are very powerful you should use them wisely and only in the case you really know there is no other way to achieve your goal, I guess the lack of documentation is an indicator for discouragement.

由于它主要是必须的提供一个解释的工作示例,它会发生: http://jsbin.com/usaluc/6/edit 。请注意,在示例中,我们可以通过使用 {{controller.session.sessionHash}}引用当前控制器的会话对象来简单地访问所提到的 sessionHash / code>在我们所在的每个路由中,这是我们通过在应用程序的每个控制器中注册和注入 App.Session 对象所做的工作的优点

Since It's mostly a must to provide a working example with an explanation, there it goes: http://jsbin.com/usaluc/6/edit. Notice how in the example we can simply access the mentioned sessionHash by referring to the current controller's session object with {{controller.session.sessionHash}} in every route we are in, this is the merit of what we have done by registering and injecting the App.Session object in every controller in the application.

希望有帮助。

这篇关于如何和何时使用Ember.Application注册和注入方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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