SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

What is smart field

According to SAP help, “Smart controls are a specific category of SAPUI5 controls that have some special features in addition to the standard SAPUI5 features and thus make it easier to use the control in certain scenarios. One important building block of smart controls is the SmartField control that, depending on the OData metadata defined, allows you to renders other controls and, for example, define fields with certain attributes based on the metadata.”

In xml view definition, only one SmartLabel and one SmartField are declared.

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

However, in rendered UI, there is additional currency field displayed as well. This is the meaning of “Smart”.

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

How SmartField works

(1) SmartField which is bound to Price field is rendered as usual:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

(2) The mock data, Products.json, will be loaded by Mock server and once it is available, success callback is triggered.

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

(3) In callback, setElementBindingContext will trigger initialization logic of SmartField:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理
SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

(4) Here ODataControlFactory will try to get all sap defined annotation from OData Metadata,

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

The picture above highlights some of sap annotations. For a complete list of them, please refer to this link.

Parse logic of all annotations could be find from AnnotationHelper.get. The example below introduces how sap:unit annotation is parsed.

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理
SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

The string “CurrencyCode” is returned:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

At this time, metadata for currency UI element is available and it is ready for creation:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

(5) UI element for currency code display is created here:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

And CSS class sapUiCompSmartFieldUnit is added:

SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理

要获取更多Jerry的原创文章,请关注公众号"汪子熙":
SAP Fiori Elements原理介绍之类型为Currency的Smart Field工作原理