OpenLayers OpenLayers

Changeset 5280

Show
Ignore:
Timestamp:
11/26/07 23:34:50 (1 year ago)
Author:
tschaub
Message:

applyDefaults now respects null - if you want to override a property with applyDefaults, set it to undefined first - applyDefaults also now correctly applies a custom toString method (closes #1063).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Util.js

    r5162 r5280  
    490490/**  
    491491 * Function: applyDefaults 
    492  * Takes a hashtable and copies any keys that don't exist from 
    493  *     another hashtable, by analogy with OpenLayers.Util.extend() from 
     492 * Takes an object and copies any properties that don't exist from 
     493 *     another properties, by analogy with OpenLayers.Util.extend() from 
    494494 *     Prototype.js. 
    495495 *  
    496496 * Parameters: 
    497  * to - {Object} 
    498  * from - {Object} 
     497 * to - {Object} The destination object. 
     498 * from - {Object} The source object.  Any properties of this object that 
     499 *     are undefined in the to object will be set on the to object. 
     500 * 
     501 * Returns: 
     502 * {Object} A reference to the to object.  Note that the to argument is modified 
     503 *     in place and returned by this function. 
    499504 */ 
    500505OpenLayers.Util.applyDefaults = function (to, from) { 
    501506    for (var key in from) { 
    502         if (to[key] == null) { 
     507        if (to[key] === undefined || 
     508            (from.hasOwnProperty 
     509             && from.hasOwnProperty(key) && !to.hasOwnProperty(key))) { 
    503510            to[key] = from[key]; 
    504511        } 
    505512    } 
     513    /** 
     514     * IE doesn't include the toString property when iterating over an object's 
     515     * properties with the for(property in object) syntax.  Explicitly check if 
     516     * the source has its own toString property. 
     517     */ 
     518    if(from.hasOwnProperty 
     519       && from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) { 
     520        to.toString = from.toString; 
     521    } 
     522     
    506523    return to; 
    507524}; 
  • trunk/openlayers/tests/test_Util.html

    r5162 r5280  
    142142    } 
    143143 
    144     function test_06_Util_applyDefaults(t) { 
    145      
    146         t.plan(8); 
     144    function test_Util_applyDefaults(t) { 
     145     
     146        t.plan(10); 
    147147         
    148148        var to = {  
    149149            'a': "abra", 
    150             'b': "blorg" 
     150            'b': "blorg", 
     151            'n': null 
    151152        }; 
    152153 
    153154        var from = {  
    154155            'b': "zoink", 
    155             'c': "press" 
     156            'c': "press", 
     157            'toString': function() {return 'works'}, 
     158            'n': "broken" 
    156159        }; 
    157160 
     
    168171        t.eq( ret["b"], "blorg", "key present in ret and from, maintained in ret"); 
    169172        t.eq( ret["c"], "press", "key present in from and not ret successfully copied to ret"); 
     173        t.eq(to.toString(), "works", "correctly applies custom toString"); 
     174        t.eq(to.n, null, "correctly preserves null"); 
    170175    } 
    171176