Skip to content

Commit fff7786

Browse files
committed
[Ajax] Add ec-crud-ajax-form-before and ec-crud-ajax-form-complete events
1 parent fa748b9 commit fff7786

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

assets/js/ajax.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ export function link (link, options) {
252252
}
253253

254254
export function sendForm (form, options) {
255+
const eventBefore = new CustomEvent('ec-crud-ajax-form-before', {
256+
bubbles: true,
257+
cancelable: true,
258+
detail: {
259+
form,
260+
options
261+
}
262+
})
263+
document.dispatchEvent(eventBefore)
264+
if (eventBefore.defaultPrevented) {
265+
return new Promise((resolve, reject) => {
266+
resolve(null)
267+
})
268+
}
269+
255270
form = optionsResolver.getElement(form)
256271
// Options in data-* override options argument
257272
// Option argument override action, method and data form
@@ -267,6 +282,25 @@ export function sendForm (form, options) {
267282
)
268283
)
269284

285+
const callbacksComplete = []
286+
callbacksComplete.push({
287+
priority: 10,
288+
callback: (statusText, response) => {
289+
const eventOnComplete = new CustomEvent('ec-crud-ajax-form-complete', {
290+
detail: {
291+
form,
292+
statusText,
293+
response
294+
}
295+
})
296+
document.dispatchEvent(eventOnComplete)
297+
}
298+
})
299+
if (optionsResolver.isNotBlank(options.onComplete)) {
300+
callbacksComplete.push(options.onComplete)
301+
}
302+
options.onComplete = callbacksComplete
303+
270304
return sendRequest(options)
271305
}
272306

tests/assets/js/ajax.spec.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,21 +1364,39 @@ describe('Test Ajax.form', function () {
13641364
status: 200,
13651365
responseText: 'CONTENT'
13661366
})
1367+
1368+
jasmine.Ajax.stubRequest(/error404/).andReturn({
1369+
status: 404,
1370+
statusText: 'Not Found',
1371+
response: 'Page not found !',
1372+
responseText: 'Page not found !'
1373+
})
13671374
})
13681375

13691376
afterEach(function () {
13701377
jasmine.Ajax.uninstall()
13711378
$('.html-test').remove()
13721379
callbackManager.clear()
1380+
$(document).off('ec-crud-ajax-form-before')
1381+
$(document).off('ec-crud-ajax-form-complete')
13731382
})
13741383

13751384
it('Send request with form', async function () {
13761385
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
13771386
$('#formToTest input[name=var1]').val('My value 1')
13781387
$('#formToTest input[name=var2]').val('My value 2')
13791388

1389+
const callbackFormBefore = jasmine.createSpy('form_before')
1390+
const callbackFormComplete = jasmine.createSpy('form_complete')
13801391
const callbackSuccess = jasmine.createSpy('success')
13811392

1393+
$(document).on('ec-crud-ajax-form-before', function (event) {
1394+
callbackFormBefore()
1395+
})
1396+
$(document).on('ec-crud-ajax-form-complete', function (event) {
1397+
callbackFormComplete()
1398+
})
1399+
13821400
const promise = ajax.sendForm('#formToTest', {
13831401
onSuccess: function (data, response) {
13841402
callbackSuccess()
@@ -1392,9 +1410,92 @@ describe('Test Ajax.form', function () {
13921410
expect(jasmine.Ajax.requests.mostRecent().url).toMatch('/goodRequest')
13931411
expect(jasmine.Ajax.requests.mostRecent().method).toBe('POST')
13941412
expect(jasmine.Ajax.requests.mostRecent().data()).toEqual([['var1', 'My value 1'], ['var2', 'My value 2']]) // Parsed by addJasmineAjaxFormDataSupport
1413+
expect(callbackFormBefore).toHaveBeenCalled()
1414+
expect(callbackFormComplete).toHaveBeenCalled()
13951415
expect(callbackSuccess).toHaveBeenCalled()
13961416
})
13971417

1418+
it('Send request with form canceled', async function () {
1419+
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
1420+
$('#formToTest input[name=var1]').val('My value 1')
1421+
$('#formToTest input[name=var2]').val('My value 2')
1422+
1423+
const callbackFormComplete = jasmine.createSpy('form_complete')
1424+
1425+
$(document).on('ec-crud-ajax-form-before', function (event) {
1426+
event.preventDefault()
1427+
})
1428+
$(document).on('ec-crud-ajax-form-complete', function (event) {
1429+
callbackFormComplete()
1430+
})
1431+
1432+
const promise = ajax.sendForm('#formToTest')
1433+
expect(promise).toBeInstanceOf(Promise)
1434+
1435+
await promise
1436+
1437+
expect(callbackFormComplete).not.toHaveBeenCalled()
1438+
})
1439+
1440+
it('Send request with form and complete callback', async function () {
1441+
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
1442+
$('#formToTest input[name=var1]').val('My value 1')
1443+
$('#formToTest input[name=var2]').val('My value 2')
1444+
1445+
const callbackFormBefore = jasmine.createSpy('form_before')
1446+
const callbackFormComplete = jasmine.createSpy('form_complete')
1447+
const callbackComplete = jasmine.createSpy('complete')
1448+
1449+
$(document).on('ec-crud-ajax-form-before', function (event) {
1450+
callbackFormBefore()
1451+
})
1452+
$(document).on('ec-crud-ajax-form-complete', function (event) {
1453+
callbackFormComplete()
1454+
})
1455+
1456+
const promise = ajax.sendForm('#formToTest', {
1457+
onComplete: function (statusText, response) {
1458+
callbackComplete()
1459+
}
1460+
})
1461+
expect(promise).toBeInstanceOf(Promise)
1462+
1463+
const response = await promise
1464+
1465+
expect(response).toBeInstanceOf(Response)
1466+
expect(jasmine.Ajax.requests.mostRecent().url).toMatch('/goodRequest')
1467+
expect(jasmine.Ajax.requests.mostRecent().method).toBe('POST')
1468+
expect(jasmine.Ajax.requests.mostRecent().data()).toEqual([['var1', 'My value 1'], ['var2', 'My value 2']]) // Parsed by addJasmineAjaxFormDataSupport
1469+
expect(callbackFormBefore).toHaveBeenCalled()
1470+
expect(callbackFormComplete).toHaveBeenCalled()
1471+
expect(callbackComplete).toHaveBeenCalled()
1472+
})
1473+
1474+
it('Send request with form and error', async function () {
1475+
$('body').append('<form action="/error404" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
1476+
$('#formToTest input[name=var1]').val('My value 1')
1477+
$('#formToTest input[name=var2]').val('My value 2')
1478+
1479+
const callbackFormComplete = jasmine.createSpy('form_complete')
1480+
const callbackSuccess = jasmine.createSpy('success')
1481+
1482+
$(document).on('ec-crud-ajax-form-complete', function (event) {
1483+
callbackFormComplete()
1484+
})
1485+
1486+
const promise = ajax.sendForm('#formToTest', {
1487+
onSuccess: function (data, response) {
1488+
callbackSuccess()
1489+
}
1490+
})
1491+
expect(promise).toBeInstanceOf(Promise)
1492+
1493+
await promise
1494+
1495+
expect(callbackFormComplete).toHaveBeenCalled()
1496+
expect(callbackSuccess).not.toHaveBeenCalled()
1497+
})
1498+
13981499
it('Send request with form with Element', async function () {
13991500
$('body').append('<form action="/goodRequest" method="POST" class="html-test" id="formToTest"><input type="text" name="var1" /><input type="text" name="var2" /></form>')
14001501
$('#formToTest input[name=var1]').val('My value 1')
@@ -1536,6 +1637,16 @@ describe('Test Ajax.form', function () {
15361637
$('#formToTest input[name=var1]').val('My value 1')
15371638
$('#formToTest input[name=var2]').val('My value 2')
15381639

1640+
const callbackFormBefore = jasmine.createSpy('form_before')
1641+
const callbackFormComplete = jasmine.createSpy('form_complete')
1642+
1643+
$(document).on('ec-crud-ajax-form-before', function (event) {
1644+
callbackFormBefore()
1645+
})
1646+
$(document).on('ec-crud-ajax-form-complete', function (event) {
1647+
callbackFormComplete()
1648+
})
1649+
15391650
$('#formToTest button[type="submit"]').get(0).click()
15401651

15411652
await wait(() => {
@@ -1545,6 +1656,8 @@ describe('Test Ajax.form', function () {
15451656
expect(jasmine.Ajax.requests.mostRecent().url).toMatch('/goodRequest')
15461657
expect(jasmine.Ajax.requests.mostRecent().method).toBe('POST')
15471658
expect(jasmine.Ajax.requests.mostRecent().data()).toEqual([['var1', 'My value 1'], ['var2', 'My value 2']]) // Parsed by addJasmineAjaxFormDataSupport
1659+
expect(callbackFormBefore).toHaveBeenCalled()
1660+
expect(callbackFormComplete).toHaveBeenCalled()
15481661
})
15491662

15501663
it('Send auto-request with form canceled', async function () {

0 commit comments

Comments
 (0)