问题
In the edit modal for my company entity, I open the address entity create modal. This allows the user to create an address for the company. On the .done of the address create app service method, I trigger an event using the abp.event.trigger method. The company edit modal then watches for this event so that it can create an entry into the companyAddress entity. Soon as the companyAddress entity app service method ends, on .done event, I turn off the event trigger. However, after the first instance of creating an address, when the user adds more addresses the trigger mechanism is firing multiple times and causing duplicate entries in the companyAddress table. I have debugged this over and over and read the docs for abp event triggers and cannot figure out how this happening. Any help would be much appreciated.
Create address modal 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);
});
};
Edit company modal 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');
});
});
Here is google chrome console showing the duplication.
I just tested the modals again, I entered about 8 different addresses via the create modal for 2 differnt companies. For this test, the duplication issue did not happen. But the issue where the event does not fire for each address created keeps happening. As you can see from the console log below, the ID number 2,3,5 and 6 did not generate the "started" log entry. My companyAddress table is also missing the entry for these 4 IDs, so the event did not trigger.
Edit company modal.js full updated code
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);
Chrome console logs for updated JS code
回答1:
From the documentation on Register To Events:
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.
You're passing in a dummy object:
abp.event.off('addressCreated', {
id: null
});
Do this:
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]
You're only calling .on
once per EditCompanyModal
, and then .off
-ing it.
Move the .off into this.save = ....
Update
Move the .off
into this.init = ...
.
this.init = function (modalManager) {
_modalManager = modalManager;
// ...
_modalManager.onClose(function () {
abp.event.off('addressCreated', addressCreated); // Turn off this handler
});
};
来源:https://stackoverflow.com/questions/47851912/event-bus-trigger-duplicating