手动构建 JSON 并手动处理 GET、POST 而不是使用适当的 RESTful API 来实现 AJAX 功能,这是否很麻烦?
我开始构建 Django 应用程序,但这可能也适用于其他框架。在调用服务器的 Backbone.js 方法(fetch()、create()、destroy() 等)中,您是否应该使用适当的 RESTful API,例如 Tastypie 或 Django-Piston 提供的 API?我发现在 Django 视图中构建 JSON 更容易、更灵活,这些视图映射到 Backbone.js 可以使用的一些 URL。话又说回来,我可能没有充分利用 Tastypie/Django-Piston 的功能。
我还没有准备好为我的应用程序创建一个成熟的 RESTful API。我只是想使用 Backbone.js 支持的一些 AJAXy 功能。
这样做的优点/缺点?
I started building a Django app, but this probably applies to other frameworks as well. In Backbone.js methods that call the server (fetch(), create(), destroy(), etc.), should you be using a proper RESTful API such as one provided by Tastypie or Django-Piston? I've founded it easier and more flexible to just construct the JSON in my Django Views, which are mapped to some URLs that Backbone.js can use. Then again, I'm probably not leveraging Tastypie/Django-Piston functionality to the fullest.
I'm not ready to make a full-fledged RESTful API for my app yet. I simply would like to use some of the AJAXy functionality that Backbone.js supports.
Pros/Cons of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,REST 不等于 JSON。如果我需要您在
text/html
中的表示,您应该能够向我提供,否则抛出 415。您当前使用的更好的解决方案是使用中间件功能Django 提供。无论您的视图回复什么,都可以使用 Django 中间件功能将响应编码为 JSON、XML 或其他内容。
Remember, REST does not equal JSON. If I require your representation in
text/html
, you should be able to provide me with that, or else throw a 415.A better solution, then you are currently using, is to use the middleware functionality that Django provides. Whatever your view replies, use Djangos middleware functionality for the response to encode into JSON, XML or whatever.
我个人更喜欢定义自己的 ajax 视图和 json 对象。使用一些已经开发的 api 可能有也可能没有多大用处。有些并不能完全满足要求,有些可能具有多余的功能(而且我不喜欢存在未使用的代码)。
编写ajax 功能也不是那么困难。内置的
serializers
/request.is_ajax
功能可以为您提供帮助。使用 django/jquery 实现 ajax 的一些示例: http://webcloud.se/ log/AJAX-in-Django-with-jQuery/ (您很可能已经看过它)
I personally prefer defining my own ajax views and json objects. Using some already apis developed may be or may not be of much use. Some don't exactly fulfill the requirements some may have features which are redundant (And I don't like a code to be present which is not being used).
Also writing ajax functionality is not that difficult either. The inbuilt
serializers
/request.is_ajax
features are there for your help.Some examples for ajax implementation with django/jquery: http://webcloud.se/log/AJAX-in-Django-with-jQuery/ (You most probably have seen it already)