javascript - 无法读取未定义的属性 'isArray'

标签 javascript requirejs yeoman typeahead.js

我正在使用 Backbone 、requirejs 和 yeoman 构建一个应用程序。

我使用 Twitter 的 typeaheadjs 并随机收到此错误!大多数时候它可以工作,但有时提前输入甚至不会抛出任何错误!并且在构建之后,预输入甚至无法工作(咕噜声)这是我调用预输入的页面

define([
    'jquery',
    'underscore',
    'backbone',
    'templates',
    ...
    'typeahead',
    ...
    ], function ( $, _, Backbone, JST, a, b, typeahead, c, d) {

这是我初始化 View render() 内的 Typeahead 的地方

this.collection.fetch({
    success: function (data) {
        $('#SerachProduct').typeahead({
            name: 'abc',
            valueKey: 'name',
            local: data.toJSON(),
            template: JST['app/scripts/templates/typeahead.ejs']
        });
    },
    error: fun() {..
    }
}

这是 github 存储库 Github

最佳答案

Typeahead 与 AMD 不兼容,您需要定义 shim对其进行配置。它会是这样的:

requirejs.config({
  // ...
  shim: {
    "typeahead": {
      deps: ['jquery'],
      exports: 'jQuery.fn.typeahead'
    }
  }
});

define(['jquery', 'typeahead'], function ($, youCanIgnoreThis) {
  var opts = {
    // ...
  };
  $("#SearchProduct").typeahead(opts);
})

阅读the documentation了解更多详情。

关于javascript - 无法读取未定义的属性 'isArray',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19163156/

相关文章:

angularjs - NodeJS - 找不到模块 'spdx-license-ids'

css - 为什么 css 文件在 "server"模式下正确导入(从 scss 文件)而不是在 "dist"模式下? (自耕农, compass )

unit-testing - 如何使用 Angular 翻译进行单元测试

javascript - 如果所有div都加载了

javascript - 当URL中没有显示mod_rewrite重写的URL时,我可以在客户端访问它吗?

javascript - 重新加载并显示消息

javascript - 在 JS 中,我可以仅 require() 模块的导出吗?

javascript - jQuery:从给定的 id 中选择特定元素并将焦点放在它上面。优化方式

javascript - 在没有单例模式的情况下通过 require js 共享原型(prototype)实例?

javascript - ChartJS 并需要 : Chart is undefined