如何使用requirejs和use.js加载jQuery插件(jquery.cookie,jquery.validate.unobtrusive)

时间:2013-01-30 17:27:45

标签: javascript backbone.js requirejs

我一直在尝试从http://net.tutsplus.com/tutorials/javascript-ajax/building-a-scalable-app-with-backbone-js/扩展模板应用程序。通过以下实现,我面临“未捕获的ReferenceError:未定义jQuery”。有人可以告诉我可能导致它的问题。

config.js
// Set the require.js configuration for your application.
require.config({
  // Initialize the application with the main application file
  deps: ["main"],
  urlArgs: "bust=" + (new Date()).getTime(),
  waitSeconds: 15,
  paths: {
    // JavaScript folders
    libs: "../assets/js/libs",
    plugins: "../assets/js/plugins",

    // Libraries
    //jquery: "../assets/js/libs/jquery",
    jquery: "http://code.jquery.com/jquery-1.7.min",
    jquerycookie:  "../assets/js/libs/jquery.cookie",
    jqueryDataTables: "../assets/js/libs/jquery.dataTables.min",
    jqueryValidate: "../assets/js/libs/jquery.validate",
    jqueryValidateUnobtrusive: "../assets/js/libs/jquery.validate.unobtrusive",
    underscore: "../assets/js/libs/underscore",
    backbone: "../assets/js/libs/backbone",
    highcharts:  "../assets/js/libs/highcharts/highcharts",
    highstock:  "../assets/js/libs/highcharts/highstock",
    easyXDM:     "../assets/js/libs/easyXDM/easyXDM.min",
    dateFormat: "../assets/js/libs/date.format",
    // Shim Plugin
    use: "../assets/js/plugins/use"
  },
  priority: [
        'jquery',
        'jquerycookie',
        'jqueryValidate',
        'jqueryValidateUnobtrusive',
        'underscore',
        'easyXDM',
        "highcharts",
        "highstock",
        "dateFormat"
  ],
  use: {
    backbone: {
      deps: ["use!underscore", "jquery"],
      attach: "Backbone"
    },
    jqueryDataTables: {
        deps: ["jquery"]
    },
    jqueryValidate: {
        deps: ["jquery"]
    },
    jqueryValidateUnobtrusive: {
        deps: ["jquery", "jqueryValidate"]
    },
    jquerycookie: {
        deps: ["jquery"]
    },
    underscore: {
      attach: "_"
    },
    highcharts: {
      exports: 'Highcharts'
    },
    highstock: {
      exports: 'highstock'
    }
  }
});

main.js
require([
    "namespace",

    // Libs
    "jquery",
    "jqueryDataTables",
    "use!backbone",

    'jquerycookie',
    'jqueryValidate',
    'jqueryValidateUnobtrusive',
    'highcharts',

    // Modules
    "modules/tricklebot",
    "modules/home",
    "modules/dashboard",
    "modules/profile",
    "modules/transactions",
    "modules/analysis",
    "modules/signup",

],

function(namespace, $, jqDT, Backbone,
         jqCookie, jqValidate, jqValidateUnobtrusive, highcharts,       
         tricklebot, Home, Dashboard, Profile, Transactions, Analysis, Signup) {
......
......

2 个答案:

答案 0 :(得分:1)

我认为您正在关注过时的教程,usepriority已将require.js配置替换为shim,请查看此问题:https://github.com/jrburke/requirejs/wiki/Upgrading-to-RequireJS-2.0

答案 1 :(得分:0)

现在有一种更简单的方法可以使用更新的RequireJS加载普通JS文件 - 这种方法可以避免所有路径,地图,填充废话:

require(['jquery'],function($){
    require(
        [
            'path/to/my/module'
            , 'path/to/plugin1' // <- plain js file but OMIT ".js" extension in the ID
            , 'path/to/plugin2' // <- plain js file but OMIT ".js" extension in the ID
            , 'path/to/plugin3' // <- plain js file but OMIT ".js" extension in the ID
        ]
        , function(myModule /*, it does not matter what plugins return*/){
            // jQuery will have all the above plugins applied by now
            // because they apply their code to global window.jQuery
            // just start using them
            $(selector).plugin1(blah)
        }
    )
})

使用没有“.js”的计划JavaScript文件使得它像模块ID一样受到所有“映射”和“路径”修改的约束,但是当/如果普通JS文件不是模块时仍然不会抱怨。 / p>

模块不会在后续加载时重新加载,因为它们是从第一次缓存的。利润!