Sha256: 3556ff36673cf4e2d3210bd302984da941d75ff37bdd9a57426d4c81f52b791a

Contents?: true

Size: 1.52 KB

Versions: 9

Compression:

Stored size: 1.52 KB

Contents

$(function() {
$('form a.add_nested_fields').live('click', function() {
  // Setup
  var assoc   = $(this).attr('data-association');           // Name of child
  var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template

  // Make the context correct by replacing new_<parents> with the generated ID
  // of each of the parent objects
  var context = ($(this).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');

  // context will be something like this for a brand new form:
  // project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
  // or for an edit form:
  // project[tasks_attributes][0][assignments_attributes][1]
  if(context) {
    var parent_names = context.match(/[a-z_]+_attributes/g) || [];
    var parent_ids   = context.match(/[0-9]+/g);

    for(i = 0; i < parent_names.length; i++) {
      if(parent_ids[i]) {
        content = content.replace(
          new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
          '$1[' + parent_ids[i] + ']'
        )
      }
    }
  }

  // Make a unique ID for the new child
  var regexp  = new RegExp('new_' + assoc, 'g');
  var new_id  = new Date().getTime();
  content     = content.replace(regexp, new_id);

  $(this).before(content);
  return false;
});

$('form a.remove_nested_fields').live('click', function() {
  var hidden_field = $(this).prev('input[type=hidden]')[0];
  if(hidden_field) {
    hidden_field.value = '1';
  }
  $(this).closest('.fields').hide();
  return false;
});
});

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
nested_form_tougg-0.0.0 lib/generators/nested_form/templates/nested_form.js
mdd-2.0.1 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-2.0 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-1.1.0 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-1.0.3 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-1.0.2 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-1.0.1 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
mdd-1.0 lib/generators/mdd/sandbox/templates/app/assets/javascripts/mdwa/template/nested_form.js
nested_form-0.0.0 lib/generators/nested_form/templates/jquery_nested_form.js