在我的应用中,我有两个重要的模型:ContentCollection 和 ContentItem。
每个 ContentCollection 都有许多 ContentCollection 和 ContentItem。您可以将 ContentCollections 视为文件夹,将 ContentItems 视为文件。因此,这种结构必须是可能的:
- ContentCollection
\- ContentCollection
\- ContentCollection
\- ContentCollection
\- Item
\- Item
\- ContentCollection
\- Item
- ContentCollection
\- Item
\- Item
- Item
- Item
我想为 ContentCollections 和 ContentItems 创建显示路由。
/home/collections/{id}
还有...
/home/items/{id}
还有...
/home/collections/{id}/collections/{id}/collections/{id}
还有...
/home/collections/{id}/collections/{id}/collections/{id}/items/{id}
等等。
有没有一种简单的方法可以让我优雅地处理主干路由中的这种嵌套?
最佳答案
你需要一个包罗万象的路线,它可以匹配任何东西......
var MyRouter = Backbone.Router.extend({
'*collection': 'showCollection'
});
然后在您的操作中进行一些解析:
Controller = {
showCollection: function (query) {
segments = query.split('/')
}
}
关于javascript - 具有无限可选参数的 Backbone 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25902792/