Skip to content

Conversation

@mlocati
Copy link
Contributor

@mlocati mlocati commented Oct 28, 2025

What about using window.fetch() instead of jQuery.ajax() to perform ajax requests?

For example, the following code:

$('#ccm-permissions-list-paste-permissions-<?= $uid ?>').click(function() {
    jQuery.fn.dialog.showLoader();
    var frm = $(this).closest('.ccm-permission-grid');
    var data = 'pkCategoryHandle=' + frm.find('input[name=pkCategoryHandle]').val();
    $.ajax({
        dataType: 'json',
        type: 'post',
        data: data,
        url: <?= json_encode($resolverManager->resolve(['/ccm/system/permissions/set/paste']) . '?' . $valt->getParameter('paste_permission_set')) ?>,
        success: function(r) {
            jQuery.fn.dialog.hideLoader();
            for (i = 0; i < r.length; i++) {
                var cell = r[i];
                $('#ccm-permission-grid-cell-' + cell.pkID).html(cell.html);
                $('#ccm-permission-grid-name-' + cell.pkID + ' a').attr('data-paID', cell.paID);        
            }

        }                
    })
})

could be written (with added error handling) as:

$('#ccm-permissions-list-paste-permissions-<?= $uid ?>').click(async function() {
    jQuery.fn.dialog.showLoader();
    try {
        var frm = $(this).closest('.ccm-permission-grid');
        const cells = await ConcreteFetch.fetchJson(
            <?= json_encode($resolverManager->resolve(['/ccm/system/permissions/set/paste']) . '?' . $valt->getParameter('paste_permission_set')) ?>,
            {
                method: 'POST',
                body: {
                    pkCategoryHandle: frm.find('input[name=pkCategoryHandle]').val(),
                },
            }
        );
        cells.forEach(function(cell) {
            $('#ccm-permission-grid-cell-' + cell.pkID).html(cell.html);
            $('#ccm-permission-grid-name-' + cell.pkID + ' a').attr('data-paID', cell.paID);
        });
    } catch (e) {
        ConcreteAlert.error({message: e.message, plainTextMessage: true});
    } finally {
        jQuery.fn.dialog.hideLoader();
    }
});

@mlocati
Copy link
Contributor Author

mlocati commented Oct 28, 2025

Mmm... ConcreteFetch.fetchJson() is maybe a bit reduntant... what about simply ConcreteFetch.json()?

@mlocati
Copy link
Contributor Author

mlocati commented Oct 29, 2025

Mmm... ConcreteFetch.fetchJson() is maybe a bit reduntant... what about simply ConcreteFetch.json()?

Pull request updated in ddbae95

@aembler
Copy link
Member

aembler commented Nov 3, 2025

Cool! Looking forward to integrating this into more Vue components going forward and easing the dependence on jQuery further.

@aembler aembler merged commit c2cc024 into concretecms:1.6.x Nov 3, 2025
1 check passed
@mlocati mlocati deleted the fetch branch November 3, 2025 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants