2012-10-31 82 views
8

當前我正在使用require.js來開發一個有趣的項目我工作的一切工作都很好,除了代碼語法higlighting plugin prism.js。我可以看到插件是通過chrome中的網絡選項卡拉取的,但插件未初始化。使用require.js加載非amd模塊

我不確定這是否是一個需要解決的問題,或者如果插件是問題,並想知道是否有人可以提供幫助。

下面來看看我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(); 
    } 
); 

回答

10

更改墊片部分包括棱鏡,並確保出口「棱鏡」:

shim: { 
    "backbone": { 
     "deps": ["underscore", "jquery", "handlebars"], 
     "exports": "Backbone" //attaches "Backbone" to the window object 
    }, 
    "prism": { 
     "exports": "Prism" 
    } 
} 
+0

一旦它被「墊底」,我該如何使用它? –

+2

就像任何其他模塊一樣:'require('prism');'或者將它作爲依賴包含在'define'的參數中。 –

1

棱鏡應該被添加到shim了。就像骨幹一樣,它不符合AMD標準,因此必須以相同的方式聲明。

+0

即使插件有沒有依賴性? – Lawrence

+0

是的我的不好,我認爲通過將它稱爲一個「插件」,你的意思是一個「jquery」插件,並且它在jquery上開放。 – yakxxx

3

車把和棱鏡不與AMD(Asyncronous模塊定義)兼容,所以你需要墊片它自己像下面一樣;

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

希望這將有助於

相關問題