Backbone.js:导航路线时何时渲染视图

发布于 2024-12-11 15:42:12 字数 587 浏览 0 评论 0原文

以下是我的骨干应用程序的要求:

  • 显示用户创建的文件夹列表,
  • 单击文件夹时显示文件夹的内容,

这是我的实现方式。

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”。如果我碰巧将此网址粘贴到浏览器的地址栏中,则不会呈现文件夹列表,因为它与路线不匹配。

在路由器的initialize功能中显示文件夹列表是一个明智的策略吗?可以创建一个页面视图来检查文件夹是否已显示?

Here are the requirements for my backbone app

  • display a list of folders that user has created
  • display the contents of a folder when a folder is clicked

Here is how I've implemented it.

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

This approach is giving me problems since when I click on a folder, the route gets saved in browser history and is of the structure 'domain.com#get/folder/1`. If I happen to paste this url in the address bar of browser, the list of folders won't be rendered since it doesn't match the route.

Would it be a smart strategy to display the list of folders in initialize function of router? may be create a page view which checks if the folders have already been displayed or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-12-18 15:42:12

如果我理解正确,文件夹列表应该永久显示。您的应用程序有两个大视图:文件夹列表和内容。并且文件夹列表必须一直显示。

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

If I understand correctly, the list of folders should be shown permanently. And your application has two big views, the list of folders, and the contents. And the list of folders must be displayed all the time.

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)
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文