主干网和 Rails 嵌套路由
我在 Rails 中定义了以下路由:
resources :accounts do
resources :transactions
end
这会产生类似以下的 url:
/accounts/123/transactions/1
有没有一种简单的方法可以将其映射到设置的主干模型?
I have the following routes defined in rails:
resources :accounts do
resources :transactions
end
This results in urls like:
/accounts/123/transactions/1
Is there an easy way to map this to a backbone model set up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,骨干很容易通过在模型中嵌套集合来支持这一点,如下所示:
这完全达到了我想要的效果。
您可以在这里阅读更多相关信息:http://documentcloud.github.com/backbone/#FAQ-nested
Turns out backbone quite easily supports this by nesting a collection in a model as follows:
This achieves exactly what I wanted.
You can read more about it here: http://documentcloud.github.com/backbone/#FAQ-nested
这可能不是一个简单的方法,但我认为最好的方法是使用 url 并将其设置为这样的函数:
或者也许在咖啡脚本中(因为它是backbone+rails):
哦,你可以这样做更多这样的(当然嵌套越深越好):
据我所知,主干中现在有 url 实用程序,这对我来说还不够痛苦,所以我会在其他库中搜索一个:)
It might not be an easy way but I think the best way is to use url and set it to a function like this:
Or maybe in coffeescript (as it is backbone+rails):
Oh and you could do it more like this(surely with deeper nesting it's better):
AFAIK there is now url utility in backbone, and it's not much enough pain for me so that I would search for one in some other library :)
Backbone 不直接支持创建嵌套 url。您必须使用函数来动态计算嵌套对象的结果 url。例如:
更多信息: http://documentcloud.github.com/backbone/#FAQ-nested
Backbone does not directly support the creating of nested urls. You must use a function to dynamically calculate the resulting url of your nested object. For example:
More information: http://documentcloud.github.com/backbone/#FAQ-nested
只需定义您的模型或(如果您使用一个)集合的 url,如下所示:
或动态:
以这种方式配置的那些模型或集合的所有模型现在将相应地生成其后端 url。
详细信息:
型号:
http://documentcloud.github.com/backbone/#Model-url
集合:
http://documentcloud.github.com/backbone/#Collection-url
Just define the url of your model or (if you use one) your collection like so:
or dynamically:
Those models or all models of a collection that is configured this way will now generate it's backend urls accordingly.
Detailed info:
Model:
http://documentcloud.github.com/backbone/#Model-url
Collection:
http://documentcloud.github.com/backbone/#Collection-url