Backbone.js 中控制器的作用
如果我创建一个具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这正是你应该做的。重新渲染是一个坏主意,因为您必须记住状态(即用户在编辑选项卡的字段中输入一些内容,然后切换到索引 - 当他返回编辑时,该字段将为空)。简单地隐藏和显示适当的图层来进行导航也更快。
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.