ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

after custom insertFields, cannot trigget nested:fieldAdded #325

Closed hlcfan closed 10 years ago

hlcfan commented 10 years ago
window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
  $('table tbody').append(content);
}

$(document).on('nested:fieldAdded', function(event){
...
});

chrome console: Uncaught TypeError: Cannot read property 'trigger' of undefined jquery_nested_form.js?body=1:48

lest commented 10 years ago

insertFields should return inserted content so you can change your code to:

window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
  $('table tbody').append(content);
  return $(content);
}
hlcfan commented 10 years ago

will override window.NestedFormEvents.prototype.insertFields affect listen to nested:fieldAdded ? I found that when i custom window.NestedFormEvents.prototype.insertFields, it cannot listen to nested:fieldAdded.

lest commented 10 years ago

You can see in https://github.com/ryanb/nested_form/blob/master/vendor/assets/javascripts/jquery_nested_form.js#L51 that insertFields return value is used to trigger nested:fieldAdded event.

lest commented 10 years ago

It means that you might need something like:

window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
  var target = $('table tbody');
  return $(content).appendTo(target);
}
hlcfan commented 10 years ago

ya, thanks.