通过 AJAX 请求提交表单
基于表单中输入的数据从 RESTful Web 服务检索数据的骨干惯例/最佳实践是什么?
据我了解,我可以在视图中注册一个点击观察者,因此当单击表单提交按钮时,视图将从表单中检索数据。但我不清楚我是否从视图、模型或路由器执行AJAX调用。
本质上,我不清楚以下哪一个是约定:
- 让视图获取参数,执行AJAX调用,然后将返回的JSON传递给模型
- 视图获取参数并将它们传递给模型并让它执行AJAX调用并从Web服务检索数据
- 拥有视图获取参数并将它们传递给路由器并且它是否执行 AJAX 调用并相应地填充适当的模型和视图?
注意:我需要向 RESTful Web 服务提交搜索条件,该服务期望数据被 POSTed;如果我错了,请纠正我,但我似乎无法使用:fetch
或 save
。我是否应该在模型中创建一个使用 $.ajax({...}) 来发布参数并接收 JSON 数据的函数?
What is backbone's convention/best practice for retrieving data from a RESTful web service, based on data entered in a form?
From what I understand I can register a click observer in the view, so when the form submit button is clicked the view will retrieve the data from the form. But I'm unclear on whether I execute the AJAX call from the view, model, or router.
Essentially, I'm unclear as to which of the following is convention:
- Have the view get the parameters, execute the AJAX call and then pass the returned JSON to the model
- Have the view get the parameters and pass them to the model and have it execute the AJAX call and retrieve the data from the web service
- Have the view get the parameters and pass them to a router and have it execute the AJAX call and populate the appropriate model and view accordingly?
NOTE: I need to submit search criteria to a RESTful Web Service, which expects the data to be POSTed; correct me if I'm wrong, but it doesn't seem like I can use: fetch
or save
. Should I create a function within the model that uses $.ajax({...}) to post the params and receive the JSON data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将提交事件绑定到表单 id 或 div id,而不是按钮 id。
Bind the submit event to the form id or the div id, not the button id.
backbone.js 模型已经设置为执行 RESTful 请求。方法2就是你想要做的。
您在模型中唯一需要设置的是 URL:
The backbone.js models are already setup to do RESTful requests. Method 2 is what you want to do.
The only thing you have to setup in the model is a URL: