1

Using Require.js to load jquery-ui is giving me problems - jquery-ui's dependencies don't see m to be working. Here is the error:

Uncaught TypeError: Cannot read property 'ui' of undefined

Here are the two files:

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');
        });
    }
});
});

File structure:

|-js
|   main.js 
|---lib
|      bootstrap.min.js
|      jquery-1.10.2.js
|      jquery-ui-1.10.3.min.js
|      require.js
|---widgets
|      demowidget.js

edit: as expected, switching 'jqueryui' with 'bootstrap' in demowidget.js gives the following error:

Bootstrap requires jQuery

4

1 回答 1

2

您首先需要定义 jquery 依赖项:

define('jquery', [], function () { return root.jQuery; });

然后你可以使用 'jquery' 来加载其他依赖于 jQuery 的库。

于 2013-09-13T14:34:55.657 回答