Author: david
Date: Tue Dec  8 16:42:00 2009
New Revision: 4121

Log:
Specify input and output patterns for field names.

Modified:
   trunk/web/js/dialog.js

Modified: trunk/web/js/dialog.js
==============================================================================
--- trunk/web/js/dialog.js      Tue Dec  8 15:17:33 2009        (r4120)
+++ trunk/web/js/dialog.js      Tue Dec  8 16:42:00 2009        (r4121)
@@ -3,7 +3,7 @@
 function submitDialog(formData, dialog, i)
 {
   var cachedData = $('body').data('cachedData');
-  
+
   dialog.loadData(formData, i);
 
   // cache event data locally
@@ -13,28 +13,49 @@
   return dialog;
 }
 
-function appendHiddenFields(thisForm)
+function appendHiddenFields(thisForm, options)
 {
   // Build hidden form input fields
   var newHiddens = '';
   var cachedData = $('body').data('cachedData');
   //var deleteEvents = $('body').data('deleteEvents');
   var i = 0;
-
+  
   for(var key in cachedData)
   {
     var thisData = cachedData[key];
+    var name = '';
 
-    // Convert all event data into a hidden input fields
-    for (var name in thisData.fields)
+    // Don't include "id" if it's a "new" object
+    if (null != thisData.id && 'new' != thisData.id.substr(0,3))
     {
-      // Don't include "id" if parameter == null
-      if ('id' == name && null == thisEvent.data.id)
+      name = 'id'; 
+      if (null != options.outputNameFormat)
       {
-        continue;
+        name = options.outputNameFormat.replace('%s', 'id').replace('%d', i);
       }
+      
+      newHiddens += '<input type="hidden" name="' + name + '" value="' + 
thisData.id + '" />\n';
+    }
+    
+    // Convert all event data into a hidden input fields
+    for (var j in thisData.fields)
+    {
+      var name = j;
 
-      newHiddens += '<input type="hidden" name="relations[' + i + '][' + name 
+ ']" value="' + thisData.fields[name].value + '" />\n';
+      // Format name according to input and output name formats
+      if (null != options.inputNamePattern)
+      {
+        var matches = name.match(options.inputNamePattern);
+        name = (null != matches) ? matches[1] : name;
+      }
+      
+      if (null != options.outputNameFormat)
+      {
+        name = options.outputNameFormat.replace('%s', name).replace('%d', i);
+      }
+      
+      newHiddens += '<input type="hidden" name="' + name + '" value="' + 
thisData.fields[j].value + '" />\n';
     }
     i++;
   }
@@ -76,32 +97,32 @@
         var counter = 0; // Counter
         $('body').data('cachedData', new Array()); // Data cache
         
-        // Build dialog data class
+        // Build object for holding dialog data
         var DialogData = function()
         {
-          var DataObject = this;
+          var obj = this;
           
-          DataObject.id = null;
-          DataObject.fields = [];
+          obj.id = null;
+          obj.fields = [];
           
           $(table).find(':input').each(function() 
           {
-            DataObject.fields[this.name] = { "type": this.type, "value": null }
+            obj.fields[this.name] = { "type": this.type, "value": null }
           } );
          
-          DataObject.loadData = function(data, i) 
+          obj.loadData = function(data, i) 
           {
             // Get a unique id (derive one if form doesn't provide one)
-            DataObject.id = (data['id'] != undefined) ? data['id'] : 'new' + i;
+            obj.id = (data['id'] != undefined) ? data['id'] : 'new' + i;
             
-            for (j in DataObject.fields)
+            for (j in obj.fields)
             {
-              DataObject.fields[j].value = data[j];
+              obj.fields[j].value = data[j];
             }
           }
         }
                
-        // Write data from dialog to informationobject/edit form for 
submission.
+        // Submit dialog data
         var handleSubmit = function() {
           submitDialog(this.getData(), new DialogData, counter++); // Save the 
data to the main form
           this.hide(); // Hide dialog
@@ -131,7 +152,10 @@
         
         // Bind addHiddenFields() to form submit
         thisForm.submit(function () {
-          appendHiddenFields($(this))
+          appendHiddenFields($(this), {
+            "inputNamePattern": /relation\[(\w+)\]/,
+            "outputNameFormat": "relations[%d][%s]"
+          } )
         } );
       })
     }

--

You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.


Reply via email to