jeudi 22 septembre 2022

Calling AJAX in Coffeescript

I have an ajax call that works well in a view, but I would like to make it in a coffee script, right inside a datatable call. The code looks like:

    $(document).ready(function () {
    $.ajax({
      type: "GET",
      url: "<%= people_path(format: :json) %>",
      dataType: "json",
      success: function({data}) {
        const user_ids = data.map(user => user.id)
        $("#people-ids").html(user_ids.join());
      }
    });
  });

I am new to coffeescript, so I need some help to finish the method around the mapping section. I could go as far as this:

ajax:
  type: 'GET'
  url: $('#people-datatable').data('source')
  dataType: 'json'
  success: ({data}) ->

I obtain 7 objects in {data}, but i do not know how to continue to retrieve their ID as in JS. can someone help please ?

Aucun commentaire:

Enregistrer un commentaire