2011-10-25 55 views
3

在这里呈现的观点是我的主干应用Backbone.js的:当导航路线

  • 显示该用户已创建
  • 显示一个文件夹的内容时,文件夹是文件夹列表的要求点击

这里是我是如何实现它。

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 /文件夹/ 1`的。如果我碰巧这个URL粘贴在浏览器的地址栏中的文件夹列表将不会被渲染,因为它不匹配的路由。

难道是在路由器的initialize功能来显示文件夹列表中选择一个明智的策略?可能会创建一个页面视图来检查文件夹是否已被显示或不显示?

回答

5

如果我理解正确,文件夹列表中应永久所示。而你的应用程序有两个大视图,文件夹列表和内容。并且必须始终显示文件夹列表。

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) 
    } 
})