1

我的 Rails 应用程序有问题 - 我有两个脚本使彼此无法正常工作。首先,algolia 搜索引擎脚本(运行良好)

在 app/assets/javascript/algolia.js 中:

  $(document).ready(function() {
    // Replace the following values by your ApplicationID and ApiKey.
    var algolia = new AlgoliaSearch('MYAPPID', 'MYAPPPWD');
    // replace YourIndexName by the name of the index you want to query.
    var index = algolia.initIndex('Pin');

    // Mustache templating by Hogan.js (http://mustache.github.io/)
    var template = Hogan.compile('<div class="hit">' +
      '<a href="http://0.0.0.0:3000/pins/{{{slug}}}">'
     +
      '<div class="name">' +
        '{{{ _highlightResult.description.value }}} ' +
      '</div>' +
      '</a>' +
      '</div>');


    // typeahead.js initialization
    $('#user-search').typeahead(null, {
      source: index.ttAdapter({ hitsPerPage: 5 }),

      displayKey: 'description',
      templates: {
        suggestion: function(hit) {

          // select matching attributes only
          hit.matchingAttributes = [];
          for (var attribute in hit._highlightResult) {
            if (attribute === 'name' || attribute == 'company') {
              // already handled by the template
              continue;
            }
            // all others attributes that are matching should be added in the matchingAttributes array
            // so we can display them in the dropdown menu. Non-matching attributes are skipped.
            if (hit._highlightResult[attribute].matchLevel !== 'none') {
              hit.matchingAttributes.push({ attribute: attribute, value: hit._highlightResult[attribute].value });
            }
          }

          // render the hit using Hogan.js
          return template.render(hit);

        }
      }
    });
  });

以及使用magnific-popup-gem加载弹出窗口的脚本

 $(function() {
          $('.please-login').magnificPopup({
          type: 'inline',
          preloader: false,
          focus: '#username',
          modal: true
          });
          $(document).on('click', '.please-login-dismiss', function (e) {
          e.preventDefault();
          $.magnificPopup.close();
          });
          });

看起来这些线条使宏伟的弹出窗口不起作用。

 <script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
    <!-- Typahead.js is used to display the auto-completion menu -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.4/typeahead.bundle.min.js"></script>
    <!-- Hogan.js is used to render the hits using Mustache.js templating -->
    <script src="//cdn.jsdelivr.net/hogan.js/3.0.0/hogan.common.js"></script>
    <!-- Algolia -->
    <script src="//cdn.jsdelivr.net/algoliasearch/latest/algoliasearch.min.js"></script>

有什么想法可以解决这个问题吗?

4

2 回答 2

2

请检查 jquery 在依赖它的每个脚本之前只加载一次。

Jquery 应该已经被 rails 加载了,所以你可能不需要这条线

<script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
于 2014-11-09T14:10:37.067 回答
0

确保您的第二个文件加载良好:

验证这些行是否存在:
在 application.css:
*= require magnific-popup

在 application.js 中:
//= 需要 magnific-popup

使用 web 控制台作为 firefox 的 firebug 或 chrome 上的基本控制台,并在 Sources -> localhost -> assets 中查找,看看是否找到了文件。

如果你没有找到它:这是一个链接问题(拼写或基本错误的小错误)如果你找到它:这是你的脚本中的一个问题(观察 css 类是否存在,使用 Web 控制台中的调试器)

于 2014-11-09T14:13:07.570 回答