我正在为我的应用程序使用sDashboard,当我尝试使用requirejs创建模块时,我总是得到未定义。
此sDashboard可在github上找到。
https://github.com/ModelN/sDashboard
我的配置文件。
requirejs.config({
baseUrl: 'http://localhost:8090/module-require_dashboard/js/',
paths: {
jquery:'lib/jquery.min',
mockjax:'lib/jquery.mockjax',
jqueryui: 'lib/jquery-ui.min',
Flotr:'lib/flotr2/flotr2',
sDashboard:'lib/jquery-sDashboard'
},
shim: { //add the dependencies that should be loaded synchronously
'jqueryui': ['jquery'],
'timepicker': ['jqueryui'],
'mockjax': ['jquery']
}
});
sDashBoard小部件js
https://github.com/ModelN/sDashboard
sDashboard的js文件的一部分供您参考。
( function(factory) {"use strict";
if ( typeof define === 'function' && define.amd) {
// Register as an AMD module if available...
define(['jquery', 'Flotr'], factory);
} else {
// Browser globals for the unenlightened...
factory($, Flotr);
}
}(function($, Flotr) {"use strict";
$.widget("mn.sDashboard", {
version : "2.5",
options : {
dashboardData : [],
widgetWidth :400
},
当我使用define在Dashboard.js文件中使用sDashboard时,我总是得到sDashboard的未定义。
define(['jquery','jqueryui','sDashboard'], function(JQuery,jqueryui,sDashboard){
console.log(sDashboard);
});
我不知道自己犯了什么错误,请提出你的建议。
答案 0 :(得分:1)
sDashboard是一个jQuery 插件。当您加载jQuery插件时,它会自行安装在jQuery
对象上。在RequireJS中加载jQuery插件的事实对此没有任何改变。所以通常这样的插件,当加载到AMD环境(如RequireJS)时,不会从其工厂函数返回任何内容。因此,如果您尝试检查模块,则会找到undefined
。这是完全正常的,而不是没有正确加载的迹象。您必须在jQuery
对象上测试插件是否存在。
你想要做的是这样的事情:
define(['jquery', 'jqueryui', 'sDashboard'], function(jQuery, jqueryui) {
console.log(jQuery.sDashboard);
});
您不需要在回调中使用与"sDashboard"
对应的参数,因为这样做无用。 (当然,如果你在"sDashboard"
之后有其他依赖关系,那么你必须在参数列表中有一个占位符,或者在它应该得到错误值之后的其他参数。)