在 JavaScript Web 应用程序中实现类似 SproutCore 的绑定

发布于 2024-09-18 15:18:11 字数 355 浏览 7 评论 0原文

我已经完成了 SproutCore 教程,并且了解了该框架必须提供的功能。这看起来很神奇,尽管我不确定我对在幕后生成 HTML 和 CSS 的框架有何感受。我发现 SproutCore 最吸引人的一点是它的绑定——减少保持所有内容同步所需的粘合代码量只能是一件好事。

我有兴趣了解 SproutCore 的绑定是如何实现的。我希望能够利用绑定,而不必使用 SproutCore 等框架。

如何编写绑定数据及其表示所需的 JavaScript 代码,以便让视图立即反映数据的更改而无需任何粘合代码?

I've done the SproutCore tutorial and have an idea of what the framework has to offer. It seems amazing, although I'm not sure how I feel about a framework which generates the HTML and CSS behind the scenes. The thing about SproutCore which I find most appealing is its bindings – reducing the amount of glue code required to keep everything in sync can only be a good thing.

I'm interested in learning about how SproutCore's bindings are implemented. I would love to be able to take advantage of bindings without necessarily using a framework such as SproutCore.

How would one go about writing the JavaScript code necessary to bind data and their representations, to have views instantly reflect changes to the data without any glue code?

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

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

发布评论

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

评论(1

赤濁 2024-09-25 15:18:11

使用 SproutCore 使用 SC.Observable 提供的“get”和“set”方法 mixin 支持键值观察。当您创建绑定时,它基本上会设置一个观察者,当对其观察的值调用“set”方法时,该观察者会触发,然后传播更改。当绑定绑定到页面上的视图时,绑定会调用必要的视图代码来对 DOM 进行更改。您可以查看 SC.Observable 的 API 文档和源代码和 SC.Binding 位于 http://docs.sproutcore.com

由于 SproutCore 分为多个“框架”,因此您可以只采用该核心内容所在的框架(称为“运行时”),并在您的项目中使用它,而无需使用您不需要的所有数据存储和视图层。您也可以尝试自己重现此功能,但我想知道您最终是否必须重现他们创建的大部分内容。

With SproutCore using the 'get' and 'set' methods provided by the SC.Observable mixin enables key-value observing. When you create a binding it basically sets up an observer that fires when the 'set' method is called on the value it's observing and then propagates the change. When a binding is tied to a view on your page, the binding invokes the necessary view code to make the changes to the DOM. You could take a look at API docs and source for SC.Observable and SC.Binding over at http://docs.sproutcore.com.

Since SproutCore is divided into multiple "frameworks", you could just take the framework where this core stuff lives, called 'runtime', and use it in your project without all of the data store and view layers that you don't want. You could also try to reproduce this functionality yourself, but I wonder if you'd end up having to reproduce much of what they've created.

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