简体   繁体   English

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

[英]How to build this web app with Backbone.js?

I'm struggling to get my head around collections, models etc. in Backbone. 我很难在Backbone中了解收藏品,模型等。

Let's say the app consists of a Sidebar, a Timeslider and a Column chart: 假设该应用由侧边栏,时间轴和柱形图组成:

Web应用程序样机

To provide some background, I've previously implemented the columnChart class using the functional inheritance pattern: 为了提供一些背景知识,我之前使用函数继承模式实现了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;
 }

With simple binding I can call the setState method on the columnChart when for example adding a new entity from the sidebar. 使用简单绑定,我可以在columnChart上调用setState方法,例如从侧边栏添加新实体。 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. 但是随着模型的增长(状态变得越来越复杂,像year,currentSelection,chartType等变量) - 我也想在URL中反映出来 - 我想利用MVC,特别是Backbone.js。

  1. So how do I structure this in Backbone? 那么我如何在Backbone中构建它呢?
    • Should I rewrite my columnChart class (and similar classes)? 我应该重写我的columnChart类(和类似的类)吗?
    • 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. 将Sidebar,Timeslider和Column图表绑在一起的一个例子 - 使用Backbone - 将非常感激。

Thanks. 谢谢。

I would make a subclass of Backbone.Model called DataSet that represents each item in the left hand list of checkboxes. 我将创建一个名为DataSet的Backbone.Model子类,它代表左侧复选框列表中的每个项目。 This should have a boolean flag called showInGraph . 这应该有一个名为showInGraph的布尔标志。 You create a Backbone.Collection subclass to represent the full set of possible data items in the left hand section. 您创建一个Backbone.Collection子类来表示左侧部分中的完整可能数据项集。 Populate this collection with instances of your DataSet for all possibilities. 使用DataSet的实例填充此集合以实现所有可能性。 Then each item gets 2 distinct Backbone.View subclasses. 然后每个项目获得2个不同的Backbone.View子类。 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). 一个是OptionView ,它只是简单地呈现复选框以及是否选中它(根据showInGraph是否为true呈现HTML输入元素的checked属性)。 This will also need an event handler bound to the onChange attribute of the checkbox to toggle showInGraph . 这还需要一个绑定到复选框的onChange属性的事件处理程序来切换showInGraph Backbone will automatically propagate that change and re-render the views for you. Backbone将自动传播该更改并为您重新呈现视图。 Use this in the left hand div and associate it with the collection of all available data sets. 在左侧div中使用它,并将其与所有可用数据集的集合相关联。

The second view subclass is ChartView which renders the item in the chart if its showInGraph attribute it true, otherwise it just ignores it. 第二个视图子类是ChartView ,如果它的showInGraph属性为true,则呈现图表中的项目,否则它只是忽略它。

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. 然后尝试在右侧使用<li>为每个具有showInGraph数据集生成一个简单的<ul> ,但如果showInGraph为false则没有任何showInGraph Going from there to the chart is just a matter of fancier rendering in the ChartView view. 从那里到图表只是在ChartView视图中更ChartView渲染问题。

Try that out and see if you can make some progress. 试试看,看看你是否可以取得一些进展。 Post another comment if you get stuck or need clarification. 如果您遇到困难或需要澄清,请发表另一条评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM