Adding an args argument to OpenLayers.String.format. This lets you set context properties as functions that will be executed with the given arguments where tokens match. For styles, this means you can specify a context that contains functions that return some value based on the feature being styled. See the styles-context.html example for use. r=ahocevar (closes #1434)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@6512 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-03-12 22:24:33 +00:00
parent 7a25a14f93
commit 106e73618a
4 changed files with 263 additions and 11 deletions
+26 -1
View File
@@ -89,7 +89,7 @@
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
"}", "${${} }"
]
t.plan(4 + unchanged.length);
t.plan(6 + unchanged.length);
var format = OpenLayers.String.format;
@@ -114,6 +114,31 @@
var context = {bar: "foo", foo: "bar"};
t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
"multiple properties replaced correctly");
// test context with properties that are functions
var context = {
bar: "church",
getDrunk: function() {
return arguments[0];
}
};
t.eq(
format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]),
"I go to the church to eat pretzels.",
"function correctly called in context with arguments"
);
// test that things don't break
var context = {
meaning: function(truth) {
return truth;
}
};
t.eq(
format("In life, truth is ${meaning}.", context),
"In life, truth is undefined.",
"still works if arguments are not supplied"
);
}