【问题标题】:Solution for SAPUI5 error message because of templateShareable:true?由于 templateShareable:true 导致 SAPUI5 错误消息的解决方案?
【发布时间】:2015-11-02 12:15:04
【问题描述】:

自从升级 SAPUI5 1.28.20 后,我收到以下错误消息:

共享模板必须在 绑定信息

代码在 MangedObject.js 中,如下所示:

        } else if ( oBindingInfo.templateShareable === MAYBE_SHAREABLE_OR_NOT ) {
            // a 'clone' operation implies sharing the template (if templateShareable is not set to false)
            oBindingInfo.templateShareable = oCloneBindingInfo.templateShareable = true;
            jQuery.sap.log.error("A shared template must be marked with templateShareable:true in the binding info");
        }

oBindingInfo.templateShareable 的值为 trueMAYBE_SHAREABLE_OR_NOT 的值为 1。

根据文档,oBindingInfo.templateShareable 的默认值是 true

那么这里有什么问题呢?库中的错误?或者我的代码有什么? 另见:https://sapui5.netweaver.ondemand.com/sdk/#docs/api/symbols/sap.ui.base.ManagedObject.html

SAPUI5 1.32.x 版更新

在 1.32.x 版本中,消息已更改为:

一个模板在绑定中被重用,但已经被标记为 毁灭的候选人。你最好应该声明这样的用法 templateShareable:true 在绑定配置中。 -

但根据文档,默认值仍应为 true:

{boolean} oBindingInfo.templateShareable?,默认值:true 选项 启用模板将被共享,这意味着它不会 自动销毁或克隆

现在看起来,这会产生一些无休止的加载,我一次又一次地收到这条消息,直到浏览器崩溃。 有人知道可能出了什么问题吗?

【问题讨论】:

    标签: data-binding sapui5


    【解决方案1】:

    看起来如果模板是在绑定之外实例化的,则会出现该消息。示例:

    此代码将起作用:

    new sap.m.Select({
        items : {
            path : "/Items",
            template : new sap.ui.core.Item({
                text : "{Name}"
            }) 
        }
    }) 
    

    这段代码似乎产生了消息:

    var oTemplate = new sap.ui.core.Item({
        text : "{Name}"
    }) 
    
    
    new sap.m.Select({
        items : {
            path : "/Items",
            template :oTemplate
        }
    })
    

    这似乎解决了问题:

    var oTemplate = new sap.ui.core.Item({
        text : "{Name}"
    }) 
    
    
    new sap.m.Select({
        items : {
            path : "/Items",
            template :oTemplate,
            templateShareable : true
        }
    })
    

    【讨论】:

    • 我在下面的代码中仍然遇到同样的问题,因为我的 XML 片段的下拉列表(选择)也缺少 'templateShareable' listView.bindItems({ path: sDimAttrPath, template: sap. ui.xmlfragment("sap.es.view.BoostItem"), templateShareable: true }); XML 片段: ...
    • 我遇到了相反的问题。从 Fiori 启动板第二次进入应用程序时,它给了我 Error: added element with duplicate id ... for a template with an ID。使用items="{..., templateShareable: false}" 解决了这个问题。 (v1.26)
    【解决方案2】:

    上面标记为正确的答案实际上根本不正确,因为这里是错误的:

    如果模板在外部实例化,看起来会出现消息 绑定。 [...]这段代码似乎产生了这样的信息:[...]

    要自己证明上面的答案是错误的,只需运行此示例(SAPUI5 1.28.20 具有相同的结果):

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>SAPUI5 single file template | nabisoft</title>
    
            <script 
                src="https://openui5.hana.ondemand.com/1.36.12/resources/sap-ui-core.js"
                id="sap-ui-bootstrap"
                data-sap-ui-theme="sap_bluecrystal"
                data-sap-ui-libs="sap.m"
                data-sap-ui-compatVersion="edge"
                data-sap-ui-preload="async"></script>
                <!-- use "sync" or change the code below if you have issues -->
     
            <script>
                sap.ui.getCore().attachInit(function () {
                    "use strict";
                    
                    var oModel = new sap.ui.model.json.JSONModel({
                        Items: [
                            {Name: "Michael"},
                            {Name: "John"},
                            {Name: "Frank"},
                            {Name: "Jane"}
                        ]
                    });                
                    sap.ui.getCore().setModel(oModel);
                    
                    var oTemplate = new sap.ui.core.Item({
                        text : "{Name}"
                    });
                  
                    new sap.m.Select({
                        items : {
                            path : "/Items",
                            template : oTemplate
                        }
                    }).placeAt("content");
                });
            </script>
     
        </head>
     
        <body class="sapUiBody">
            <div id="content"></div>
        </body>
    </html>

    基本上,UI5 中缺少(或曾经)模板生命周期的明确定义。当检测到这个问题时,周围已经有很多旧的应用程序......所以现在这个新的“功能”是在去年的某个时候引入的(这是一种向后兼容)。 UI5 尝试自动检测开发人员对绑定中使用的给定模板的生命周期的意图(使用一些启发式方法)。如果 UI5 不能清楚地告诉开发人员真正想要什么,那么您将看到这个错误日志 - 这实际上根本不会影响功能。它只是告诉开发人员,在某处有一个模板不会被 UI5 运行时破坏。换句话说,如果您设置了 templateShareable=true,那么您应该确保销毁模板以避免内存泄漏。所以仅仅设置 templateShareable=true 并不是全部...

    我已经发表了一篇详细的博客:Understanding templateShareable in SAPUI5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 2016-04-28
      相关资源
      最近更新 更多