Javascript 如何在 Handlebars.js 模板上绑定数据以进行自动值更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10302289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind data on a Handlebars.js template for automatic value update?
提问by Alejandro García Iglesias
There are many JavaScript MVC frameworksright now that work with Handlebars.jsoffering data binding between the model/controller and the template. Currently I'm implementing Handlebars.js alone (with jQuery) and needing to bind data between my main code and the template so the latter gets updated automatically. Is there a native way to do it in Handlebars or with a plugin? If there isn't, is there any component of these MVC frameworks that brings this functionality and can be used stand-alone without the full-stack framework?
现在有许多JavaScript MVC 框架与Handlebars.js 一起工作,提供模型/控制器和模板之间的数据绑定。目前我正在单独实现 Handlebars.js (使用jQuery)并且需要在我的主代码和模板之间绑定数据,以便后者自动更新。在 Handlebars 中或使用插件是否有本地方法?如果没有,这些 MVC 框架中是否有任何组件带来了此功能并且可以在没有全栈框架的情况下独立使用?
采纳答案by bigspotteddog
Take a look at rivets: https://github.com/mikeric/rivets. I use backbone, handlebars, and backbone.modelbinding for two-way updates. Works great.
看看铆钉:https: //github.com/mikeric/rivets。我使用backbone、handlebars 和backbone.modelbinding 进行双向更新。效果很好。
Backbone.modelbinding (https://github.com/derickbailey/backbone.modelbinding) has been abandoned, but refers to rivets.
Backbone.modelbinding ( https://github.com/derickbailey/backbone.modelbinding) 已被放弃,但指的是铆钉。
回答by Geoffrey Hing
It sounds like you might just want to try using a Backbone viewto render your Handlebars template and listen to jQuery events on elements "inside" the view. You can use Backbone views without having to use the framework's Models, Collections or the Router, but you still have to include the full Backbone library (6.3kb, Packed and gzipped) and Underscore, which might be a problem for you if size is a consideration.
听起来您可能只想尝试使用Backbone 视图来呈现您的 Handlebars 模板并监听视图“内部”元素上的 jQuery 事件。您可以使用 Backbone 视图而不必使用框架的模型、集合或路由器,但您仍然必须包含完整的 Backbone 库(6.3kb,打包和 gzipped)和下划线,如果 size 是一个考虑。