使用Require.js加载jquery-ui给了我一些问题--jquery-ui的依赖关系看不到m工作。这是错误:
未捕获的TypeError:无法读取未定义的属性“ui”
以下是两个文件:
main.js
require.config({
baseUrl: '/git-cake-skeleton/js',
paths: {
'jquery': 'lib/jquery-1.10.2',
'jqueryui': 'lib/jquery-ui-1.10.3.min',
'bootstrap': 'lib/bootstrap.min'
},
shim: {
'jqueryui': {
exports: '$',
deps: ['jquery']
},
'bootstrap': {
deps: ['jquery']
}
} });
require([
'jquery',
'widgets/demowidget'
], function ($) {
$(document).ready(function() {
// This is where we bind our widgets to stuff on the page. All the real logic happens inside widgets!
$(".app-js-demowidget").demowidget();
});
} );
demowidget.js
// Example of simple plugin inside Require.js framework
define(['jquery', 'jqueryui'], function ($) {
$.widget('skeleton.demowidget', {
options: {
},
_init: function() {
this.element.click(function(e){
alert('Hello world');
});
}
});
});
文件结构:
|-js
| main.js
|---lib
| bootstrap.min.js
| jquery-1.10.2.js
| jquery-ui-1.10.3.min.js
| require.js
|---widgets
| demowidget.js
编辑:正如所料,在demowidget.js中使用'bootstrap'切换'jqueryui'会出现以下错误:
Bootstrap需要jQuery
答案 0 :(得分:1)
首先需要定义jquery依赖项:
define('jquery', [], function () { return root.jQuery; });
然后你可以使用'jquery'来加载其他的libs,具体取决于jQuery。