Backbone.js 与非 RE​​STful 应用程序? (Backbone.js 适合我当前的项目吗?)

发布于 2024-11-08 18:31:20 字数 1138 浏览 3 评论 0原文

我试图弄清楚 Backbone.js 是否是适合我当前项目的正确框架:可视化应用程序。

我有很多问题:

1) 状态/路由?

由于这不是典型的 RESTful 应用程序,而是具有各种图表类型和这些图表设置的可视化应用程序,我如何维护状态网址? 假设我的areaChart模型有许多这样的默认值:

AreaChartModel = Backbone.Model.extend({
    defaults: {
        selectedCountries: [],
        year: 1970,
        stacked: false
    },
    initialize: function(){
        [...]
    }
});

在更新模型时,我想序列化其中一些属性,以便我可以为特定状态添加书签:chartApp.html#!year=1970&stacked=false反之亦然

,当以这种状态初始化应用程序时,如何“deparam”url 状态并设置模型?我可以使用 Backbone 的内部路由吗?

2) 控制器和耦合?

看来 Backbone 的视图模型耦合相当紧密? 这真的是我应该将例如我的areaChartView绑定到模型的方式吗?

AreaChartView = Backbone.View.extend({
    initialize: function(){
        areaChartModel.bind("change:year", this.render);
    }
});

这通常不是控制器的角色吗?

3) 继续:模型与控制器?

考虑到这种情况:在此处输入图像描述

更改“侧边栏”中应该触发一系列功能:
1)“应加载当前选择的新数据”
2) “根据此数据,可视化视图中的比例应更新”
3)“应该渲染可视化视图”

我应该在哪里放置这些函数以及如何在模型中创建一个在状态稳定时触发的事件? (即当所有函数都被调用并且是时候设置视图状态时?)

I'm trying to figure out if Backbone.js is the right framework for my current project: a visualization app.

I have a number of questions:

1) State / Routing?

As this is not your typical RESTful app, but rather a visualization application with various chart types and settings for these charts, how do i maintain state in the URL?
Let's say my areaChart model has a number of defaults like this:

AreaChartModel = Backbone.Model.extend({
    defaults: {
        selectedCountries: [],
        year: 1970,
        stacked: false
    },
    initialize: function(){
        [...]
    }
});

On an update to the model I'd like to serialize some of these attributes so that I can bookmark the specific state: chartApp.html#!year=1970&stacked=false etc.

And vice-versa, when initing the app with this state, how do I "deparam" the url state and set the model? Can I use Backbone's intrinsic routing?

2) Controller and coupling?

It seems as Backbone has a pretty tight view-model coupling?
Is this really how I should bind for example my areaChartView to the model?

AreaChartView = Backbone.View.extend({
    initialize: function(){
        areaChartModel.bind("change:year", this.render);
    }
});

Isn't this normally the role of the controller?

3) Continuation: Model vs. Controller?

Given this scenario:enter image description here

A change in the "Sidebar" should trigger a sequence of functions:
1) "New data for the current selection should be loaded"
2) "Based on this data, the scales in the Visualization view should be updated"
3) "The visualization view should be rendered"

Where should I place these functions and how can I create an event in the model that I trigger when the state is stable? (i.e. when all the functions have been invoked and it's time to set the view states?)

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

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

发布评论

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

评论(3

1) 我会尽可能使用 Backbone.js 原生路由,使用“:params”和“*splats”

例如我将默认设置为线条栏,并且您不能使用 URL 预设它,而是更改为堆叠图只需单击一个按钮即可。

我可能会不再使用 ?和&在我的网址中。我可能稍后会回到这一点,因为它很有趣。

2)你的例子很好,你只需要记住 Backbone.js MVC 术语与传统 MVC 无关。

主干视图本质上是传统 MVC 中的控制器。
主干控制器只是框架内的一种路由方式。
Backbone.js 使用的模板引擎是传统的 MVC 视图。

3) 还在写

1) I would use Backbone.js native routing as much as possible using “:params” and “*splats” , read more. You could fit all your queries into the Backbone.js routing but I would personally sacrifice certain things in favor of intuitive UI buttons

e.g. I would have the default as a line bar and you can't preset this with the URL but to change to a stacked graph would be a simple click of a button.

I would probably stray from ever using ? and & in my URL's. I might come back to this point later as it is interesting.

2) Your example is fine and you just need to remember Backbone.js MVC terminology doesn't correlate to traditional MVC.

Backbone Views are essentially the Controller in traditional MVC.
Backbone Controllers are simply a way of routing inside a framework.
The templating engine you use with Backbone.js is the traditional MVC view.

3) Still writing

带刺的爱情 2024-11-15 18:31:20

关于问题 #3,我将为滑块创建一个 Model 和一个 View

然后,我会将模型上 change 事件的触发与视图中更新图形视图的某个函数相关联(例如更改比例)。比如:

var Slider = Backbone.Model.extend({})

var SliderView = Backbone.View.extend({
    initialize: function() {
        this.model.bind('change', this.render);
    }

    render: function() {
        // load data, change scales, etc.
    }
});

var slider = new Slider();
var slider_view = new SliderView({ model: slider });

也许一个好主意是将绑定放在父视图中,然后将其分派到子视图,协调它们的工作。

Regarding question #3, I would create a Model and a View for the slider.

Then I would associate the triggering of the change event on the model to some function in the view that updates the graph's view (like changing the scales). Something like:

var Slider = Backbone.Model.extend({})

var SliderView = Backbone.View.extend({
    initialize: function() {
        this.model.bind('change', this.render);
    }

    render: function() {
        // load data, change scales, etc.
    }
});

var slider = new Slider();
var slider_view = new SliderView({ model: slider });

Maybe a good idea would be to put the bindings in a parent view, that would then dispatch to sub-views, coordinating their work.

沙沙粒小 2024-11-15 18:31:20

请坐下来考虑一下维持整个状态是否是一个好主意?采用基于 url 的状态管理的主要动机是能够支持基于浏览器的导航按钮并能够为页面添加书签。在可视化应用程序中,您的数据可能每时每刻都在变化。这不是您想要保留在 app-url 中的内容。当用户为您的应用添加书签并在三天后返回时,您真的希望看到三天前数据的可视化吗?对于您的场景,假设我没有误解您的要求,我建议将数据状态保留在模型本身中。

另外,关于视图与模型数据的同步,是的,您可以自己编写所有绑定逻辑。在这种情况下,您的 View 类将负责在第一次渲染时设置绑定。在后续调用 render 时(可以响应模型中的任何更改事件而调用 render),将刷新可视化所在的 DOM/canvas。

也许您应该期待一个数据同步插件,它可以为您处理大部分样板文件。 此页面列出了一些可用的数据绑定扩展。 Orchestrator 是我一直在研究的另一个解决方案,在这方面可能会有所帮助。

Do sit down for a while and consider if maintaining the entire state is at all a good idea ? The key motivations for having url-based state management is being able to support browser based navigation buttons and being able to bookmark a page. In a visualization app, your data would probably change every moment. This is not something you want to persist in your app-url. Do you really want that when a user bookmarks your app and comes back to it three days later - he sees the visualization for three days old data ? For your scenario, assuming I have not misunderstood your requirements, I would recommend to keep the data state in your model itself.

Also regarding synchronization of views with model data, Yes you can code all the binding logic on your own. In that case your View class will take care of setting up the bindings on the first render. And upon subsequent calls to render, which can be invoked in response to any change event in the model, will refresh the DOM/canvas where the visualization is present.

Probably you should be look forward to a plugin for data-synchronization that takes care of much of boilerplate for you. This page lists some of the data-binding extensions available. Orchestrator is another solution that I have been working on, which might be helpful in this regard.

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