当从ember.js1.11.3升级到1.12以删除弃用警告时,我如何将这些初始化程序转换为实例初始化程序

How do I convert these initializers to instance initializers when upgrading from ember.js 1.11.3 to 1.12 to remove the deprecation warnings?

本文关键字:初始化 程序 实例 程序转换 警告 3升 js1 ember 删除 当从      更新时间:2023-09-26

我正在将我们的ember/ember-cli版本从1.11.3升级到1.12,为不久后达到1.13和2.0做准备。

然而,我在准确解读wrt初始化器和实例初始化器的变化时遇到了一些困难。。。我看过这些文档,读了很多帖子,但仍然不清楚这些是如何工作的。我特别困惑application.register和container.register之间的区别,以及我应该在什么时候使用application、container或instance。

从ember.js1.11.3到ember.js1.12,从ember cli 0.2.2到ember cli 0.2.7

我的初始化程序相当简单,有人能帮我转换它们吗?关于ember在初始化/实例初始化过程中的确切工作方式的简要概述或链接也会有所帮助。

以下是1.11.3中我现有的初始化程序:

initializers/auth.js

import Ember from 'ember';
import Session from 'simple-auth/session';
import CustomAuthenticator from 'soxhub-client/authenticators/soxhub';
import CustomAuthorizer from 'soxhub-client/authenticators/soxhub';
export default {
  name: 'authentication',
  before: 'simple-auth',
  initialize: function(container, application) {
    Session.reopen({
        user: function() {
            var userId = this.get('user_id');
            if (!Ember.isEmpty(userId)) {
              return container.lookup('store:application').find('user', userId);
            }
        }.property('accountId')
    });
    console.log('registering authenticator on container');
    container.register('authenticator:soxhub', CustomAuthenticator);
    container.register('authorizer:soxhub', CustomAuthorizer);
  }
};

initializers/inject-session-into-services.js

    import Ember from 'ember';
    export default {
        name: 'inject-session-into-service',
        after: 'simple-auth',
        initialize: function(container, application) {
            console.log('ran initializer for sesion');
            application.inject('service:pusher', 'session', 'simple-auth-session:main');
        }
    };

initializers/inject-store-in-to-component.js

export default {
  name: "injectStoreIntoComponent",
  after: "store",
  initialize: function(container, application) {
    console.log('injecting store onto component');
    // container.typeInjection('component', 'store', 'store:main');
    application.inject('component', 'store', 'store:application');
  }
};

我不熟悉初始化程序,但我看到了这篇关于初始化程序的Ember NYC Meetup Talk,我认为它可能会对你有所帮助。整个演讲非常有趣,但我链接到初始化程序寻址的部分

看看你的初始化程序,我会说这个片段

Session.reopen({
    user: function() {
        var userId = this.get('user_id');
        if (!Ember.isEmpty(userId)) {
          return container.lookup('store:application').find('user', userId);
        }
    }.property('accountId')
});

可能被移动到应用程序路由上的beforeModel挂钩。做这样的事?

beforeModel() {
  let user = this.get('store').find('user', accountId);
  this.get('session').set('user', user);
}

从那个演讲中,我认为你不应该使用"*.lookup",但你仍然可以使用inject/register。

此外,来自最新版本(2.0+)的ember指南。初始化程序不再获得container,而是拥有注入/注册方法的应用程序。

希望对有所帮助