Sha256: f6c5b4695f6484dfc22928d0a446211aad9881c9395423b0fc3dae96459ed921

Contents?: true

Size: 1.92 KB

Versions: 31

Compression:

Stored size: 1.92 KB

Contents

jQuery(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][new_1255929127459][assignments_attributes][new_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(/(new_)?[0-9]+/g) || [];

      for(var i = 0; i < parent_names.length; i++) {
        if(parent_ids[i]) {
          content = content.replace(
            new RegExp('(_' + parent_names[i] + ')_.+?_', 'g'),
            '$1_' + 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_" + new_id);

    var field = $(content).insertBefore(this);
    $(this).closest("form").trigger({type: 'nested:fieldAdded', field: field});
    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();
    $(this).closest("form").trigger('nested:fieldRemoved');
    return false;
  });
});

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
nimbleshop_core-0.0.23 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.21 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.20 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.19 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.17 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.16 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.15 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.14 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.14.rc2 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.14.rc1 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.13 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.12 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.11 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.10 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.9 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.8 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.7 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.5 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.4.beta1 vendor/assets/javascripts/nested_form.js
nimbleshop_core-0.0.4 vendor/assets/javascripts/nested_form.js