Backbone.Js 端点/Rails 集成和 URL 参数
在我的 Rails 应用程序中,我有视频。视频链接到 YouTube,因此有时无法使用。
当我在 Rails 中查询视频端点时,有时会传入“request_type=”url 属性来指示我想要获取链接已损坏的视频。
为此,我在backbone.js 中简单地覆盖了 url 方法并使用 jQuerys params 命令添加 url 参数。我现在遇到的问题是更新现有项目。它尝试使用如下 URL:
PUT /medias?request_type=broken_medias/2
显然 /2 需要出现在 url 的 /medias 部分之后。 request_type 参数是否保留在 URL 上并不重要,尽管我更喜欢它,以防我需要处理损坏媒体的特定更新行为。
关于如何最好地处理这个问题有什么想法吗?损坏介质的独特路径?检查我是否在集合中执行 PUT 请求?在执行更新请求之前重写 url 方法?
In my Rails application I have videos. Videos are linked to Youtube so sometimes they become unavailable.
When I query my videos endpoint in Rails I will sometimes pass in a "request_type=" url attribute to indicate I want to get videos that have broken links.
To do this, in backbone.js I've simply overwritten the url method and used jQuerys params command to add the url parameters. The problem I have now is with updating existing items. It's trying to use a URL like the following:
PUT /medias?request_type=broken_medias/2
Clearly the /2 needs to appear after the /medias portion the the url. It doesn't matter if the request_type params stays on the URL although I would prefer it incase I need to handle specific update behavior for broken medias.
Any thoughts on how best to handle this? Unique routes for broken medias? Check if I'm doing an PUT request in the collection? Override the url method just before doing an update request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了完全控制 Backbone 的后端请求,您需要重写 Collections 或 Models sync 方法,该方法实际构建并发出 ajax 请求。
请参阅此处的示例:
Backbone.js 更新集合中的模型
更新:我对 Juliens 的评论进行了更多思考,并记住集合或模型中的 url 属性也可以是一个可以构建 url 的函数。
In order to fully control the backend requests with Backbone you need to override your Collections or Models sync method which actually builds and makes the ajax request.
See an example here:
Backbone.js updating of models in a collection
UPDATE: I gave it some more thought upon Juliens comment and remembered that url attribute in Collections or models could also be a function where you can build your url.