마리온네 (Marionette)는 훌륭하지만 약간의 것들은 따라하기가 다소 혼란 스러울 수 있습니다. 처음 페이지를로드 할 때 Marionette.Layout을 Marionette.Application 영역에 표시합니다. 그러나 어떤 이유로 클릭 이벤트는 위임되지만 실제로 응답하지는 않습니다. 다른 경로 (레이아웃보기 제거)로 이동 한 다음 경로로 돌아가서 (보기 재 렌더링) 이벤트가 활성화됩니다. 뷰를 연속적으로 두 번 렌더링하게하면 작동합니다. 어쩌면이 시스템을 잘못 초기화하는 것일 수 있습니다.Backbone.Marionette.Application을 사용할 때 어떻게 이벤트를 초기화합니까?
var ListView = Backbone.Marionette.Layout.extend({
el: "#listView", // exists on DOM already
events: {
// this is what is supposed to work
// as well as events in my subviews
"click" : function() { console.log("clicked");}
});
var Router = Backbone.Router.extend({
initialize: function(options) {
this.app = options.app;
},
start: function(page) {
console.log("start...");
// with silent false this causes list() to be called
// and the view is rendered
// but the ListView does not get its events delegated
Backbone.history.start({
pushState: false,
silent: false
});
// only by running loadUrl here
// do I get the events delegated
// or by switching between other routes
// and coming back to 'list'
// but this is causing a second useless calling
// of list() re-rendering
console.log("loadUrl...");
Backbone.history.loadUrl(page);
// actually the events are not yet delegated at this point
// if I pause a debugger here.
// events only active after the Application finishes its initializers
},
routes: {
'list': 'list'
},
list: function() {
var lv = ListView({});
this.app.focus.show(lv)
}
});
var app = new Backbone.Marionette.Application();
app.addInitializer(function(options) {
this.router = new Router({app: this});
this.router.start();
});
app.addRegions({
// #focus does exist on the DOM
focus: "#focus",
});
app.start({});
내가 일하는 것이 기대하지만 슬프게도 loadUrl의 마법 효과가 무엇인지 볼 수 없습니다 아직 내가 디버거 모두를 강화했습니다
start: function(page) {
console.log("start...");
Backbone.history.start({
pushState: false,
silent: true // don't trigger the router
});
// call the router here via history
console.log("loadUrl...");
Backbone.history.loadUrl(page);
// causes page to be rendered correctly
// but events are not delegated
},
않습니다 있지만 바꾸는 시작 그것이 작동하게 만듭니다.
은 또한 단지 지역으로 레이아웃을 다시 삽입 시도했다 :
app.focus.show(app.focus.currentView)
어쩌면 단순히이 문제로 실행되지 않을 것이라고이를 구성하는 다른 방법이있다.
답변 덕분에! 이것이 문제를 해결할 가능성이 높습니다. 나는 아직 그것을 시험해 볼 기회가 없었지만 곧 돌아와서 당신의 답을 곧 받아 들일 것입니다. – felix
하지 않았습니다. 메소드는 attachView()이지만보기를 렌더링하지 않으므로 여전히 show()를 수행해야합니다. 아주 작은 사용자 최종 결과로 DOM을 너무 많이 저글링하지 않아야한다고 결정했습니다. 따라서 사용자가 페이지를 매기거나 검색 결과를 변경하면 서버에서 새로운 결과가 렌더링되어 목록에 추가되지만 첫 페이지로드시 페이지에 이미있는 내용은보기와 연관되어서는 안됩니다. 그것에 이득이 없다. – felix