Closed tohyongcheng closed 10 years ago
I don't have issue using it on Rails 4. In addition the JS is binding on the document. So it should work.
@tohyongcheng Looks like nested_form js is evaluated multiple times. This is possible if you put application.js
in body
instead of head
. Turbolinks evaluates head
only single time so you should consider putting javascript_include_tag
into head
element.
Is there anyway to improve nested form to support turbolinks properly? I always put javascript after all content to improve page load performance. https://developer.yahoo.com/performance/rules.html#js_bottom
If the script is in the body and evaluated every time then changing the bottom of the nested_form.js file to be the following seems to work:
window.nestedFormEvents = new NestedFormEvents();
// Setup click handlers
$(document)
.on('click', 'form a.add_nested_fields', nestedFormEvents.addFields)
.on('click', 'form a.remove_nested_fields', nestedFormEvents.removeFields);
// Tidy up click handlers
$(document).on('page:after-remove', function () {
$(document)
.off('click', 'form a.add_nested_fields', nestedFormEvents.addFields)
.off('click', 'form a.remove_nested_fields', nestedFormEvents.removeFields);
});
})(jQuery);
After navigating through a few pages, the form adds more than 1 field into the nested form. It is mostly due to turbolinks on Rails 4.
More information on how to solve it may be here: https://github.com/kossnocorp/jquery.turbolinks
I have tried it, but its not working with this gem. We may need to edit the js of the nested_form gem.