Browse files

refactored jquery javascript files, now you can override how template…

…s will be inserted inside form
  • Loading branch information...
1 parent 89953fa commit 15c4fef848394803c4e3390530d607f0a60295fe @fxposter fxposter committed Jun 19, 2011
Showing with 62 additions and 44 deletions.
  1. +62 −44 lib/generators/nested_form/templates/jquery_nested_form.js
View
106 lib/generators/nested_form/templates/jquery_nested_form.js
@@ -1,51 +1,69 @@
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] + ']');
+ window.NestedFormEvents = function() {
+ this.addFields = $.proxy(this.addFields, this);
+ this.removeFields = $.proxy(this.removeFields, this);
+ };
+
+ NestedFormEvents.prototype = {
+ addFields: function(e) {
+ // Setup
+ var link = e.currentTarget;
+ var assoc = $(link).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 = ($(link).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 parentNames = context.match(/[a-z_]+_attributes/g) || [];
+ var parentIds = context.match(/(new_)?[0-9]+/g) || [];
+
+ for(var i = 0; i < parentNames.length; i++) {
+ if(parentIds[i]) {
+ content = content.replace(
+ new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'),
+ '$1_' + parentIds[i] + '_');
+
+ content = content.replace(
+ new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
+ '$1[' + parentIds[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;
- });
+ // 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);
- $('form a.remove_nested_fields').live('click', function() {
- var hidden_field = $(this).prev('input[type=hidden]')[0];
- if(hidden_field) {
- hidden_field.value = '1';
+ var field = this.insertFields(content, assoc, link);
+ $(link).closest("form")
+ .trigger({ type: 'nested:fieldAdded', field: field })
+ .trigger({ type: 'nested:fieldAdded:' + assoc, field: field });
+ return false;
+ },
+ insertFields: function(content, assoc, link) {
+ return $(content).insertBefore(link);
+ },
+ removeFields: function(e) {
+ var link = e.currentTarget;
+ var hiddenField = $(link).prev('input[type=hidden]')[0];
+ if (hiddenField) {
+ hiddenField.value = '1';
+ }
+ var field = $(link).closest('.fields');
+ field.hide();
+ $(link).closest("form").trigger({ type: 'nested:fieldRemoved', field: field });
+ return false;
}
- $(this).closest('.fields').hide();
- $(this).closest("form").trigger('nested:fieldRemoved');
- return false;
- });
+ };
+
+ window.nestedFormEvents = new NestedFormEvents();
+ $('form a.add_nested_fields').live('click', nestedFormEvents.addFields);
+ $('form a.remove_nested_fields').live('click', nestedFormEvents.removeFields);
});

0 comments on commit 15c4fef

Please sign in to comment.