예, 부모 섹션을 만든 후에 항상 하위 섹션을 호출해야하며 직접 URL 또는 라우터 탐색을 통해 액세스 한 경우에는 문제가되지 않습니다.
내 해결 방법은 항상 내 응용 프로그램에 기본보기가 있으며 라우터는 항상이 기본보기를 호출합니다. 라우터는 다른보기에 액세스 할 수 없습니다. 내 메인보기에서는 부모보기가 만들어지는 경우를 처리 할 수 있습니다. 또한 처음부터 새 프로젝트를하지 시작하려는 경우
var MainView = Backbone.View.extend({
id : 'mainView',
categories : null,
events : {
},
initialize : function(){
_.bindAll(this);
},
openSection : function(section){
switch(section){
case 'categories':
this.validateCategories();
break;
case 'products':
this.validateCategories();
this.categories.open(new ProductsView());
break;
}
},
validateCategories : function(){
if(!this.categories){
//we create the parent view only if not yet created
this.categories = new CategoriesView();
}
}
});
var mainView = new MainView();
var RouterClass = Backbone.Router.extend({
routes : {
"categories" : "viewCategories",
"categories/:id/:section" : "viewProducts"
},
viewCategories : function(path) {
mainView.openSection('categories');
},
viewProducts : function(id, section){
mainView.model.set({
productId : id,
section : section,
});
mainView.openSection('products');
}
});
:
예, 라우터는 MAINVIEW를 호출하고 거기에 내가 필요한 경우 부모 뷰를 생성하는 방법이라는 validateCategories을 얼마나 확인 이 확장 프로그램에서 Backbone.js 프로젝트를 구성하는 데 도움이되는 것을 잊지 마십시오. https://github.com/derickbailey/backbone.marionette