从上下文访问托管在表单中的WebResource Post Xrm.Page Removal。

问题描述 投票:0回答:1

从表单上的WebResource访问表单的FormContext的最佳方法是什么?

文档中说要 从html WebResource访问表单上下文,使用parent.Xrm.Page。.

添加到表单中的 HTML 网络资源不能使用表单中加载的 JavaScript 库所定义的全局对象。HTML web资源可以通过使用parent.Xrm.Page或parent.Xrm.Utility与表单中的Xrm.Page或Xrm.Utility对象进行交互,但不能使用父库访问表单脚本定义的全局对象。你应该在HTML web资源中加载HTML web资源所需的任何库,这样它们就不会依赖于表单中加载的脚本。

但是Xrm.Page已经被废弃,并将被移除。 当它被移除时,我的假设是parent.Xrm.Page在那个时候也会失效。

这个假设在文档页面最后发布的一个已关闭的问题中得到了回应。

添加到表单中的HTML网络资源不能使用加载的JavaScript库所定义的全局对象,但是如果你看看上面页面上的问题,有人提出了一个明显的问题。 为了防止功能上的差距,当目前唯一支持的方法被废弃时,包含在实体表单上的HTML网络资源将如何访问表单的上下文? 我们是否可以依靠在Xrm.Page被移除时,有一个受支持的替代方法来替代 "parent.Xrm.Page"?

对此,我们的回答是,在Xrm.Page中使用 getContentWindow并从表单的onload中注入。 这仍然不是很好,因为不能保证web资源能及时完成加载,从而能够接受上述值。 一个开发人员甚至这样评论,并在getContentWindow文档页上发布了他们的工作方法。

function form_onload(executionContext) {
    const formContext = executionContext.getFormContext();
    const wrControl = formContext.getControl("new_myWebResource.htm");
    if (wrControl) {
        wrControl.getContentWindow().then(contentWindow => {
            let numberOfCalls = 0;
            let interval = setInterval(() => {
                if (typeof contentWindow.setClientApiContext !== "undefined") {
                    clearInterval(interval);
                    contentWindow.setClientApiContext(Xrm, formContext);
                }
                else
                    //stop interval after 1 minute
                    if (++numberOfCalls > 600) {
                         clearInterval(interval);
                         throw new Error("Content Window failed to initialize.");
                    }
            }, 100);
        });
    }
}

这是目前最好的推荐方法,还是说即使Xrm.Page被删除,parent.Xrm.Page仍然可以工作?

javascript dynamics-crm dynamics-crm-online powerapps-modeldriven
1个回答
1
投票

我使用getContentWindow已经有一段时间了,它确实在等待Web资源的加载。

这个函数在后台做的是检索iframe中的内容,所以我强烈认为这是在等待contentWindow被加载.我的想法是这个contentWindow做的事情和一个Iframe的普通contentWindow完全一样。https:/developer.mozilla.orgen-USdocsWebAPIHTMLIFrameElementcontentWindow。

举个例子,如果你有两个标签,比方说。常规和细节。

如果您在onLoad中设置了getContentWindow,并且在Details选项卡中有您的Web资源,那么只有当Details选项卡被点击时,getContentWindow才会被加载,因此它被渲染为Web资源。

function form_onload(executionContext) {
  const formContext = executionContext.getFormContext();
  const wrControl = formContext.getControl("new_myWebResource.htm");
  if (wrControl) {
    var contentWindow = await wrControl.getContentWindow();
    contentWindow.CallTheFunctionInYourWebResource(Xrm,formContext,...params)
  }
}

在你的Web资源中,你会有这样的内容。

<html>...
<script>
function CallTheFunctionInYourWebResource(_xrm, _formContext) {
  // Do whatever you need to do here
}

</script>

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.