9

目前,我正在将 require.js 用于一个有趣的项目,除了一个名为 prism.js 的代码语法高亮插件外,我的工作一切正常。我可以看到插件正在通过 chrome 中的网络选项卡拉出,但插件没有初始化。

我不确定这是一个需要问题还是插件是问题所在,并且想知道是否有人可以提供帮助。

这是我的 main.js:

require.config({
  // 3rd party script alias names
  paths: {
    // Core Libraries
    modernizr: "libs/modernizr",
    jquery: "libs/jquery",
    underscore: "libs/lodash",
    backbone: "libs/backbone",
    handlebars: "libs/handlebars",

    text: "libs/text",
    prism: "plugins/prism",

    templates: "../templates"
  },
  // Sets the configuration for your third party scripts that are not AMD compatible
  shim: {
    "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
    }
  }
});

// Include Specific JavaScript
require(['prism', 'modernizr', 'jquery', 'backbone', 'routers/router', 'views/AppVIew' ],
  function(Prism, Modernizr, $, Backbone, Router, App) {
    this.router = new Router();
    this.App = new App();
  }
);
4

3 回答 3

11

更改 shim 部分以包括 prism,并确保它导出“Prism”:

shim: {
  "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
  },
  "prism": {
      "exports": "Prism"
  }
}
于 2012-11-01T00:08:57.677 回答
3

Handlebars 和 Prism 与 A​​MD(Asyncronous Module Definition)不兼容,因此您需要像下面一样自己填充它;

requirejs.config({
    shim: {
        'backbone': {
            "deps": ["underscore", "jquery", "handlebars"],
            "exports": "Backbone"  //attaches "Backbone" to the window object
        },
        'handlebars': {
            "exports": 'Handlebars'
        },
        'prism': {
            "exports": "Prism"
        }
    }
});

您可能希望查看 require.js shim 文档站点; http://requirejs.org/docs/api.html#config-shim

希望这会有所帮助

于 2013-05-23T04:50:46.627 回答
1

棱镜也应该添加shim。就像主干一样,它不符合 AMD 标准,因此必须以相同的方式声明。

于 2012-10-31T23:30:24.770 回答