util.js tweak

View: New views
2 Messages — Rating Filter:   Alert me  

util.js tweak

by Joe Walker-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Mike,

I noticed that setValues does not respond to { escapeHtml:false } because it ignores all options. I had a hack around and came up with this. What do you think?

/**
 * Given a map, or a recursive structure consisting of arrays and maps, call
 * setValue() for all leaf entries and use intermediate levels to form nested
 * element ids.
 * @see http://getahead.org/dwr/browser/util/setvalues
 */
dwr.util.setValues = function(data, options) {
  if (options == null) options = {};
  if (!options.prefix) {
    options.prefix = options.idPrefix || "";
  }
  dwr.util._setValuesRecursive(data, options);
};

/**
 * @private Recursive helper for setValues()
 */
dwr.util._setValuesRecursive = function(data, options) {
  // Array containing objects -> add "[n]" to prefix and make recursive call
  // for each item object
  if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) {
    for (var i = 0; i < data.length; i++) {
      options.prefix = options.prefix+"["+i+"]";
      dwr.util._setValuesRecursive(data[i], options);
    }
  }
  // Object (not array) -> handle nested object properties
  else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) {
    for (var prop in data) {
      var subidpath = options.prefix ? options.prefix+"."+prop : prop;
      // Object (not array), or array containing objects -> call ourselves recursively
      if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop])
          || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) {
        options.prefix = subidpath;
        dwr.util._setValuesRecursive(data[prop], options);
      }
      // Functions -> skip
      else if (typeof data[prop] == "function") {
        // NOP
      }
      // Only simple values left (or array of simple values, or empty array)
      // -> call setValue()
      else {
        // Are there any elements with that id or name
        if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) {
          dwr.util.setValue(subidpath, data[prop], options);
        }
      }
    }
  }
};

Joe.


RE: util.js tweak

by mikewse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hm, you're absolutely right about that. I'll have a look at it and check in {a|the} fix!
Best regards
Mike


From: joseph.walker@... [mailto:joseph.walker@...] On Behalf Of Joe Walker
Sent: den 23 juni 2008 13:10
To: users@...
Subject: [dwr-user] util.js tweak


Mike,

I noticed that setValues does not respond to { escapeHtml:false } because it ignores all options. I had a hack around and came up with this. What do you think?

/**
 * Given a map, or a recursive structure consisting of arrays and maps, call
 * setValue() for all leaf entries and use intermediate levels to form nested
 * element ids.
 * @see http://getahead.org/dwr/browser/util/setvalues
 */
dwr.util.setValues = function(data, options) {
  if (options == null) options = {};
  if (!options.prefix) {
    options.prefix = options.idPrefix || "";
  }
  dwr.util._setValuesRecursive(data, options);
};

/**
 * @private Recursive helper for setValues()
 */
dwr.util._setValuesRecursive = function(data, options) {
  // Array containing objects -> add "[n]" to prefix and make recursive call
  // for each item object
  if (dwr.util._isArray(data) && data.length > 0 && dwr.util._isObject(data[0])) {
    for (var i = 0; i < data.length; i++) {
      options.prefix = options.prefix+"["+i+"]";
      dwr.util._setValuesRecursive(data[i], options);
    }
  }
  // Object (not array) -> handle nested object properties
  else if (dwr.util._isObject(data) && !dwr.util._isArray(data)) {
    for (var prop in data) {
      var subidpath = options.prefix ? options.prefix+"."+prop : prop;
      // Object (not array), or array containing objects -> call ourselves recursively
      if (dwr.util._isObject(data[prop]) && !dwr.util._isArray(data[prop])
          || dwr.util._isArray(data[prop]) && data[prop].length > 0 && dwr.util._isObject(data[prop][0])) {
        options.prefix = subidpath;
        dwr.util._setValuesRecursive(data[prop], options);
      }
      // Functions -> skip
      else if (typeof data[prop] == "function") {
        // NOP
      }
      // Only simple values left (or array of simple values, or empty array)
      // -> call setValue()
      else {
        // Are there any elements with that id or name
        if (dwr.util.byId(subidpath) != null || document.getElementsByName(subidpath).length >= 1) {
          dwr.util.setValue(subidpath, data[prop], options);
        }
      }
    }
  }
};

Joe.