如何将 JavaScript MVC 代码组织到 AMD 模块中?

发布于 2024-12-17 18:16:29 字数 499 浏览 6 评论 0原文

我正在构建一个具有各种可视化组件的 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 技术交流群。

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

发布评论

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

评论(1

时光匆匆的小流年 2024-12-24 18:16:29

为了回答您的问题,这是我的建议,回答所有三个问题:

模块应该尽可能小,因此我将为每个视图创建一个新模块,一个用于模块,一个用于序列化逻辑。然后,我将创建一个将所有这些联系在一起的模块,以便外部代码不必处理模型、视图或序列化。


这是我第一次尝试这样的事情:

// components/populationVisualization/Model.js
define(function (require, exports, module) {
  return Backbone.Model.extend({ /* ... */});
});

// components/populationVisualization/views/Timeslider.js
define(function (require, exports, module) {
  return Backbone.View.extend({ /* ... */});
});

// components/populationVisualization/views/Legend.js
define(function (require, exports, module) {
  return Backbone.View.extend({ /* ... */});
});

// components/populationVisualization/serializer.js
define(function (require, exports, module) {
  exports.setupSerialization = function (model) {
    // ...
  };
});

// components/populationVisualization.js
define(function (require, exports, module) {
  var Model = require("./populationVisualization/Model");
  var TimesliderView = require("./populationVisualization/views/Timeslider");
  var LegendView = require("./populationVisualization/views/Legend");
  var serializer = require("./populationVisualization/serializer");

  exports.createAndRender = function (modelParams, $el) {
    var model = new Model(modelParams);
    serializer.setupSerialization(model);

    var timesliderView = new TimesliderView({ model: model });
    var legendView = new LegendView({ model: model });

    $el.append(timesliderView.el);
    $el.append(legendView.el);
  };
});

在应用程序的其他地方,您只需 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:

// components/populationVisualization/Model.js
define(function (require, exports, module) {
  return Backbone.Model.extend({ /* ... */});
});

// components/populationVisualization/views/Timeslider.js
define(function (require, exports, module) {
  return Backbone.View.extend({ /* ... */});
});

// components/populationVisualization/views/Legend.js
define(function (require, exports, module) {
  return Backbone.View.extend({ /* ... */});
});

// components/populationVisualization/serializer.js
define(function (require, exports, module) {
  exports.setupSerialization = function (model) {
    // ...
  };
});

// components/populationVisualization.js
define(function (require, exports, module) {
  var Model = require("./populationVisualization/Model");
  var TimesliderView = require("./populationVisualization/views/Timeslider");
  var LegendView = require("./populationVisualization/views/Legend");
  var serializer = require("./populationVisualization/serializer");

  exports.createAndRender = function (modelParams, $el) {
    var model = new Model(modelParams);
    serializer.setupSerialization(model);

    var timesliderView = new TimesliderView({ model: model });
    var legendView = new LegendView({ model: model });

    $el.append(timesliderView.el);
    $el.append(legendView.el);
  };
});

Elsewhere in the app you would only require("components/populationVisualization") and call that module's createAndRender method with the appropriate parameters.

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