与js进行双向数据绑定的最有用的技术 [英] Most useful techniques for two way data binding with js
问题描述
示例场景:
$ b $ ol <$>
jQuery可以轻松地完成AJAX页面加载请求,调用服务,返回一个对象,并使用jQuery模板插件将该对象绑定到表单。当需要提交表单时,可以使用jQuery Form插件通过AJAX发送到服务,并返回JSON对象,并将其绑定到jQuery模板插件容器(表单)。
- jQuery AJAX API
http: //api.jquery.com/jQuery.ajax/ - jQuery模板插件
http://plugins.jquery.com/project/jquerytemplate
http://api.jquery.com/category/plugins/templates/ - jQuery Form Plugin >
http://jquery.malsup.com/form/ - Declerative Bindings >
- 自动刷新UI
- Get Data From Service as JSON/XML
- Display/Bind on UI
- Capture User Input
- Recreate request from the UI controls/html
- Post Data To Service
- Get Response and Rebind
- jQuery AJAX API
http://api.jquery.com/jQuery.ajax/ - jQuery Templates Plugin
http://plugins.jquery.com/project/jquerytemplate
http://api.jquery.com/category/plugins/templates/ - jQuery Form Plugin
http://jquery.malsup.com/form/ - Declerative Bindings
- Automatic UI Refresh
$ b
另外,你可以结合使用jQuery的Form插件来发送表单,而KnockOut JavaScript库,这是关于你想要做的绑定的类型。
请参阅 http://knockoutjs.com/ 上的关于JS库的详细信息
上面列出的前两个功能包括:
查看这个非常小的介绍实例 http://knockoutjs.com/examples/helloWorld.html
更新:
由于这个答案最近得到了赞赏,新的孩子angularJS,这是一个更大的框架,可以做很多事情,但做双向数据绑定是有史以来最容易的事情。
官方网址: http://angularjs.org
示例: http://docs.ang ularjs.org/guide/forms
With abundance of web services and client side templating features of jQuery and likes, creating mashups or sites consuming a multitude of web services and posting data back to these services is becoming exceedingly popular. For a page of decent size with this kind of architecture, say a dashboard. What are the useful techniques of maintaining this client side state. In other words whats are some of the ways to do two way databinding? Sample scenario:
In jQuery you can easily do AJAX request of page load that calls the service, returns an object, and bind that object to the form using jQuery Templates plugin. When the form needs to be submitted, you can use jQuery Form plugin to send to the service via AJAX and return the JSON object, bind it to the jQuery Templates plugin container (the form).
also, You may combine the use of jQuery "Form" plugin to send the form, and the KnockOut JavaScript library, which is all about the kind of binding you want to do.
See knockout JS library details on http://knockoutjs.com/
The first two features as listed there are:
See this live example for very small introduction http://knockoutjs.com/examples/helloWorld.html
Update:
Since this answer got a recent upvote, it's also important to mention the relatively-new kid in the town, angularJS, it's a bit of a bigger framework that can do so many things, but doing two-way data-binding is the easiest thing ever.
Official URL: http://angularjs.org
Example: http://docs.angularjs.org/guide/forms
这篇关于与js进行双向数据绑定的最有用的技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!