【问题标题】:Cannot read property 'isArray' of undefined无法读取未定义的属性“isArray”
【发布时间】:2013-10-03 15:28:43
【问题描述】:

我正在使用主干构建一个应用程序,需要 yeoman 的 js。

我正在使用 twitter 的 typeaheadjs 并得到这个错误随机!大多数情况下它都可以工作,但有时甚至不会抛出任何错误!在 build(grunt) 这是我调用 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

【问题讨论】:

    标签: javascript requirejs yeoman typeahead.js


    【解决方案1】:

    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了解更多详情。

    【讨论】:

    • 这在开发版本中有效!现在开发版本运行良好,但在构建后预输入无法正常工作
    • 这些信息不足以帮助您。我怀疑这可能是一个单独的问题,需要提出另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多