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

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

问题描述

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

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

这些功能是为什么用例设计的?如何使用以及何时使用?

我真的很想知道!

推荐答案

默认情况下,Ember 在使用大多数约定启动应用程序时会进行依赖注入,例如,如果您使用 ember-data,则使用 store 类被注入到应用程序中的每个 routecontroller 中,因此您稍后可以通过简单地执行 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.

例如,这里是一个代码摘录,其中注册了默认的 store(取自 )

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');
  }
  ...
});

换句话说,registerinject 是注册依赖项并自己注入它们的方法.

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: ''
});

此代码会将每个控制器实例的 session 属性设置为 App.Session 的单例实例,因此您可以在任何控制器中执行 this.get('session') 并获得对它的引用,因为它被定义为单例,所以它总是相同的 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.

使用register,您可以注册控制器、模型、视图或任何任意对象类型.inject,另一方面,可以注入给定类的所有实例.例如 inject('model', 'session', 'session:current') 也将使用 session:current 注入 session 属性实例到所有模型中.要注入 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').

尽管 registerinject 非常强大,但您应该明智地使用它们,并且只有在您确实知道没有其他方法可以实现目标的情况下,我猜缺乏文档表明气馁.

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,这就是我们通过在应用程序的每个控制器中注册和注入 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天全站免登陆