javascript - 具有无限可选参数的 Backbone 路由

标签 javascript html backbone.js backbone-routing

在我的应用中,我有两个重要的模型: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/

相关文章:

html - 当文章内容越过折叠时容器上的左填充

javascript - 如何使用 Javascript 表单元素填充 html 表格?

javascript - 在获取/保存后更新 Backbone 子模型

ruby-on-rails - Backbone .js分页

javascript - 选择文本并替换为循环

javascript - 错误: read ECONNRESET when connected to a mysql server with Node.js

javascript - 如何在 Samsung Note 2 上使用 toDataURL 创建 JPG 数据字符串

javascript - 在具有绝对位置的 div 中实现平滑过渡

javascript - 直接用 Backbone 调用 url

javascript - NVD3 调度事件不适用于 HistoricalBarChart