简体   繁体   English

Ember Octane 如何转换自定义 mixin

[英]Ember Octane How to convert custom mixins

This question is related to: Does Ember Octane Route class support using mixins?这个问题与: Ember Octane Route class support using mixins 有关吗? Mixins are officially deprecated in Ember Octane.在 Ember Octane 中正式弃用了 Mixin。

Question:问题:

What is the best option to replace Ember mixins with and how do I implement it?用什么替换 Ember mixins 的最佳选择是什么,我如何实现它?

Context:语境:

I have custom mixins that expand functionality offered by ember-simple-auth (~v1.8.2), which was not available at the time the methods were created (see below).我有自定义 mixin 来扩展 ember-simple-auth (~v1.8.2) 提供的功能,在创建方法时它不可用(见下文)。 I am currently using ember-simple-auth 3.0.0 https://github.com/simplabs/ember-simple-auth .我目前正在使用 ember-simple-auth 3.0.0 https://github.com/simplabs/ember-simple-auth In the documentation on github, they appear to be using their own mixins on Ember Octane as you can see:在 github 的文档中,他们似乎在 Ember Octane 上使用了自己的 mixin,如您所见:

// my-engine/addon/routes/index.js
import Route from '@ember/routing/route';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default class IndexRoute extends Route.extend(AuthenticatedRouteMixin) {
  triggerAuthentication() {
    this.transitionToExternal('login');
  }
}

It appears that I am not the only one having this issue as Simple Auth doesn't know what route they want to go down either: https://github.com/simplabs/ember-simple-auth/issues/2185看来我不是唯一一个遇到此问题的人,因为 Simple Auth 不知道他们想要 go 向下的路线: https://github.com/simplabs/ember-simple-auth/issues/2185

Options:选项:

  • Pure native classes, sharing functionality via class inheritance.纯原生类,通过 class inheritance 共享功能。
  • Utility functions which can be imported and used in multiple classes.可以在多个类中导入和使用的实用函数。
  • Services which can be injected into multiple classes, sharing functionality and state between them.可以注入多个类的服务,在它们之间共享功能和 state。

I have the following authentication mixins:我有以下身份验证混合:

  1. Application Route: This handles whether the user is logged in and whether the user has two-factor authentication (2FA) enabled, and if it is enabled, it verifies that the user has been authenticated through 2FA. Application Route:这个处理用户是否登录以及用户是否启用了双因素身份验证(2FA),如果启用,则验证用户是否已通过 2FA 进行身份验证。 This is because simple auth did not offer 2FA at the time of development of the mixin(s)这是因为 simple auth 在开发 mixin(s) 时没有提供 2FA
  2. Authenticated Route: Makes sure the user is logged in. If they are not logged in then the user is routed to the login page. Authenticated Route:确保用户已登录。如果他们未登录,则将用户路由到登录页面。 Once they login, they are routed back to the page that they initially tried to go to一旦他们登录,他们就会被路由回他们最初尝试的页面 go 到
  3. UnAuthenticated Route: Prevents logged in users from going to certain routes that logged in users should not go to UnAuthenticated Route:防止登录用户转到某些登录用户不应该 go 到的路由

Firstly I want to make very clear that mixins are not "officially deprecated" in Ember, and to my knowledge there's not even an active RFC about it.首先,我想非常清楚地表明,Ember 中的 mixin 并没有被“正式弃用”,据我所知,甚至没有关于它的有效 RFC As the Upgrade Guides explain, Glimmer components do not support mixins due to not extending EmberObject, but the pre-existing framework classes (Route, Controller, etc) necessarily have to or it would be a breaking change.正如升级指南所解释的,Glimmer 组件不支持 mixin,因为它没有扩展 EmberObject,但预先存在的框架类(Route、Controller 等)必须这样做,否则这将是一个重大变化。

There is no best option to replace mixins as it depends on the usage of the API.没有替代 mixins 的最佳选择,因为它取决于 API 的使用情况。 If you are asking how to replace ember-simple-auth mixins, my answer is that you can't until the addon itself provides alternative APIs.如果您要问如何替换 ember-simple-auth mixins,我的回答是在插件本身提供替代 API 之前您不能。 Mixins and the example code you posted will continue working for the foreseeable future. Mixins 和您发布的示例代码将在可预见的未来继续工作。
You can see an example of using class inheritance to share functionality in this PR .您可以在此 PR 中查看使用 class inheritance 共享功能的示例。

when i migrated to ember Octane i tried to replace ember-simple-auth mixins, i found that it would take me lot of time than rewriting my own authentication service, my example in this twiddle, i am using cookies, Auth service if you are not using cookies, you could customize your adapter to include a token in the headers当我迁移到 ember Octane 时,我尝试替换 ember-simple-auth mixins,我发现这比重写我自己的身份验证服务要花很多时间,我在这个 twiddle 中的示例,我正在使用 cookies,如果你是Auth 服务不使用 cookies,您可以自定义适配器以在标头中包含令牌

I circled back with ESA on Git, and the same issue I cited in my OP has been closed with a new issue that has subsequently been merged:我在 Git 上用 ESA 盘旋,我在我的 OP 中引用的同一问题已被一个随后合并的新问题关闭:

https://github.com/simplabs/ember-simple-auth/pull/2198 https://github.com/simplabs/ember-simple-auth/pull/2198

ESA has now updated their library to get rid of route mixins. ESA 现在更新了他们的库以摆脱路由混合。

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

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