2017-12-17 3 views
0

내 회사 엔티티 편집 모달에서 주소 엔티티 작성 모달을 엽니 다. 이를 통해 사용자는 회사 주소를 작성할 수 있습니다. 주소 생성 앱 서비스 메서드의 .done에서 나는 abp.event.trigger 메서드를 사용하여 이벤트를 트리거합니다. 회사 편집 모달은이 이벤트를 감시하여 companyAddress 엔터티에 항목을 만들 수 있습니다. 곧 companyAddress 엔티티 앱 서비스 메소드가 종료되면 .done 이벤트에서 이벤트 트리거를 끕니다. 그러나 주소를 만드는 첫 번째 인스턴스 이후에 사용자가 더 많은 주소를 추가하면 트리거 메커니즘이 여러 번 실행되고 companyAddress 테이블에 중복 항목이 발생합니다. 나는 이것을 계속해서 디버깅하고 abp 이벤트 트리거를위한 ​​문서를 읽었으며 어떻게 이런 일이 일어나는지 알 수 없다. 어떤 도움이라도 대단히 감사 할 것입니다.이벤트 버스 트리거 복제

주소 모달 JS

this.save = function() { 
     if (!_$form.valid()) { 
      return; 
     } 
     var address = _$form.serializeFormToObject(); 
     _modalManager.setBusy(true); 
     _addressService.createAddress(
      address 
     ).done(function (result) { 
      abp.notify.info(app.localize('SavedSuccessfully')); 
      abp.event.trigger('addressCreated', { 
       id: result 
      }); 
      console.log('addressCreated event triggered for id: ' + result); 
      _modalManager.close(); 
     }).always(function() { 
      _modalManager.setBusy(false); 
     }); 
    }; 

편집 회사를 만들기 모달 JS 여기

abp.event.on('addressCreated', function (item) { 
     console.log('addressCreated event caught for id: ' + item.id); 
     //Call company address service 
     var _companyAddressService = abp.services.app.companyAddress; 
     _companyAddressService.createCompanyAddress({ 
      CompanyId: $("#CompanyId").val(), 
      AddressId: item.id 
     }).done(function() { 
      abp.event.off('addressCreated', { 
       id: null 
      }); 
      console.log('addressCreated event turned off for id: ' + item.id); 
      abp.notify.success(app.localize('AddressCreated')); 
      abp.ui.clearBusy('.modal-body'); 
     }); 
    }); 

는 중복을 보여주는 구글 크롬 콘솔입니다. enter image description here

방금 ​​모달을 테스트 한 결과 두 개의 다른 회사에 대해 작성 모드를 통해 약 8 개의 다른 주소로 입력했습니다. 이 테스트에서는 중복 문제가 발생하지 않았습니다. 그러나 생성 된 각 주소에 대해 이벤트가 실행되지 않는 문제가 계속 발생합니다. 아래의 콘솔 로그에서 볼 수 있듯이 ID 번호 2,3,5 및 6은 "시작된"로그 항목을 생성하지 않습니다. 내 companyAddress 테이블에도이 4 가지 ID의 항목이 누락되어 이벤트가 트리거되지 않았습니다. enter image description here

편집 회사는 Register To Events에 문서에서 업데이트 코드

var EditCompanyModal = (function ($) { 
app.modals.EditCompanyModal = function() { 

    var _modalManager; 
    var _companyService = abp.services.app.company; 

    var _$Form = null; 
    this.init = function (modalManager) { 
     _modalManager = modalManager; 

     _$Form = _modalManager.getModal().find('form[name=EditCompany]'); 
     $(".modal-dialog").addClass("modal-lg"); 
     _$Form.validate(); 
    }; 

    this.save = function() { 
     if (!_$Form.valid()) { 
      return; 
     } 
     var company = _$Form.serializeFormToObject(); 
     _modalManager.setBusy(true); 
     _companyService.updateCompany(
      company 
     ).done(function() { 
      abp.notify.info(app.localize('SavedSuccessfully')); 
      _modalManager.close(); 
      abp.event.trigger('app.editCompanyModalSaved'); 
     }).always(function() { 
      _modalManager.setBusy(false); 
     }); 

     abp.event.off('addressCreated', addressCreated); // Turn off this handler 
    }; 

    var _editModal = new app.ModalManager({ 
     viewUrl: abp.appPath + 'Nursing/Address/EditModal', 
     scriptUrl: abp.appPath + 'view-resources/Areas/Nursing/Views/Address/_EditModal.js', 
     modalClass: 'EditAddressModal' 
    }); 

    var _createModal = new app.ModalManager({ 
     viewUrl: abp.appPath + 'Nursing/Address/CreateModal', 
     scriptUrl: abp.appPath + 'view-resources/Areas/Nursing/Views/Address/_CreateModal.js', 
     modalClass: 'CreateAddressModal' 
    }); 

    $('#add_new_address').click(function (e) { 
     _createModal.open(); 
    }); 

    $('#addressTiles').on('click', '.btnEditAddress', function() { 
     var addressID = $(this).parent().find("input").first().val(); 
     _editModal.open({ id: addressID }); 
    }); 

    abp.event.on('addressCreated', addressCreated); 

    //After address create event, save company address Id 
    function addressCreated(item) { 
     console.log(new Date().toUTCString() + ' - addressCreated started for id: ' + item.id); 
     //Call company address service 
     var _companyAddressService = abp.services.app.companyAddress; 
     _companyAddressService.createCompanyAddress({ 
      CompanyId: $("#CompanyId").val(), 
      AddressId: item.id 
     }).done(function() { 
      console.log('addressCreated event turned off for id: ' + item.id); 
      abp.notify.success(app.localize('AddressCreated')); 
      abp.ui.clearBusy('.modal-body'); 
     }); 
    } 

};})(jQuery); 
업데이트 JS 코드

크롬 콘솔 로그 enter image description here

답변

1

전체 modal.js :

You can use abp.event.off method to unregister from an event. Notice that; same function should be provided in order to unregister. So, for the example above, you should set the callback function to a variable, then use both in on and off methods.

당신 '아르 자형 전자 더미 객체를 전달 :

abp.event.off('addressCreated', { 
    id: null 
}); 

는이 작업을 수행합니다 : 당신은 그것을 -ing 다음 .offEditCompanyModal 당 한 번 .on를 호출하고있어

function addressCreated(item) { 
    console.log('addressCreated event caught for id: ' + item.id); 
    //Call company address service 
    var _companyAddressService = abp.services.app.companyAddress; 
    _companyAddressService.createCompanyAddress({ 
     CompanyId: $("#CompanyId").val(), 
     AddressId: item.id 
    }).done(function() { 
     abp.event.off('addressCreated', addressCreated); // Turn off this handler 
     console.log('addressCreated event turned off for id: ' + item.id); 
     abp.notify.success(app.localize('AddressCreated')); 
     abp.ui.clearBusy('.modal-body'); 
    }); 
} 

abp.event.on('addressCreated', addressCreated); 

The addressCreated function is executing not at all sometime[s]

.

이동 this.save에 .OFF = ....

Update

this.init = ....off로 이동.

this.init = function (modalManager) { 
    _modalManager = modalManager; 

    // ... 

    _modalManager.onClose(function() { 
     abp.event.off('addressCreated', addressCreated); // Turn off this handler 
    }); 
}; 
+0

그 변화를 시도했지만 지금은 "catch되지 않은 오류 ReferenceError을 : addressCreated이 정의되지 않은"지고있어 그것은 근무 abp.event.off 라인 – exlnt

+0

에,하지만 난 더 테스트 유지로, 복제는 여전히 일어나고있다 , 때때로.addressCreated 함수는 여러 번 실행 중이며 언젠가는 실행되지 않습니다. 최신 크롬 콘솔 로그를 표시하도록 내 게시물을 업데이트했습니다. – exlnt

+0

네, 최신 콘솔 이미지에서 제가 문제를 공유 한 것은 트리거 이벤트가 ID 2와 3에 대해 실행되지 않았기 때문입니다. 같은 시간 동안 다른 ID가 여러 번 발동합니다. – exlnt