Backbone.js 中控制器的作用

发布于 2024-11-08 17:27:08 字数 659 浏览 0 评论 0原文

如果我创建一个具有 3 种不同“状态”的标准 Web 应用程序:索引、查看、编辑,我会创建一个具有 3 种不同路由的控制器吗?

假设索引有 4 个与其关联的 Backbone.Views,而编辑有 3 个。如果从索引导航到编辑:通过简单地删除所有索引 Backbone.Views 并呈现编辑视图来更改页面的外观是否是标准做法?这看起来工作量很大——在模式之间切换时只切换“display:none”是否明智?如果是这样,该功能是否属于控制器的功能范围内?

例如,以下是一种有效的方法吗?

window.MyController = Backbone.Controller.extend({
  routes: {
    '#index':index,
    '#view/:id':view,
    '#edit/:id':edit
  },

  switchState: function(state){
    hideStates();
    showState(state);
   },

  index: function(){
    switchState(index)
  },

  view: function(id){
    switchState('view')
  },

  edit: function(id){
    switchState('edit')
  }
})

If I created a standard webapp with 3 different "states": index, view, edit, would I create a single controller that had 3 different routes?

Assume the index has 4 Backbone.Views associated with it and edit has 3. If one navigates from index to edit: is it standard to change the appearance of the page by simply removing all the index Backbone.Views and rendering the edit ones? That seems like a lot of work - would it be wise to just toggle "display:none" when moving between modes? If so would that functionality be within the Controller's functions?

For instance would the following be a valid way of doing it?

window.MyController = Backbone.Controller.extend({
  routes: {
    '#index':index,
    '#view/:id':view,
    '#edit/:id':edit
  },

  switchState: function(state){
    hideStates();
    showState(state);
   },

  index: function(){
    switchState(index)
  },

  view: function(id){
    switchState('view')
  },

  edit: function(id){
    switchState('edit')
  }
})

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

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

发布评论

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

评论(1

涫野音 2024-11-15 17:27:08

这正是你应该做的。重新渲染是一个坏主意,因为您必须记住状态(即用户在编辑选项卡的字段中输入一些内容,然后切换到索引 - 当他返回编辑时,该字段将为空)。简单地隐藏和显示适当的图层来进行导航也更快。

That's exactly what you should do. Re-rendering on is a bad idea, because you would have to remember states (ie. user typed something into a field in edit tab, then switched to index - when he's back to edit, the field would be empty). It's also faster to base navigation on simply hiding&showing appropriate layers.

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