window.nested_form_fields or= {} nested_form_fields.bind_nested_forms_links = () -> $('body').off("click", '.add_nested_fields_link') $('body').on 'click', '.add_nested_fields_link', (event) -> $link = $(this) object_class = $link.data('object-class') association_path = $link.data('association-path') added_index = $(".nested_#{association_path}").length $.event.trigger("fields_adding.nested_form_fields",{object_class: object_class, added_index: added_index, association_path: association_path}); if $link.data('scope') $template = $("#{$link.data('scope')} ##{association_path}_template") else $template = $("##{association_path}_template") target = $link.data('insert-into') template_html = $template.html() # insert association indexes index_placeholder = "__#{association_path}_index__" template_html = template_html.replace(new RegExp(index_placeholder,"g"), added_index) # replace child template div tags with script tags to avoid form submission of templates $parsed_template = $(template_html) $child_templates = $parsed_template.closestChild('.form_template') $child_templates.each () -> $child = $(this) $child.replaceWith($("