要求使用 AMD 模式为 jQuery UI 事件提供错误

require using AMD pattern gives error for jQuery UI events

在我的代码中,test.js 依赖于 jquery-ui,它不使用 require AMD 模式,test.spec.js 依赖于 jquery-ui, test.js 使用AMD模式。我们可以在 运行ning test.spec.js.

时动态加载 jquery-ui in test.js 的依赖吗?
require.config({

    baseUrl: '/demo',

    paths: {
        'jquery': '../library/jquery-1.11.1',
        'jquery-ui': '../library/jquery-ui-1.11.4'
    },
    shim: {
        'jquery': {
            exports: 'jQuery'
        },
        'jquery-ui': {
            deps: ['jquery']
        },
        'library/src/js/test': {
            deps: ['library/jquery-1.11.1', 'library/jquery-ui-1.11.4', '../js/collapse'],
            exports: 'Test'
        }
    },
    callback: window.__karma__.start
});

在test.js"draggable" of jquery-ui中写入了可拖动事件。评估后 $('#panelId').draggable({revert: true}); 收到错误

"TypeError: 'undefined' is not a function (evaluating '$('#panelId').draggable({revert: true})')"

如何为 require.config 中的 test.js 加载 jquery-ui。当我将它用于 运行 我的 jasmine 测试用例时。 在真实环境中,它按预期工作,但在 jasmine 测试用例中无法找到 jquery-ui 事件。 test.js 未使用 require.js,但 test.spec.js 使用 require AMD 模式。

在 test.spec.js 代码执行后出现 jquery-ui draggable undefined

错误
define(['jquery-ui','library/src/js/test'], function ($) {

});

我可以使用 $ 在 test.spec.js 中访问 jquery ui,而不是在 test.js 中访问 jquery-ui 事件写成 test.js 不使用 AMD require 模式。不知道缺少什么。任何帮助都会得到帮助... :)

您需要使用 jquery-ui 而不是文件路径。

shim:{
     'jquery': {
            exports: 'jQuery'
          },
     'jquery-ui': {
            deps: ['jquery']
        }
}

define(['jquery-ui','library/src/js/test'], function ($, test) {
    //code goes here
});

试试这个更新的 require js 配置

使用 "library/src/js/test" 加载您的测试

require.config({

    baseUrl: '/demo',

    paths: {

        'jquery': '../library/jquery-1.11.1', // assuming this is a correct path
        'jquery-ui': '../library/jquery-ui-1.11.4'  // assuming this is a correct path
    },
    shim: {
        'jquery': {
            exports: 'jQuery'
        },
        'jquery-ui': {
            deps: ['jquery']
        },
        'library/src/js/test': {
            deps: ['jquery', 'jquery-ui', '../js/collapse'], // changes done here
            exports: 'Test'
        }
    },
    callback: window.__karma__.start
});