加载一个JavaScript库而不是返回一个对象

Pat*_*ten 2 xpages

我想在XPages中加载一个JavaScript库.

通常在HTML中,引用如下所示:

<html>
<head>
<script src="https://hammerjs.github.io/dist/hammer.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这给了我一个DOM中的Hammer对象,我可以进一步使用它.

在XPages中,我进行了以下设置:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" disableTheme="true"
    dojoForm="false" dojoTheme="false" dojoParseOnLoad="false"
    createForm="false">
    <xp:this.resources>
        <xp:script src="https://hammerjs.github.io/dist/hammer.js"
            clientSide="true">
        </xp:script>
    </xp:this.resources>
</xp:view>
Run Code Online (Sandbox Code Playgroud)

或者:

<?xml version="1.0" encoding="UTF-8" ?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" disableTheme="true" dojoForm="false" dojoTheme="false" dojoParseOnLoad="false" createForm="false">
    <xp:this.resources>
        <xp:headTag tagName="script">
            <xp:this.attributes>
                <xp:parameter name="script" value="text/javascript" />
                <xp:parameter name="src" value="https://hammerjs.github.io/dist/hammer.js" />
            </xp:this.attributes>
        </xp:headTag>
    </xp:this.resources>
</xp:view>
Run Code Online (Sandbox Code Playgroud)

但是DOM中没有Hammer对象!

我究竟做错了什么?

Per*_*ten 5

hammer.js使用AMD.这是使用AMD的hammer.js源代码片段:

if (typeof define == TYPE_FUNCTION && define.amd) {
    define(function() {
        return Hammer;
    });
} else if (typeof module != 'undefined' && module.exports) {
    module.exports = Hammer;
} else {
    window[exportName] = Hammer;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是AMD在XPage中加载与Dojo的冲突.请参阅此答案,了解如何删除AMD加载.

在您的情况下,您需要下载hammer.js,更改AMD加载部分,将其添加到您的nsf,然后从您的nsf加载脚本.

您可以通过更改hammer.js中的代码来删除AMD加载部分,例如:

//if (typeof define == TYPE_FUNCTION && define.amd) {
//    define(function() {
//        return Hammer;
//    });
//} else if (typeof module != 'undefined' && module.exports) {
if (typeof module != 'undefined' && module.exports) {
    module.exports = Hammer;
} else {
    window[exportName] = Hammer;
}
Run Code Online (Sandbox Code Playgroud)