require.js加载的模块对于引导和解析为空

require.js loaded modules are null for bootstrap and parse

本文关键字:和解 于引导 js 加载 模块 require      更新时间:2023-09-26

我正在尝试使用RequireJS加载Parse JS(parse.com)和Bootstrap JS。 我无法让它与这些库一起使用,但其他库似乎工作正常(例如 backcore.js)。

我已确保将其包含在我的索引.html文件中:

<script data-main="js/main" src="js/libs/require/require.js"></script>

我的主.js文件是这样的:

require.config({
  paths: {
    jquery: 'libs/jquery/jquery-min',
    underscore: 'libs/underscore/underscore-min',
    backbone: 'libs/backbone/backbone-min',
    bootstrap: 'libs/bootstrap/bootstrap-min',
    parse: 'libs/parse/parse-min',
    templates: '../templates'
  }
});
require([
  // Load our app module and pass it to our definition function
  'app',
], function(App){
  // The "app" dependency is passed in as "App"
  // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
  App.initialize();
});

最后我的应用程序.js是:

// Filename: app.js
define([
  'jquery', 
  'underscore', 
  'parse',
  'bootstrap',
  'router' // Request router.js
], function($, _, Parse, Bootstrap, Router){
  var initialize = function(){
    *console.log(Parse);*
    // Pass in our Router module and call it's initialize function
    Router.initialize();
  };
  return { 
    initialize: initialize
  };
});

如果相关,这里是我的文件结构:

/* File Structure
├── js
│   ├── libs
│   │   ├── jquery
│   │   │   ├── jquery-min.js
│   │   ├── backbone
│   │   │   ├── backbone-min.js
│   │   └── underscore
│   │   │   ├── underscore-min.js
│   │   └── parse
│   │   │   ├── parse-min.js
│   │   └── bootstrap
│   │   │   ├── bootstrap-min.js
│   │   └── require
│   │   │   ├── require.js
│   ├── router.js
│   ├── app.js
│   ├── main.js  // Bootstrap
│   └── text.js  //Require.js plugin
└── index.html

我看到我的用于解析/引导的 JS 文件正在由浏览器在 firebug 控制台选项卡中加载,但是 app.js 中的控制台.log语句返回 null。 当我打印路由器、_ 或 $ 时,它们不为空。

我在这里做错了什么?有什么指示吗?谢谢!

对于非 AMD 文件,您需要使用填充码配置。由于 parse-min 通过全局变量 'Parse' 导出它的功能,因此您需要在填充码配置中指定全局变量名称,如下所示。由于 bootstrap 不会导出特定的全局变量,因此您可以使用它的任何插件全局变量,例如 $.fn.popover。

有关垫片配置的更多信息,请阅读此内容:http://requirejs.org/docs/api.html#config-shim

require.config({
  paths: {
    jquery: 'libs/jquery/jquery-min',
    underscore: 'libs/underscore/underscore-min',
    backbone: 'libs/backbone/backbone-min',
    bootstrap: 'libs/bootstrap/bootstrap-min',
    parse: 'libs/parse/parse-min',
    templates: '../templates'
  },
  shim: {
     parse: {
        exports: 'Parse',
        deps: ["jquery"]
     },
     bootstrap: {
        exports: '$.fn.popover',
        deps: ["jquery"]
     },
     backbone: {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
     }, 
     underscore: {
        exports: '_'
     }, 
     jquery: {
        exports: '$'
     }
  }
});