Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoon data-association-insertion-node with a function

According https://github.com/nathanvda/cocoon#link_to_add_association you should be able to pass a function to data-association-insertion-node

I have tried this:

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row()'} %>

And this:

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row'} %>

And this (getting desperate):

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'function get_row(node){var row = "<tr></tr>";$("#table_body").append(row);return row;}'} %>

But none of them work.

Javascript:

function get_row(node){
    var row = "<tr></tr>"
    $("#table_body").append(row);
    return row
}

I am trying to add a tr to a table and then append the nested timeslot form to the tr.

like image 496
DrevanTonder Avatar asked Mar 10 '17 02:03

DrevanTonder


1 Answers

At the moment this is not possible what you are trying to do. When setting the data-association-insertion-method like this, it will just be a string in javascript, and not a reference to a method.

As documented in the README, you have to do the following (assuming you gave your links a class .add-timeslot):

$(document).on('turbolinks:load', function() {
  $(".add-timeslot a").
    data("association-insertion-method", 'append').
    data("association-insertion-node", function(link){
      return link.closest('.row').next('.row').find('.sub_tasks_form')
    });
});

(the example is almost verbatim copied from documention for demonstration purposes only --you will have to fill in your get_row function)

like image 99
nathanvda Avatar answered Nov 05 '22 03:11

nathanvda