如何将应用程序设计为模块化/支持插件

发布于 2024-09-01 01:39:04 字数 626 浏览 5 评论 0原文

我目前正在重构我的网络播放器,以便我们能够更轻松地在其他网络广播电台上运行它。这些播放器之间的大部分设置都非常相似,但是,有些播放器需要不同的 UI 插件/其他插件。

目前在网络播放器中我在 init() 中做了类似的事情:

_this.ui = new UI();

_this.ui.playlist = 新播放列表();

_this.ui.channelDropdown = new ChannelDropdown();

_this.ui.timecode = ne 时间码();

等等

这工作正常,但阻止我在运行时需要这些对象。我想做的是能够根据电台的需要添加这些内容。基本上我的问题是,我需要在这里添加某种“addPlugin()”功能吗?如果我这样做,我是否需要在尝试使用该插件之前不断地从我的 WebPlayer 对象中检查该插件是否存在?就像...

if (_hasPlugin('playlist')) this.plugins.playlist.add(track);

如果其中一些内容可能不清楚,我深表歉意……我真的很想弄清楚这一切。我觉得我更接近了,但我仍然被困住了。任何有关我应该如何进行此操作的建议将不胜感激。

预先感谢,

I'm currently in the process of refactoring my webplayer so that we'll be more easily able to run it on our other internet radio stations. Much of the setup between these players will be very similar, however, some will need to have different UI plugins / other plugins.

Currently in the webplayer I do something like this in it's init():

_this.ui = new UI();

_this.ui.playlist = new Playlist();

_this.ui.channelDropdown = new ChannelDropdown();

_this.ui.timecode = ne Timecode();

etc etc

This works fine but that blocks me into requiring those objects at run time. What I'd like to do is be able to add those based on the stations needs. Basically my question is, do I need to add some kind of "addPlugin()" functionality here? And if I do that, do I need to constantly check from my WebPlayer object if that plugin exists before it attempts to use it? Like...

if (_hasPlugin('playlist')) this.plugins.playlist.add(track);

I apologize if some of this might not be clear... really trying to get my head wrapped around all of this. I feel I'm closer but I'm still stuck. Any advice on how I should proceed with this would be greatly appreciated.

Thanks in advance,

Lee

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

城歌 2024-09-08 01:39:04

您需要在应用程序中公开某些您希望其他人能够使用的功能。 IE:在 UI 和播放器等主要组件上公开 get{}set{} 访问器。您公开的功能越多,就有越多的插件能够修改您功能的重要部分。

假设您有一个 UI.header,并且 header 包含定义 header 如何显示 UI 的属性。因此,您将 header.backgroundImage 公开为公共字符串,将 header.Text 公开为公共字符串,将 header.height 公开为公共 int。现在设计插件的人可以更改您的标头值并使其看起来并说出他们想要的内容。

这完全取决于您希望人们能够根据您所公开的内容来改变您的球员的程度。

You would need to expose certain functionality within your application that you want others to be able to work off of. IE: making public get{} set{} accessors on major components like your UI and your player. The more functionality you expose the more plugins will be able to modify important parts of your functionality.

So let's say you have a UI.header, and header contains properties that define how the header displays the UI. So you expose header.backgroundImage as a public string, header.Text as a public string, and header.height as a public int. Now someone designing a plugin can change your header values and make it look and say what they want.

It's all about how much you want people to be able to alter your player based on what you expose.

桃扇骨 2024-09-08 01:39:04

您可以为插件定义 JavaScript 类,将它们作为依赖项加载到网络播放器,并在运行时根据需要在 RequireJS AMD。

//in your webplayer.js
define(['./ui','./playlist'],function(ui, playlist){
     var webPlayer = function(stationID){
     //initializing work
    }
    return webPlayer; 
});

在运行时根据需要加载 webplayer.js 文件并实例化网络播放器。

看看 BoilerplateJS,它是 JavaScript 产品开发的参考架构。诸如事件处理、创建自包含组件、处理它们之间的交互、由谁决定何时创建/显示/隐藏 UI 组件等问题都会得到处理,以便快速启动开发。

You can define JavaScript classes for your plugins, load them as dependencies to the webplayer and instantiate them at runtime as needed with the help of RequireJS AMD.

//in your webplayer.js
define(['./ui','./playlist'],function(ui, playlist){
     var webPlayer = function(stationID){
     //initializing work
    }
    return webPlayer; 
});

At runtime load the webplayer.js file when needed and instantiate the web player.

Have a look at BoilerplateJS which is a reference architecture for JavaScript product development. Concerns such as event handling, creating self contained components, handling interaction between them, who decides when to create/show/hide your UI components are taken care of in order to quickstart development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文