0

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?

2
  • What have you tried? What is not working? Have you read help about asking questions here? Commented 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. Commented Nov 7, 2013 at 15:41

1 Answer 1

1

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"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.