从视图调用 SAPUI5 控件有效,嵌入在 XMLComposite 控件中则无效(全局外部库)



我正在尝试构建一个amCharts容器控件,一个UI5控件,其中包含通过amCharts构建的图形。一般来说,它可以工作,但我相信你可以发现很多可以做得更好的黑客。

我目前最大的问题是,当我从另一个XMLComposite控件中使用该控件时,我收到错误

未捕获的引用错误:am4core 未在 f.onAfterRendering 中定义 (AmChartContainer.js?eval:38(

调试之前执行jQuery.sap.includeScript的代码证明,全局元素am4core仍然不可用。当我将控件直接嵌入到视图中时,它可以工作。

我不满意的另一件事是事件顺序。很想在渲染方法中实例化 amchart,但当时没有 htmlelement,这是 amchart 实例化所需要的。同意?

将不胜感激任何指示!

sap.ui.define([ 'sap/ui/core/Control', "jquery.sap.global" ], function(Control, jQuery) {
return Control.extend("io.rtdi.amChartContainer", {
metadata: {
properties: {
width: {
type: "sap.ui.core.CSSSize",
defaultValue: "100%"
},
height: {
type: "sap.ui.core.CSSSize",
defaultValue: "100%"
},
plugin: {
type: "string"
}
},
aggregations : {},
},
renderer : function(oRm, oControl) {
oRm.write("<div");
oRm.write(" style="width: " + oControl.getWidth() + 
"; height: " + oControl.getHeight() + ";" ");
oRm.writeControlData(oControl);
oRm.write(">");
oRm.write("</div>");
},
onBeforeRendering : function() {
jQuery.sap.includeScript("https://www.amcharts.com/lib/4/core.js",
"amCharts.core", null, null);
jQuery.sap.includeScript("https://www.amcharts.com/lib/4/charts.js",
"amCharts.charts", null, null);
if (!!this.getPlugin()) {
jQuery.sap.includeScript(
"https://www.amcharts.com/lib/4/" + this.getPlugin(), 
"amCharts.plugin", null, null);
}
},
onAfterRendering : function() {
// if I need to do any post render actions, it will happen here
if (sap.ui.core.Control.prototype.onAfterRendering) {
sap.ui.core.Control.prototype.onAfterRendering.apply(this, arguments);
}
this._chart = am4core.create(this.getId(), am4plugins_forceDirected.ForceDirectedTree);
},
getChart: function() {
return this._chart;
}
});
});

首先,请使用 sap/ui/dom/includeScript,因为 jQuery.sap.includeScript 被认为是不推荐使用的。

您没有定义任何 fnLoadCallback。你是否100%确定,当调用onAfterRendering时,脚本已经加载并且am4core已经定义?

似乎一开始的全局关键字是缺失的部分:

/* global am4core:true */
/* global am4charts:true */
sap.ui.define([
"sap/ui/core/mvc/Controller",...

最新更新