【发布时间】:2012-01-01 06:57:03
【问题描述】:
我正在构建一个单页网络应用程序(因为我希望在跨页面/状态移动时具有灵活性和速度)但我正在努力处理路由/url ...
在传统范例中,我会使用以下网址:
example.com/tools/population-tool/#currentYear=1950
example.com/tools/income-tool/#country=usa
example.com/nice-story/
example.com/nice-chapter/nice-story/
现在我想用一个为相应路由加载模板和控制器的路由器(例如使用 Backbone)替换它。
我正在考虑使用一个 pages 对象来存储必要的页面信息:
pages : {
tools : {
template : "#tools",
breadcrumb : ["Home","Tools"]
}
nice-story : {
template : "#nice-story",
breadcrumb : ["Home","Stories","Nice Story"]
}
}
有了路由器,我现在想加载正确的内容和页面状态,给定一个像这样的 url:
example.com/#!/tools/population-tool/?currentYear=1950
如果不使用Hashbang,也可以这样:
example.com/tools/population-tool/?currentYear=1950
您将如何组织此路由以使 url 方案有意义,同时仍然灵活并允许重定向和新的查询字符串参数?
【问题讨论】:
-
我找到了这个主干插件:github.com/jhudson8/backbone-query-parameters。您只需将其插入即可开始使用。
标签: javascript backbone.js router hashbang