这是我的主干应用程序的要求
- 显示用户创建的文件夹列表
- 单击文件夹时显示文件夹的内容
下面是我的实现方式。
AppRouter = Backbone.Router.extend({
routes: {
'': 'home',
'get/:name/:id': 'contents'
},
home: function() {
// show list of folders
},
contents: function(name, id) {
// show contents of clicked folder
}
});
这种方法给我带来了问题,因为当我单击一个文件夹时,该路由会保存在浏览器历史记录中并且具有结构“domain.com#get/folder/1”。如果我碰巧将此 url 粘贴到浏览器的地址栏中,则不会呈现文件夹列表,因为它与路由不匹配。
在路由器的 initialize
函数中显示文件夹列表是否是一个明智的策略?可以创建一个页面 View 来检查文件夹是否已经显示?
最佳答案
如果我理解正确,文件夹列表应该永久显示。您的应用程序有两个大 View ,文件夹列表和内容。文件夹列表必须一直显示。
var AppRouter = Backbone.Router.extend({
// it's better to use REST names than custom ones
routes: {
'': 'index',
'get/:id/:name': 'show'
},
initialize: function(options) {
this.folders = new Folders(options.folders); // folders is your Backbone.Collection
// always show the FoldersView, something like
// new FoldersView({collection: this.folders, el: $('the folders container')})
},
index: function() {
// probably clear the contents area, something like
// $("#content").html('')
}
show: function(id, name) {
var folder = this.folders.get(id);
// create a view for this folder
// and render it in the content area, something like
// view = new FolderView(model: folder)
// $("#content").html(view.render().el)
}
})
关于views - Backbone.js:导航路线时何时呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7887152/