0

I'm trying to make a base in-repo addon that provides functionality through namespace like platform/services/whatever.

In addition to that I want if another addon isEnabled() that extends or reopens that same service (in some way) I want it to all be merged together without having to call the second addon's service by name like platform-second/services/whatever

I just want a nice clean abstraction. The reason I'm doing it this way is because based on an ENV variable, I need to build / inject different stuff into index.html and different app.imports.

and I want it to be completely separated into separate little in-repo addons to keep it clean.

I want the app to not need to know the platform but just be able to utilize methods of the general platform addon.

So for example platform.services.whatever.myMethod() might be a noOp by default, but if the second addon extends that and implements that, it'll fire that version instead automatically.

Wonder if I'm making any sense at all LOL

If you have any tips or advice on how one might implement this setup, please let me know.

I currently have the in-repo addons built but ideally I'd have the base platform addon be the one that actually has the app folder contents and the other addons that "extend" or "override" the methods / properties of that addon's objects would just be isEnabled() based on that ENV variable.

I should add that I can't simply do merge tree and override the original file because I need the base functionality of those files.

So if I extend one method of the base platform.services.whatever.myMethod() then I still need the rest of that services methods and properties as well.

4

1 回答 1

0

所以,我很难说得太具体,因为你的问题对细节有点模糊,但不用担心。我会抛出一些想法,希望我们可以一起得出一些有用的结论:)

如果您在运行时需要不同的对象,诀窍是根据配置在依赖注入容器中注册不同版本的服务(这在基于 DI 的编程中很常见)。所以在你的插件中有你的基础服务,甚至是一些具体的实现,用某些方法的不同实现来扩展基础。然后,在您的应用程序中,您有一个初始化程序,它查看不同的 ENV 变量并确定要注入哪些服务。你总是在同一个 key 下注入service:some-service。因此,baseClass = Ember.Object.extend()并且impl1 = baseClass.extend({methodToOverride: ...}允许您保留除您覆盖的方法之外的所有基类方法。所以你已经成功地避免了调用者必须知道是否有父母或孩子正在处理service.someMethod

现在,如果它只是在构建时,那么你在 node.js 中的土地(require()module.exports。使用 pojos 或 es6 你有很多方法来实现继承(原型继承,用于分层合并 pojos 的 $.extend,es6 extends)......带上你的选择。但仅从编程设计模式的角度来看,您的插件似乎需要公开一个工厂,该工厂抽象出符合某个接口的对象的创建。因此,导出一些具有 create 方法的模块,该方法接受一个选项对象并返回正确的 impl。

于 2017-11-18T20:57:24.787 回答