javascript - Typeahead 在 AEM 6.1 中不起作用,但在 CQ 5.6.1 中起作用

标签 javascript jquery java-8 aem

我正在将一个项目从 CQ 5.6.1 迁移到 AEM 6.1。

然后,当我测试它时,发现我的代码中使用 typeahead.bundle.js 的部分不再工作。当我返回在 CQ 5.6.1 中测试该项目时,它确实工作正常。

尝试隔离问题(获取代码片段,无需将其部署到 AEM 6.1),并且工作正常。

我的初步发现是,单独的 AEM 6.1 服务器可能会导致 typeahead.bundle.js 无法工作。

<html>
<head>

<script src="jquery-2.0.0.min.js"></script>
<script src="hogan-3.0.1.js"></script>
<script src="typeahead.bundle.js"></script>

</head>
<body>

<row>

<input id="country-search" type="text" class="typeahead form-control" placeholder="Country" title="Enter the terms you wish to search for" />

</row>

<script type="text/javascript">
    $(document).ready(function() {
        var countries = new Bloodhound({
          name: 'countrysearch-countries',
          datumTokenizer: Bloodhound.tokenizers.obj.whitespace('englishName', 'localName'),
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          limit: 5,
          prefetch: {
           url: 'country.json',
          }
        });
        var countrySuggestionTemplate = Hogan.compile('<p>{{localName}} ({{englishName}})</p>');

        countries.initialize();
        $("#country-search").typeahead(null, {
            displayKey: 'localName',
            source: countries.ttAdapter(),
            templates: {
                suggestion: function(country) {
                    return countrySuggestionTemplate.render(country);
                }
            }
        }).on('typeahead:selected', function (e, datum) {
            window.location = datum.url;
        });
    });
</script>

country.json 的 JSON 字符串内容:

[{"url":"http://xxxx.com ","localName":"阿富汗","englishName":"阿富汗"},{"url":"http://xxxx.com ","localName":"Shqipëria","englishName":"阿尔巴尼亚"},{"url":"http://xxxx.com ","localName":"阿尔巴尼亚","englishName":" Algolia "}]

注意:请注意,上面的 HTML 脚本在未部署到 AEM 6.1 时可以正常工作。

希望大家能帮我重新评分。

谢谢

最佳答案

我已经找到答案了。

由于未声明的变量,aem6 服务器中似乎存在未初始化的内部 JavaScript(很奇怪,因为它在 CQ 5.6.1 中工作)。

所以我明确地声明了该变量,现在它工作正常。

关于javascript - Typeahead 在 AEM 6.1 中不起作用,但在 CQ 5.6.1 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988441/

相关文章:

javascript - TinyMCE 文本区域不响应 'onkeydown' 事件

javascript - Angular 服务/值没有从 $(document).ready() 中获取新的 var

javascript - 向显示/可见的每 4 个里添加一个特定类

java - 使用 lambda 执行 void : Is this correct?

javascript - 如何使用 ffmpeg 通过媒体记录器 api 从 block 中创建视频文件 webm

javascript - 在 javascript 中拦截的原型(prototype)

javascript - 无法请求使用 JQuery 将 GET API 部署到本地 Tomcat 服务器?

javascript - 如何在没有提交按钮的情况下使用回车键在表单中发送 <select>

java - 理解 Java Completablefuture 的行为

java - 有没有办法使用 java 8 替换列表中的 Map 值?