错误:TypeError:$(...)。selectpicker不是使用requirejs的函数加载库

时间:2016-02-25 05:01:04

标签: javascript jquery-ui requirejs

错误:TypeError:$(...)。selectpicker不是第一次打开Web应用程序时发生的函数错误。

在我的app.js

requirejs.config({
    baseUrl: '',
    paths: {
        "jquery": "js/jquery",
        "jquery-ui": "js/jquery-ui",
        text: "lib/text",
        //"css": "css",
        handlebars: 'js/handlebars',
        moment: "js/moment.min",
        bootstrap: 'js/bootstrap.min',
        somtjquery: 'widgets/somt/js/somtjquery',
        bootstrapselect: 'js/bootstrap-select',
        utils: 'widgets/somt/js/utils',
        filterwidget: 'widgets/somt/js/filterwidget'
    },  
    shim: {
        handlebars: {
            exports: 'Handlebars'
        },
        'bootstrap': {
            deps: ['jquery']
        },
        //'bootstrapdatepicker':{deps: ['jquery','bootstrap']},
        'somtjquery': {
            deps: ['jquery', 'utils', 'bootstrap']
        },
        'bootstrapselect': {
            deps: ['jquery', 'bootstrap']
        },
        'jquery': {
            exports: '$'
        },
        "datepicker": {
            deps: ["jquery-ui", "bootstrap"],
            exports: "$.fn.datepicker"
        },
        'tooltip': {
            deps: ['jquery'],
            exports: '$.fn.tooltip'
        },
        'filterwidget': {
            deps: ['jquery', "jquery-ui", "bootstrap", "bootstrapselect"],
            exports: 'filterwidget'
        }
    }
});

In filterwidget.js

define(["handlebars",'hbs!widgets/somt/templates/pfilter'],function(Handlebars,filter){
    var selector = "#somt-filter-area";
    $(selector).html(filter);
    $(function selectDatePicker() {
        try {
            $(selector).find('.selectpicker').selectpicker();
        } catch (e) {
            console.log("Date select err message : " + e);
        }
        try {
            $(selector).find("#week").datepicker({
                showOn: "button",
                buttonImage: "images/common-calendar-month-glyph.svg",
                buttonImageOnly: true,
                firstDay: 1,
                beforeShowDay: function(date) {
                    return [date.getDay() === weekofday, ""];
                },
                dayNamesMin: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
                maxDate: currentcutoffday,
                dateFormat: 'mm/dd/yy'
            });
        } catch (e) {
            console.log("Date picker err message : " + e);
        }
    });
});

特别是我在Firefox 36及以下版本中出现错误

1 个答案:

答案 0 :(得分:0)

您误解了如何使用shim。 Config没有filterwidget的正确依赖项。使用'shim'必须仅适用于非amd库。如果file具有define函数而不是它的amd模块,则该模块不需要shim。从shim

中删除此代码
'filterwidget': {
        deps: ['jquery', "jquery-ui", "bootstrap", "bootstrapselect"],
        exports: 'filterwidget'
    }

并在filterwidget.js中设置filterwidget的所有依赖项:

  

define(['jquery',“jquery-ui”,“bootstrap”,“bootstrapselect”,“handlebars”,'hbs!widgets / somt / templates / pfilter']

ps:也从shim中删除jquery,jquery-ui。这是AMD的libs。你怎么知道?只看一下图书馆的开头。例如'jquery-ui'。如果有这样的事情:

if ( typeof define === "function" && define.amd ) {

    // AMD. Register as an anonymous module.
    define([ "jquery" ], factory );
} else {

    // Browser globals
    factory( jQuery );
}

然后不需要垫片。