# 在插件中为类添加 mixin？

**URL:** https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905
**Category:** Development
**Created:** [2018年三月26日 20:30 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905 "2018-03-26T20:30:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![shoshber](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/shoshber/32/95393_2.png) [@shoshber](https://meta.discourse.org/u/shoshber)
#### Post date: [2018年三月26日 20:30 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905/1 "2018-03-26T20:30:17Z")

</div>

I need to modify a javascript controller (`create-account`). If I were writing it from scratch, I’d add a mixin called something like (`FooValidation`), the same way the mixins `PasswordValidation` and `UsernameValidation` are applied ([discourse/app/assets/javascripts/discourse/controllers/create-account.js.es6 at f03b6bd8c925163b41b9e33b39484d102dbaee44 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/f03b6bd8c925163b41b9e33b39484d102dbaee44/app/assets/javascripts/discourse/controllers/create-account.js.es6#L13)). I can’t figure out how, though.

I _could_ just add the relevant properties/methods to the create account controller using `PluginApi.modifyClass`, but that doesn’t feel right, especially seeing as I’ll want to reuse the mixin.

What’s the best way to achieve this?

---

<div class="post-metadata">

### Author: ![Osama](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/osama/32/98013_2.png) [@Osama](https://meta.discourse.org/u/Osama)
#### Post date: [2018年三月26日 21:57 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905/2 "2018-03-26T21:57:23Z")

</div>

I believe the `modifyClass` method accepts a mixin as an argument, so I’d try something like this:

```javascript
api.modifyClass("controller:create-account", FooValidation);

```

---

<div class="post-metadata">

### Author: ![shoshber](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/shoshber/32/95393_2.png) [@shoshber](https://meta.discourse.org/u/shoshber)
#### Post date: [2018年三月27日 20:11 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905/3 "2018-03-27T20:11:45Z")

</div>

Ah you’re right, `modifyClass` calls Ember’s `reopen`, which can take mixins as an argument. I couldn’t get it to work at first but I got it! Thanks!

---

<div class="post-metadata">

### Author: ![Dev\_Work](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dev_work/32/124200_2.png) [@Dev\_Work](https://meta.discourse.org/u/Dev_Work)
#### Post date: [2019年十二月17日 20:32 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905/4 "2019-12-17T20:32:36Z")

</div>

你能分享一些示例代码吗？

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2019年十二月17日 22:03 UTC](https://meta.discourse.org/t/adding-a-mixin-to-a-class-in-a-plugin/83905/5 "2019-12-17T22:03:14Z")

</div>

我在主题列表预览中实现了这一功能：[https://github.com/paviliondev/discourse-topic-previews/blob/master/assets/javascripts/discourse/initializers/preview-edits.js.es6](https://github.com/paviliondev/discourse-topic-previews/blob/master/assets/javascripts/discourse/initializers/preview-edits.js.es6)

具体示例：`api.modifyClass('component:topic-list', Settings);`

我这样做是为了将原本会被重复且近乎复制粘贴的共享代码提取出来。
