public/javascripts/nested_fields.js in nested_fields-0.3.0 vs public/javascripts/nested_fields.js in nested_fields-0.4.0
- old
+ new
@@ -1,49 +1,34 @@
-$(document).ready(function() {
- $('.nested_fieldset').each(function(i, nf) {
- $(nf).trigger('nested_fields_ready');
- });
-});
-
-$('.add_nested_fields').live('click', function() {
+$('a.add_nested_fields').live('click', function() {
$.ajax({
context: this,
dataType: 'html',
url: this.getAttribute('data-url'),
success: function(content) {
$(this).after(content);
- $(this).trigger('nested_fields_ready');
+ $(this).trigger('nested_fields_changed');
}
});
return false;
});
-$('.remove_nested_fields').live('click', function() {
- $(this).closest('div.nested_fields').find('input[type=hidden]').val('1');
- $(this).closest('div.nested_fields').hide();
+$('a.remove_nested_fields').live('click', function() {
+ $(this).closest('div.nested_fields').hide().find('input[type=hidden].nested_fields_destroy').val('1');
+ $(this).closest('.nested_fieldset').trigger('nested_fields_changed');
return false;
});
-$('.nested_fields_has_one').live('change', function() {
- var hidden_field = $(this).closest('.nested_fieldset').find('.destroy_nested_one');
- $(hidden_field).val($(hidden_field).val() == 'true' ? 'false' : 'true');
- $(this).closest('.nested_fieldset').find('.nested_one').toggle();
+$('input[type=checkbox].nested_fields_has_one').live('change', function() {
+ var hidden_field = $(this).closest('.nested_fieldset').find('input[type=hidden].destroy_nested_one');
+ $(hidden_field).val( $(hidden_field).val() == '' ? 1 : '' );
+ var nfs = $(this).closest('.nested_fieldset')
+ nfs.find('.nested_one').toggle();
+ nfs.trigger('nested_fields_changed')
});
-function reposition(nested_fieldset, pos_fld) {
- $(nested_fieldset).find('> .nested_fields').each(function(i, nested_fields) {
+/* reset position inputs to reflect addition or removal of associates */
+function repositionNestedFields(nested_fieldset, pos_fld) {
+ $(nested_fieldset).children('.nested_fields:visible').each(function(i, nested_fields) {
$(nested_fields).find('input[id$="' + pos_fld + '"]').val(i);
});
}
-
-$('fieldset.positioned').live('nested_fields_ready', function() {
- var pos_fld = this.getAttribute('data-positioned-by');
-
- if (pos_fld) {
- reposition(this, pos_fld);
-
- if (!$(this).hasClass('ui-sortable')) {
- $(this).sortable({update: function() { reposition(this, pos_fld); }});
- }
- }
-});