首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用jquery来要求一个需求to模块?

如何使用jquery来要求一个需求to模块?
EN

Stack Overflow用户
提问于 2016-03-31 08:15:58
回答 2查看 648关注 0票数 0

假设我有这个ResourceBundleContext.js文件(requirejs模块):

代码语言:javascript
运行
复制
sap.ui.define([
   "sap/ui/model/resource/ResourceModel"
], function (ResourceModel)
{
    'use strict';
    var resourceBundleContext = function ()
    {
        var i18nModel = new ResourceModel({
            bundleName: "sap/rules/ui/src/sap/rules/ui/lib/parser/i18n.messages_descriptions"
        });
        return {
            getString: function (messageKey, paramsArray)
            {
                var oBundle = i18nModel.getResourceBundle();
                var sMsg = oBundle.getText(messageKey, paramsArray);
                jQuery.sap.log.debug("code: " + messageKey + ", params: " + paramsArray + "\nMessage: " + sMsg);
                return sMsg;
            }
        };
    };
    return new resourceBundleContext;
});

在道路上:

代码语言:javascript
运行
复制
sap.hrf.ui.uilib.js.parser.infrastructure.locale.lib.resourceBundleContext

我想要求上面的模块使用jquery,这将是很好的:

代码语言:javascript
运行
复制
jQuery.sap.require("sap.hrf.ui.uilib.js.parser.infrastructure.locale.lib.resourceBundleContext");

我如何使用jQuery来要求一个需求模块?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-31 09:21:29

这是因为你正在混合‘旧的’同步需求和新的异步AMD模块。您有两个可能的解决方案:

  1. 在您的模块定义中,您必须给出sap.ui.define() true作为最后一个参数,以便将模块导出到全局命名空间,以便您可以在jquery.sap.require()之后访问它。
代码语言:javascript
运行
复制
sap.ui.define([
   "sap/ui/model/resource/ResourceModel"
], function (ResourceModel)
{
    'use strict';
    var resourceBundleContext = function ()
    {
        var i18nModel = new ResourceModel({
            bundleName: "sap/rules/ui/src/sap/rules/ui/lib/parser/i18n.messages_descriptions"
        });
        return {
            getString: function (messageKey, paramsArray)
            {
                var oBundle = i18nModel.getResourceBundle();
                var sMsg = oBundle.getText(messageKey, paramsArray);
                jQuery.sap.log.debug("code: " + messageKey + ", params: " + paramsArray + "\nMessage: " + sMsg);
                return sMsg;
            }
        };
    };
    return new resourceBundleContext;
}, true /* export to global namespace */); //<-- there's the true
  1. 您必须使用sap.ui.require()以下列方式加载您的AMD模块:
代码语言:javascript
运行
复制
sap.ui.require("sap/hrf/ui/uilib/js/parser/infrastructure/locale/lib/resourceBundleContext",
               function(resourceBundleContext){
                 // called asynchronously when the module has loaded.
                 // resourceBundleContext is your module
               });
票数 2
EN

Stack Overflow用户

发布于 2016-03-31 09:44:20

在利用resourceBundleContext的模块中,您还可以使用类似于resourceBundleContext定义中的模式,例如:

代码语言:javascript
运行
复制
sap.ui.define([
    "sap/hrf/ui/uilib/js/parser/infrastructure/locale/lib/resourceBundleContext"
], function (ResourceBundleContext)
{
    'use strict';

    var AwesomeApp = function ()
    {
        var resourceBundleContext = new ResourceBundleContext();
        alert(resourceBundleContext.getString("AwesomeKey", ["AwesomeParms"]));
    };

    return AwesomeApp;

});

在整个UI5应用程序中以AMD风格构建所有模块,提供了一个一致性级别,并提高了加载所需模块的性能。

请注意,SAP没有利用require.js实现define,也不完全兼容require.js或其他AMD。您可以更多地了解SAP的实现以及与require.js在https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.html#.define上的不同之处

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36327759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档