无法读取未定义的属性“isArray”

Cannot read property 'isArray' of undefined

本文关键字:isArray 属性 读取 未定义      更新时间:2023-09-26

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

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

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

这是我在视图的 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,您需要为其定义填充码配置。它将是这样的:

requirejs.config({
  // ...
  shim: {
    "typeahead": {
      deps: ['jquery'],
      exports: 'jQuery.fn.typeahead'
    }
  }
});
define(['jquery', 'typeahead'], function ($, youCanIgnoreThis) {
  var opts = {
    // ...
  };
  $("#SearchProduct").typeahead(opts);
})

有关更多详细信息,请阅读文档。