3
votes

I've noticed that in the metadata there's an object entityType but also an object enumType.

We use manager.metadataStore.getEntityType() to access the metadata of an Entity.

How can we do it for a given enum ? How would I create the enum on the client side out of the metadata ?

Also, when I assign an enum value to a property, I'd like to to it by name instead of by value.

For instance, assuming that Status is of type myEnum:

myEntity.Status = myEnum.Valid;

instead of

myEntity.Status = 1;

Does breeze have any helper function to access the values of an enum ?

3
anyone for that ? Is my question not clear enough maybe ?Sam

3 Answers

2
votes

This issue is still open as I write. But you might want to take a look at the work-around described in the answer to this SO question.

0
votes

I am assuming that you are talking about data properties that are defined as .NET enums on the server, and you want additional metadata about these properties to be made available on the Breeze client.

Unfortunately, Breeze does not yet support any metadata on enum types other than the name of the .NET type backing the enum value. This is the 'enumType' property that will appear on any dataProperty that is backed by an .NET Enum on the server. (We do need to document this better)

Please add a feature request for this to the Breeze User Voice. It's a good idea and we do take these suggestions very seriously.

0
votes

Well this is not exact solution to your question but definitely can help people who are generating metadata offline. I am using NancyFx(No EF) + Breeze + AngularJS for my web project and generating breeze metadata offline(using EF methods at development) and then using it in js file.
I also encountered similar situation where I want to get all Enum values to bind dropdowns and to display EnumName corresponding to EnumValue(Id). I searched over net but there was not much as per my scenario.

So I have written raw JS methods 1. To extract all enums and their values(Id & Name) in a JS dictionary(associated array) from metadata.

 var enumDictionary = {};

        JSON.parse(window.app.metadata).schema.enumType.forEach(function (enumType) {
            var newEnumValues = [];

            enumType.member.forEach(function (enumValue) {
                var newEnumValue = { id: enumValue.value, name: enumValue.name };
                newEnumValues.push(newEnumValue);
            });

            enumDictionary[enumType.name] = newEnumValues;
        });
  1. I created a method to get all enum values for a specific enum. This will be used for binding a dropdown.

    function GetEnumDictionary(enumName) {
    return enumDictionary[enumName];
    } 
    
  2. Another method I created to get specific Enum name on basis of value.

    function GetEnumDictionaryValue(enumName, enumValueId) {
     var result = null;
     enumDictionary[enumName].some(function (enumValue) {
     if (enumValue.id == enumValueId) {
        result = enumValue.name;
        return;
    }
    });
    
      return result;
     }