Suppose I have an entity Person and string defining a "path" to property - let's say 'Address.Country'. Is there a function that will let me access the observable holding Country?
-
What have you tried? What is not working? Have you read help about asking questions here?Krzysztof Cieslak– Krzysztof Cieslak2013-11-07 15:20:24 +00:00Commented Nov 7, 2013 at 15:20
-
I tried to find such function in the breeze's docs and Api, without luck. It looks like there is no such function available, but it's very likely that breeze performs something similar internally. I can write a function that will do what I want, but would prefer to use one if it's available.pawel– pawel2013-11-07 15:41:16 +00:00Commented Nov 7, 2013 at 15:41
Add a comment
|
1 Answer
You can start with the getProperty and setProperty methods.
var address = myEntity.getProperty("Address");
var country = address.getProperty("Country");
Then you could use
function getPropertyPathValue(obj, propertyPath) {
var properties;
if (Array.isArray(propertyPath)) {
properties = propertyPath;
} else {
properties = propertyPath.split(".");
}
if (properties.length === 1) {
return obj.getProperty(propertyPath);
} else {
var nextValue = obj;
for (var i = 0; i < properties.length; i++) {
nextValue = nextValue.getProperty(properties[i]);
// == in next line is deliberate - checks for undefined or null.
if (nextValue == null) {
break;
}
}
return nextValue;
}
}
The 'propertyPath' param can be either an array of string or a '.' delimited path
var country = getPropertyPath(myEntity, "Address.Country");
or
var country = getPropertyPath(myEntity, ["Address", "Country"]);