shoshber
(Shoshana Berleant)
2018 年 3 月 26 日午後 8:30
1
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 (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?
Osama
2018 年 3 月 26 日午後 9:57
2
I believe the modifyClass method accepts a mixin as an argument, so I’d try something like this:
api.modifyClass("controller:create-account", FooValidation);
「いいね!」 4
shoshber
(Shoshana Berleant)
2018 年 3 月 27 日午後 8:11
3
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!
「いいね!」 2
Dev_Work
(Aleksandr)
2019 年 12 月 17 日午後 8:32
4
can you share some sample code?
merefield
(Robert)
2019 年 12 月 17 日午後 10:03
5
I do it in Topic List Previews: https://github.com/paviliondev/discourse-topic-previews/blob/master/assets/javascripts/discourse/initializers/preview-edits.js.es6
Specific example: api.modifyClass('component:topic-list', Settings);
I did this to break out shared code that would otherwise have been repeated and almost a cut and paste.
「いいね!」 3