backbone.js 一个模型被多个视图改变?

发布于 2024-12-19 20:27:42 字数 523 浏览 0 评论 0原文

好吧,我昨晚才开始学习backbone.js,我不得不承认我现在有点困惑。我的场景相对简单,我有一个 id 为 #checkout_table 的 HTML 表。该表有 4 行。它始终有 4 行,并且永远不会减少或增加行数。页面上还有各种其他表单元素,我希望能够影响行的值。最终这将通过 ajax 实现,但这对于本例来说似乎并不重要。这是我想到的: http://jsfiddle.net/XYZas/

在主干文档中它说“总的想法是将界面组织成由模型支持的逻辑视图,每个视图都可以在模型更改时独立更新,而无需重新绘制页面。与其深入研究 JSON 对象,不如查找元素在 DOM 中,并更新 HTML您可以手动将视图的渲染函数绑定到模型的“更改”事件 - 现在,无论模型数据显示在 UI 中的何处,它总是立即更新。”

我这样做的方向正确吗?将 CheckoutTable 模型的实例作为全局变量传递确实是执行此操作的正确方法还是您会以不同的方式执行此操作?

预先感谢您的帮助。

Okay, I've only started learning backbone.js last night and I have to admit I am a tad baffled at the moment. My scenario is relatively simple, I have one HTML table with an id of #checkout_table. This table has 4 rows. It will always have 4 rows, it will never decrease or increase the number of rows. There are various other form elements on the page which I want to be able to effect the values of the rows. Eventually this will be via ajax, but that doesn't seem important for this example. Here is what I came up with: http://jsfiddle.net/XYZas/

In the backbone docs it says that, "The general idea is to organize your interface into logical views, backed by models, each of which can be updated independently when the model changes, without having to redraw the page. Instead of digging into a JSON object, looking up an element in the DOM, and updating the HTML by hand, you can bind your view's render function to the model's "change" event — and now everywhere that model data is displayed in the UI, it is always immediately up to date."

Am I on the right track to do this? Is passing around an instance of the CheckoutTable model as a global variable really the right way to do this or would you do it differently?

Thanks in advance for your help.

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

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

发布评论

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

评论(1

酒几许 2024-12-26 20:27:42

就像沙皇提到的:模型代表你的数据,视图使用你的模型将其渲染成 HTML 来显示。

您的应用程序应该看起来更像这样: http://jsfiddle.net/XYZas/2

另外,查看这个很棒的教程:http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

Like czar mentioned: Model represents your data, Views use your models to render it into HTML for display.

Your app should look more along the lines of this: http://jsfiddle.net/XYZas/2

Also, check out this great tutorial: http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

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