如何将 JavaScript MVC 代码组织到 AMD 模块中?
我正在构建一个具有各种可视化组件的 Web 应用程序,由 Backbone.js 模型和视图组成:
“PopulationVisualization 组件”可能具有:
- 一个主模型 - 存储组件的状态
- 几个 Backbone 视图(timesliderView、legendView 等) .) - 侦听模型上的更改
所有这些组件都依赖于外部 dataManager 和 dataSource 对象,但否则它们应该是解耦的。
在给定页面上,我想实例化 PopulationVisualization 组件的实例。我还想监听该组件的主模型中的更改,以便我可以在 URL 中序列化其状态。
1) 如果我尝试采用 AMD 模块模式,会是什么样子?
2) 我会制作 PopulationVisualization 组件的一个模块还是多个模块?
3) 我是否会将模块级方法公开为 API,或者是否提供对内部模型和视图的直接操作?
谢谢。
I'm building a web application with various visualization components, made up of Backbone.js Models and Views:
The "PopulationVisualization component" might for example have:
- a main model - storing the state of the component
- several Backbone views (timesliderView, legendView etc.) - listening to changes on the model
All of these components depend on external dataManagers and dataSource objects but otherwise they are supposed to be decoupled.
On a given page, I'd like to instantiate an instance of the PopulationVisualization component. I'd also like to listen for changes in the main model of that component so that I could serialize its state in the URL.
1) What would this look like if I tried adopting the AMD module pattern?
2) Would I make one module of the PopulationVisualization component or several?
3) Would I expose module-level methods as an API or would I provide direct manipulation of the inner Models and Views?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了回答您的问题,这是我的建议,回答所有三个问题:
模块应该尽可能小,因此我将为每个视图创建一个新模块,一个用于模块,一个用于序列化逻辑。然后,我将创建一个将所有这些联系在一起的模块,以便外部代码不必处理模型、视图或序列化。
这是我第一次尝试这样的事情:
在应用程序的其他地方,您只需
require("components/populationVisualization")
并使用适当的参数调用该模块的createAndRender
方法。To answer you questions, here's my advice, answering all three:
Modules should be as small as possible, so I would create a new module for each view, one for the module, and one for the serialization logic. Then I would create one module that ties all this together so that outside code doesn't have to deal with models, views, or serialization.
Here is my first stab at something like this:
Elsewhere in the app you would only
require("components/populationVisualization")
and call that module'screateAndRender
method with the appropriate parameters.