사이드 바 및 여러 하위보기로 구성된 단일 백본보기가 있습니다. 단순화를 위해 사이드 바 및 하위보기가 단일 render
함수에 의해 관리되도록하기로 결정했습니다. 그러나 사이드 바 항목 중 하나를 클릭하면 click .edit
이벤트가 여러 번 실행되는 것으로 보입니다. 예를 들어 '일반'으로 시작하여 .edit
을 클릭하면 hello
번만 실행됩니다. 사이드 바에서 .profile
을 클릭하고 .edit
을 다시 클릭하면 hello
이 두 번 발생합니다. 어떤 아이디어?하위보기를 다시 랜더링 한 후에 백본 이벤트가 여러 번 발생합니다.
보기
events: {
"click .general": "general",
"click .profile": "profile",
"click .edit": "hello",
},
general: function() {
app.router.navigate("/account/general", {trigger: true});
},
profile: function() {
app.router.navigate("/account/profile", {trigger: true});
},
render: function(section) {
$(this.el).html(getHTML("#account-template", {}));
this.$("#sidebar").html(getHTML("#account-sidebar-template", {}));
this.$("#sidebar div").removeClass("active");
switch (this.options.section) {
case "profile":
this.$("#sidebar .profile").addClass("active");
this.$("#content").html(getHTML("#account-profile-template"));
break;
default:
this.$("#sidebar .general").addClass("active");
this.$("#content").html(getHTML("#account-general-template"));
}
},
hello: function() {
console.log("Hello world.");
},
라우터
account: function(section) {
if (section) {
var section = section.toLowerCase();
}
app.view = new AccountView({model: app.user, section: section});
},
솔루션 내 솔루션은 다음과 라우터 변경했다
:
을account: function(section) {
if (section) {
var section = section.toLowerCase();
}
if (app.view) {
app.view.undelegateEvents();
}
app.view = new AccountView({model: app.user, section: section});
},
이것은 현재 작동하지만 메모리 누수가 발생합니까?
라우터 코드를 게시하면 중복보기 인스턴스를 생성하고 있음을 확인할 수 있습니다. –
좋아, 라우터의 관련 섹션을 게시했습니다. –