shoshber
(Shoshana Berleant)
3월 26, 2018, 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 (discourse/app/assets/javascripts/discourse/controllers/create-account.js.es6 at f03b6bd8c925163b41b9e33b39484d102dbaee44 · discourse/discourse · GitHub ). 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
3월 26, 2018, 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);
shoshber
(Shoshana Berleant)
3월 27, 2018, 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!
Topic List Previews에서 이렇게 하고 있습니다: https://github.com/paviliondev/discourse-topic-previews/blob/master/assets/javascripts/discourse/initializers/preview-edits.js.es6
구체적인 예시: api.modifyClass('component:topic-list', Settings);
이렇게 함으로써, 그렇지 않았다면 반복되고 거의 복사-붙여넣기 수준이 되었을 공유 코드를 분리해냈습니다.