Javascript 主干子计算机到特定视图
是否可以将子计算机添加到特定视图?假设我有一个多车主干应用程序。现在,我的路由器看起来像这样Javascript 主干子计算机到特定视图,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,是否可以将子计算机添加到特定视图?假设我有一个多车主干应用程序。现在,我的路由器看起来像这样 carRouter = Backbone.Router.extend({ routes: { '': 'index' 'contact' : 'contact' ':car': 'car', ':car/gallery' : 'cargallery', ':car/specs' : 'specs', ':car/infos'
carRouter = Backbone.Router.extend({
routes: {
'': 'index'
'contact' : 'contact'
':car': 'car',
':car/gallery' : 'cargallery',
':car/specs' : 'specs',
':car/infos' : 'infos',
'about: 'aboutPage'
}
car: function(){
// do this
},
cargallery: function(){
// do this
},
specs: function(){
// do this
},
infos: function(){
// do this
}
...etc
});
这种方法显然会使整个页面呈现,这是我基本上想要避免的。例如,当我来回点击gallery和specs时,每次点击都会重新呈现整个页面
那么,有没有可能采取如下措施:
routes: {
'contact' : 'contact'
':car': 'car',
'about: 'aboutPage'
},
car: function(){
// new router containing
// ':car/gallery' : 'cargallery',
// ':car/specs' : 'specs',
// ':car/infos' : 'infos',
},
}
然后,在汽车页面上,我会在菜单库中有3个选项卡,“规格”、“信息”,这将加载特定汽车的模型/集合,而无需重新呈现页面
欢迎提供任何帮助/建议 您可以通过事件、主干触发器和自定义EventHandler对象来实现这一点 CarView中的事件哈希:
events : {
"click .spec" : "showSpecs",
"click .gallery" : "showGallery",
"click .info" : "showInfo",
},
//Invoked when you click the show specs tab.
showSpecs : function(event) {
//Say that EventBus is your custom event handler
event.preventDefault();
MyApp.EventBus.trigger("show:spec",payload);
},
showGallery : function(event) {
event.preventDefault();
MyApp.EventBus.trigger("show:gallery",payload);
}
....
在MyApp.EventBus中:
_.extend(MyApp.EventBus,Backbone.Events);
MyApp.EventBus.showGallery = function(payload) {
// payload is an optional data that you can get from the triggered view
// Fetch your models or collection
// Instantiate the corresponding view and pass your data(models/collections) to it.
// Update the url to reflect the new view so that if you hit refresh you
// come to the same tab.
MyApp.router.navigate("/your-url",{trigger:false});
}
MyApp.EventBus.on("show:gallery",showGallery);
还可以在路由器中添加一个方法,该方法可以处理标签的刷新