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).
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5280 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -489,20 +489,37 @@ OpenLayers.Util.upperCaseObject = function (object) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Function: applyDefaults
|
* Function: applyDefaults
|
||||||
* Takes a hashtable and copies any keys that don't exist from
|
* Takes an object and copies any properties that don't exist from
|
||||||
* another hashtable, by analogy with OpenLayers.Util.extend() from
|
* another properties, by analogy with OpenLayers.Util.extend() from
|
||||||
* Prototype.js.
|
* Prototype.js.
|
||||||
*
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* to - {Object}
|
* to - {Object} The destination object.
|
||||||
* from - {Object}
|
* from - {Object} The source object. Any properties of this object that
|
||||||
|
* are undefined in the to object will be set on the to object.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* {Object} A reference to the to object. Note that the to argument is modified
|
||||||
|
* in place and returned by this function.
|
||||||
*/
|
*/
|
||||||
OpenLayers.Util.applyDefaults = function (to, from) {
|
OpenLayers.Util.applyDefaults = function (to, from) {
|
||||||
for (var key in from) {
|
for (var key in from) {
|
||||||
if (to[key] == null) {
|
if (to[key] === undefined ||
|
||||||
|
(from.hasOwnProperty
|
||||||
|
&& from.hasOwnProperty(key) && !to.hasOwnProperty(key))) {
|
||||||
to[key] = from[key];
|
to[key] = from[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* IE doesn't include the toString property when iterating over an object's
|
||||||
|
* properties with the for(property in object) syntax. Explicitly check if
|
||||||
|
* the source has its own toString property.
|
||||||
|
*/
|
||||||
|
if(from.hasOwnProperty
|
||||||
|
&& from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) {
|
||||||
|
to.toString = from.toString;
|
||||||
|
}
|
||||||
|
|
||||||
return to;
|
return to;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -141,18 +141,21 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_06_Util_applyDefaults(t) {
|
function test_Util_applyDefaults(t) {
|
||||||
|
|
||||||
t.plan(8);
|
t.plan(10);
|
||||||
|
|
||||||
var to = {
|
var to = {
|
||||||
'a': "abra",
|
'a': "abra",
|
||||||
'b': "blorg"
|
'b': "blorg",
|
||||||
|
'n': null
|
||||||
};
|
};
|
||||||
|
|
||||||
var from = {
|
var from = {
|
||||||
'b': "zoink",
|
'b': "zoink",
|
||||||
'c': "press"
|
'c': "press",
|
||||||
|
'toString': function() {return 'works'},
|
||||||
|
'n': "broken"
|
||||||
};
|
};
|
||||||
|
|
||||||
OpenLayers.Util.applyDefaults(to, from);
|
OpenLayers.Util.applyDefaults(to, from);
|
||||||
@@ -167,6 +170,8 @@
|
|||||||
t.eq( ret["a"], "abra", "key present in ret but not from maintained");
|
t.eq( ret["a"], "abra", "key present in ret but not from maintained");
|
||||||
t.eq( ret["b"], "blorg", "key present in ret and from, maintained in ret");
|
t.eq( ret["b"], "blorg", "key present in ret and from, maintained in ret");
|
||||||
t.eq( ret["c"], "press", "key present in from and not ret successfully copied to ret");
|
t.eq( ret["c"], "press", "key present in from and not ret successfully copied to ret");
|
||||||
|
t.eq(to.toString(), "works", "correctly applies custom toString");
|
||||||
|
t.eq(to.n, null, "correctly preserves null");
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_07_Util_getParameterString(t) {
|
function test_07_Util_getParameterString(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user