Create Custom Enum Type

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I want to create a custom enum type for the custom control like https://sapui5.hana.ondemand.com/docs/api/symbols/sap.ui.core.ValueState.html#.Error.

My questions are:

  • How can I create an enum type?
  • On the custom control, you will be able only to pass property enum
    type. How can I validate if the given enum is valid or not?

回答1:

In order to create an enum type in UI5, there are certain rules to take into account:

  • The enum definition must be a plain object. Internally, it's validated via jQuery.isPlainObject.
  • Each key-value pair must be identical to each other.
  • Renaming is not supported.
  • Only keys and values of type string are supported.

Example:

{   Red: "Red",   Yellow: "Yellow",   Blue: "Blue" }

That being said, in order to actually make use of the enum object:

  1. The enum object must be first globally accessible. One way to do this is to define a module that consists of a plain object and making it available under the module name. For example:

    /** * file: PrimaryColor.js * path: "custom/control/type/enum/" * namespace: "demo" */ sap.ui.define({ // module value   Red: "Red",   Yellow: "Yellow",   Blue: "Blue", }, true); // resulting module name: "demo.custom.control.type.enum.PrimaryColor"
  2. The module name of the object must be assigned to the type in the property definition:

    sap.ui.define([   "sap/ui/core/Control",   "./MyColorPickerRenderer",   "./type/enum/PrimaryColor", // Defines the module and prevents fetching it via sync XHR ], function(Control) {   "use strict";    return Control.extend("demo.custom.control.ColorPicker", {     metadata: {       properties: {         "selectedColor": {           type: "demo.custom.control.type.enum.PrimaryColor"         }       }     },     // ...   }); });

In my example above, it's the selectedColor property that awaits only "Red", "Yellow", or "Blue". Let's test it:

  • new ColorPicker().getMetadata().getProperty("selectedColor").getType().isEnumType() returns true (getType returns the created sap.ui.base.DataType
  • new ColorPicker().setSelectedColor("Hans") throws an error as expected:

  • new ColorPicker().setSelectedColor("Yellow")

Note

Do not try to create an enum type via DataType.create.

Array types and enumeration types cannot be created with this method. They're created on-the-fly by DataType.getType when such a type is looked up. [source]

References



回答2:

Start off by defining your enum...

MessageType.js

sap.ui.define([], function() {     "use strict";      return {       Unread: "Unread",       Read: "Read"     }; });

Next, mark this enum as a dependency in your custom control so that you are able to validate the value.

MyControl.js

    sap.ui.define(["sap/ui/core/Control", "/path/to/MessageType.js"], function(Control, MessageType) {          Control.extend("myControl", {             someMethod: function(sMessageType) {                 // Validate if we are dealing with a valid message type                 var aKeys = Object.keys(MessageType);                 var bValidEnumValue = aKeys.some(function(sKey) {                     if (MessageType[sKey]) {                         return true;                     }                 });                  // Do other stuff..             }         });     });

Of course the way to check if you are dealing with a valid enum value can be implemented in different ways depending on what you're trying to do.

if (sMessageType === MessageType.Read) {    // do something } else if (sMessageType === MessageType.Unread) {    // do something else  } else {    // throw an error? }


转载请标明出处:Create Custom Enum Type
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!