2013-03-10 4 views
1

저는 백본에 젖은 채 젖히고 있습니다. 문제는 해결하기 쉽습니다. 2 시간마다 발사됩니다백본 이벤트가 여러 번 호출되었습니다.

myApp.views.Support = { 
    Form: Backbone.View.extend({ 
     initialize: function() { 
      this.el = $('#support'); 
      this._ensureElement(); 
     }, 
     render: function() { 
      if (this.$el.hasClass('support-panel')) { 
       // close panel 
       this.$el.empty(); 
       this.$el.removeClass('support-panel'); 
       this.$el.addClass('support-button'); 
      } 
      else { 
       // open and populate panel 
       var template = _.template(myApp.utils.RenderTemplate('support/default'), {}); 
       this.$el.removeClass('support-button'); 
       this.$el.addClass('support-panel'); 
       this.$el.html(template); 
      } 

      return this; 
     }, 
     closePanel: function() { 
      alert('close event fired'); 
     }, 
     events: { 
      'click #SubmitFormButton': 'submitForm', 
      'click #CloseSupportPanel': 'closePanel' 
     }, 
     submitForm: function (event) { 
      alert('form submitted: ' + $('#message')); 
     } 
    }) 
} 

모두가 "closePanel"를 제외하고 잘 작동 : 나는 클릭 패널을 열고 닫을 때 때이 탭으로 돌아갑니다 것을 간단한 탭입니다 다음보기가 클릭 이벤트가 발생합니다. 나는 그것이 내가 빠뜨린 일종의 정리라고 생각하지만, 나는 무엇을 모르고있다.

답변

3

이벤트가 버블 링되기 때문에 가능성이 있습니다. false을 반환 해보세요.

+0

이 방법은 효과가 있지만 "최상의"방법은 아닙니다. 나는 그 진술 뒤에 절대적으로 근거가 없다는 것을 완전히 인정한다. 아마도이 시나리오를 다루는 적절한 방법 일 것이다. 어떻게 생각해? – Matt

+0

올바른 방법. 자바 스크립트가 작동하는 방법입니다. –

1

나는 이것이 오래된 질문이지만 내 문제가 무엇인지 깨닫는데 도움이되었다. Daniel이 말했듯이 false로 돌아 왔지만 문제의 근본 원인은 내 마크 업에서 두 ​​번 jQuery 선택기를 사용했기 때문에 두 개의 jQuery 객체가 생성되어 click 이벤트가 두 번 발생합니다.

+0

올바른 방향으로 나를 가리켜 주셔서 감사합니다! – Tuan

관련 문제