首页 > 其他分享 >SAP Fiori Elements 应用里的 visitor 访问者设计模式

SAP Fiori Elements 应用里的 visitor 访问者设计模式

时间:2023-07-29 18:22:24浏览次数:29  
标签:Elements name oAttribute visitor value visitAttribute 设计模式 oElement 访问者

SAP Fiori Elements 应用 SmartTable title 控件的 text 属性,在运行时如何生成的?

在 TitleRenderer.js 里设置断点:

调用 getText 拿到 text property 值:

问题是这个 Text 属性是从哪里来的?

SmartTable.js 内部能够观察到这个 Products 字符串:

最终的入口还是 XMLTemplateProcessor.js:

应该是这些 XML fragment 加载触发的:

把 smarttable.fragment.xml 下载到本地打开,观察其 header 绑定信息:

{= !${parameter>/settings/quickVariantSelection/showCounts} ? ${path: 'header>TypeNamePlural', formatter: 'sap.ui.model.odata.AnnotationHelper.format'} : '' }

根据关键字 TypeNamePlural 进行搜索:

设置了断点,一个也没有触发。
PropertyBinding:

一个 visitor 模式:

源代码:

/**
			 * Visits the given attribute of the given element. If the attribute value represents a
			 * binding expression that can be resolved, it is replaced with the resulting value.
			 *
			 * @param {Element} oElement the XML DOM element
			 * @param {Attr} oAttribute one of the element's attribute nodes
			 * @param {sap.ui.core.template._with} oWithControl the "with" control
			 * @returns {sap.ui.base.SyncPromise}
			 *   A sync promise which resolves with <code>undefined</code> as soon as the
			 *   attribute's value has been replaced, or is rejected with a corresponding error if
			 *   getting the binding's value fails.
			 */
			function visitAttribute(oElement, oAttribute, oWithControl) {
				if (fnSupportInfo) {
					fnSupportInfo({context:undefined /*context from node clone*/, env:{caller:"visitAttribute", before: {name: oAttribute.name, value: oAttribute.value}}});
				}
				return resolveAttributeBinding(oElement, oAttribute, oWithControl)
					.then(function () {
						if (fnSupportInfo) {
							fnSupportInfo({context:undefined /*context from node clone*/, env:{caller:"visitAttribute", after: {name: oAttribute.name, value: oAttribute.value}}});
						}
					});
			}

			/**
			 * Visits all attributes of the given element. If an attribute value represents a
			 * binding expression that can be resolved, it is replaced with the resulting value.
			 *
			 * @param {Element} oElement the XML DOM element
			 * @param {sap.ui.core.template._with} oWithControl the "with" control
			 * @returns {sap.ui.base.SyncPromise}
			 *   A sync promise which resolves with <code>undefined</code> as soon as all
			 *   attributes' values have been replaced, or is rejected with a corresponding error if
			 *   getting some binding's value fails.
			 */
			function visitAttributes(oElement, oWithControl) {
				/*
				 * Comparator for DOM attributes by name.
				 *
				 * @param {Attr} oAttributeA
				 * @param {Attr} oAttributeB
				 * @returns {number} <0, 0, >0
				 */
				function comparator(oAttributeA, oAttributeB) {
					return oAttributeA.name.localeCompare(oAttributeB.name);
				}

				return stopAndGo(
					// Note: iterate over a shallow copy to account for removal of attributes!
					// Note: sort attributes by name to achieve a stable log order across browsers
					Array.prototype.slice.apply(oElement.attributes).sort(comparator),
					function (oAttribute) {
						return visitAttribute(oElement, oAttribute, oWithControl);
					});
			}

这段代码中所使用的设计模式为访问者模式(Visitor pattern)。

访问者模式是一种将算法与对象结构分离的设计模式。这种模式在对象结构中的元素上定义一个访问操作,该操作以一个访问者作为参数,访问者在该操作中实现了一个针对该元素的操作。

在这段代码中,visitAttributevisitAttributes 函数就充当了访问者的角色。它们对 XML DOM 元素及其属性进行访问,解析属性中可能存在的绑定表达式,并将解析后的结果替换原始属性值。

例如,visitAttribute 函数访问单个属性,通过 resolveAttributeBinding 解析可能的绑定表达式,并通过 Promise 结构异步替换属性值。而 visitAttributes 函数则访问元素的所有属性,通过调用 visitAttribute 对每个属性进行处理。

这种通过访问者访问和处理元素的方式,将元素与处理逻辑分离,增加了代码的可扩展性和复用性。当需要对元素进行新的操作时,只需要定义新的访问者,而无需修改元素类。同时,访问者模式也使得添加新的操作变得更简单,只需添加一个新的访问者即可。

这段代码还体现了访问者模式的另一特性,即访问者可以累积状态。在这段代码中,访问者通过 Promise 结构返回处理结果,这可以看作是访问者在访问过程中累积的状态。通过 Promise 结构,可以方便地处理异步操作和错误,这也是访问者模式的一种常见应用。

标签:Elements,name,oAttribute,visitor,value,visitAttribute,设计模式,oElement,访问者
From: https://www.cnblogs.com/sap-jerry/p/17590250.html

相关文章

  • SAP Fiori Elements 本地 annotation.xml 里的一个代码片段
    下面是从SAPUI5FioriElements应用本地注解文件摘录出来的xml片段,这些代码的含义是:<AnnotationsTarget="SEPMRA_PROD_MAN.SEPMRA_PROD_MAN_Entities/SEPMRA_C_PD_Product"xmlns="http://docs.oasis-open.org/odata/ns/edm"><AnnotationTerm=&......
  • SAP Fiori Elements SemanticKey 的语法
    annotation本地文件的xml片段如下:<AnnotationsTarget="SEPMRA_PROD_MAN.SEPMRA_C_PD_ProductType"xmlns="http://docs.oasis-open.org/odata/ns/edm"><AnnotationTerm="Common.SemanticKey">......
  • SAP Fiori Elements propertyAnnotations 举例讲解
    SAPFioriElements是SAP提供的一种用户界面开发框架,它允许开发人员创建符合Fiori设计原则的应用程序。这种框架主要基于SAPUI5,它是SAP的一种基于HTML5的用户界面技术。FioriElements提供了一种声明性的方式来定义用户界面,这意味着开发人员不需要编写大量的前端代码。......
  • SAP Fiori Elements 应用 OData 元数据请求 url 里的模型名称决定逻辑
    问题我用yarnstart本地启动一个SAPFioriElements应用,在Chrome开发者工具network面板,观察到一个ODatametadata请求的url如下:http://localhost:8080/sap/opu/odata/sap/SEPMRA_PROD_MAN/$metadata?sap-value-list=none&sap-language=EN这个OData服务名称SEPM......
  • 如何在 SAP Fiori Elements 应用的 manifest.json 里定义注解
    在SAPFioriElements应用中,manifest.json文件是应用的主要配置文件,其中定义了应用的元数据、模型、服务等信息。其中,dataSources区域负责描述应用使用的数据源,比如OData服务或者其他类型的后端服务。在dataSources区域的settings字段中,我们可以定义annotations,它们是......
  • 关于 Fiori Elements 应用 manifest.json 的 localURI 字段
    SAPFioriElements是一个开发框架,它提供了一种创建SAPFiori应用的标准化方法。这些应用具有一致的用户体验,并且具有适应各种设备和屏幕尺寸的能力。SAPFioriElements的设计目标是简化开发过程,降低维护成本,并提高应用的质量。在SAPFioriElements应用中,manifest.json文件用于......
  • SAP Fiori Elements 应用加载时的 url 参数 sap-ui-xx-viewCache=false
    SAPFioriElements是SAP提供的一种UI技术,其主要目的是提供一种快速、简单、一致且易于维护的方式来开发SAP用户界面。而sap-ui-xx-viewCache=false是一个URL参数,用于控制FioriElements应用的视图缓存。在SAPFioriElements中,视图缓存是一个可以提高应用性能的......
  • Java设计模式-策略模式
    简介策略模式是指有一定行动内容的相对稳定的策略名称,策略模式作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不同的实现算法经验总结抽象策略角色:策略类,通常由一个接口或者抽象类实现具体策略角色:包装了相关的算法和行为环境角色:持有一个策略类的引用,最终给客......
  • 设计模式-备忘录模式在Java中使用示例-象棋悔棋
    场景备忘录模式备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,当前很多软件都提供了撤销(Undo)操作,其中就使用了备忘录模式。备忘录模式结构图 在备忘录模式结构......
  • 设计模式-中介者模式在Java中使用示例-客户信息管理
    场景欲开发客户信息管理窗口界面,界面组件之间存在较为复杂的交互关系:如果删除一个客户,要在客户列表(List)中删掉对应的项,客户选择组合框(ComboBox)中客户名称也将减少一个;如果增加一个客户信息,客户列表中需增加一个客户,且组合框中也将增加一项。中介者模式概述如果在一个系统......