如何使用 Backbone.js 构建这个 Web 应用程序?

发布于 2024-11-07 11:56:50 字数 912 浏览 1 评论 0原文

我正在努力了解 Backbone 中的集合、模型等。

假设该应用程序由侧边栏、时间滑块和柱形图组成:

Web 应用程序模型

为了提供一些背景知识,我之前使用函数继承模式实现了 columnChart 类:

namespace.columnChart = function() {

    var chart = {};

    var width = 500;
    var height = 500;
    var data = [];

    chart.setState = function(state){
        data = state.data;
        updateVis();
    }

    function updateVis(){
        ... render chart based on state ...
    }

    return chart;
 }

通过简单的绑定,我可以在 for 时调用 columnChart 上的 setState 方法从侧边栏添加新实体的示例。但随着模型的增长(并且状态变得更加复杂,变量如年份、currentSelection、chartType 等)——我也希望在 URL 中反映出来——我想利用 MVC,特别是 Backbone.js。

  1. 那么我如何在 Backbone 中构建它呢?
    • 我应该重写我的columnChart 类(和类似的类)吗?
    • 有没有一种简单的方法可以检测状态中发生的变化并仅使​​用这些参数设置新状态?

如果有一个使用 Backbone 将侧边栏、时间滑块和柱形图结合在一起的示例,我们将不胜感激。

谢谢。

I'm struggling to get my head around collections, models etc. in Backbone.

Let's say the app consists of a Sidebar, a Timeslider and a Column chart:

Web app mockup

To provide some background, I've previously implemented the columnChart class using the functional inheritance pattern:

namespace.columnChart = function() {

    var chart = {};

    var width = 500;
    var height = 500;
    var data = [];

    chart.setState = function(state){
        data = state.data;
        updateVis();
    }

    function updateVis(){
        ... render chart based on state ...
    }

    return chart;
 }

With simple binding I can call the setState method on the columnChart when for example adding a new entity from the sidebar. But as the model grows (and the state gets more complex with variables like year, currentSelection, chartType etc.) - which I'd also like reflected in the URL - I'd like to make use of MVC and specifically Backbone.js.

  1. So how do I structure this in Backbone?
    • Should I rewrite my columnChart class (and similar classes)?
    • Is there an easy way to detect what has changed in the state and only set the new state using these params?

An example of tying the Sidebar, Timeslider and Column chart together - using Backbone - would be much appreciated.

Thanks.

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

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

发布评论

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

评论(1

吃不饱 2024-11-14 11:56:50

我将创建一个名为 DataSet 的 Backbone.Model 子类,它代表左侧复选框列表中的每个项目。这应该有一个名为 showInGraph 的布尔标志。您创建一个 Backbone.Collection 子类来表示左侧部分中可能的数据项的完整集合。使用数据集的实例填充此集合以实现所有可能性。然后每个项目都有 2 个不同的 Backbone.View 子类。一种是 OptionView,它简单地呈现复选框以及是否选中它(根据 showInGraph 是否为 true 呈现 HTML 输入元素的 checked 属性)。这还需要一个绑定到复选框的 onChange 属性的事件处理程序来切换 showInGraph。 Backbone 将自动传播该更改并为您重新渲染视图。在左侧 div 中使用它,并将其与所有可用数据集的集合相关联。

第二个视图子类是 ChartView,如果其 showInGraph 属性为 true,则它会呈现图表中的项目,否则它会忽略它。

一步一步来吧。首先只需制作左侧的复选框列表。按照主干文档,这应该很简单。然后尝试在右侧创建一个简单的

    ,并为每个 showInGraph true 的数据集添加一个

  • ,但如果 showInGraph 为 false,则什么也不做。从那里到图表只需在 ChartView 视图中进行更精美的渲染即可。

尝试一下,看看是否能取得一些进展。如果您遇到困难或需要澄清,请发表另一条评论。

I would make a subclass of Backbone.Model called DataSet that represents each item in the left hand list of checkboxes. This should have a boolean flag called showInGraph. You create a Backbone.Collection subclass to represent the full set of possible data items in the left hand section. Populate this collection with instances of your DataSet for all possibilities. Then each item gets 2 distinct Backbone.View subclasses. One is OptionView which simply renders the checkbox and whether or not it is checked (rendering the HTML input element's checked attribute based on whether showInGraph is true). This will also need an event handler bound to the onChange attribute of the checkbox to toggle showInGraph. Backbone will automatically propagate that change and re-render the views for you. Use this in the left hand div and associate it with the collection of all available data sets.

The second view subclass is ChartView which renders the item in the chart if its showInGraph attribute it true, otherwise it just ignores it.

Take it step by step. First just make the left hand list of checkboxes. That should be straightforward following the backbone docs. Then try to make just a simple <ul> on the right with an <li> for each data set that has showInGraph true but nothing if showInGraph is false. Going from there to the chart is just a matter of fancier rendering in the ChartView view.

Try that out and see if you can make some progress. Post another comment if you get stuck or need clarification.

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