1

我有以下 config.js

require.config({
  baseUrl: "/static/js",
  paths: {
    jquery: "src/jquery-1.7.2",
    jqueryui: "src/jquery-ui-1.10.0.custom/js/jquery-ui-custom", 
    angular: "src/angular/1.0.4/angular",
    bootstrap: "bootstrap/js/bootstrap"
  },
  shim: {
    "jquery-validation-1.10.0/dist/jquery.validation.min" : {
        deps: ["jquery"],
        exports: "jquery.validation"
    }
  }
});

在我正在加载的一个 js 文件(名为signup.js)中,我试图使validate来自 jquery 验证插件的方法可用。

# File signup.js

define(["jquery"], function($) {

$('#beta_signup_form button[type=submit]').attr("disabled", "disabled");

$('#beta_signup_form').validate({
    rules: {
        name: {
            required: true,
            minlength: 4
        },
        email: {
            required: true,
            email: true 
        }
    },
    focusCleanup: true,
    onkeyup: false,
    errorElement: "span",
      ... the rest of the code ...

我在控制台中收到一个错误,上面写着Uncaught TypeError: Object [object Object] has no method 'validate'.

如何将validate方法传递给我的signup.js脚本?

4

1 回答 1

2

您需要稍微调整一下配置。通常,在填充模块时,您还需要为其指定路径:

require.config({
  baseUrl: "/static/js",
  paths: {
    jquery: "src/jquery-1.7.2",
    jqueryui: "src/jquery-ui-1.10.0.custom/js/jquery-ui-custom", 
    angular: "src/angular/1.0.4/angular",
    bootstrap: "bootstrap/js/bootstrap",
    // Give validate a path
    "jquery-validate": "jquery-validation-1.10.0/dist/jquery.validation.min" 
  },
  shim: {
    // Now shim it
    "jquery-validate": {
        deps: ["jquery"],
        exports: "jquery.validation"
    }
  }
});


// Define it as a dependency. If the plugin doesn't return a value
// (most just augment the jQuery.fn) then no need to pass an argument 
// to your callback.    
define(["jquery", "jquery-validate"], function($) {
    // module code
});

如果插件不导出值,您可以进一步缩短代码:

  shim: {
    "jquery-validate": ["jquery"]
  }

官方文档中的更多示例 - http://requirejs.org/docs/api.html#config-shim

于 2013-02-07T09:22:08.733 回答