2012-12-19 38 views
11

如何將requirejs集成到流星應用程序中並使用AMD模塊,例如我的Backbone模塊? 有沒有人這樣做,並可以告訴我需要什麼步驟來實現這個工作?使用Meteor with Requirejs

回答

4

一個簡單的答案(儘管可能不是你正在尋找的那個)是你可以簡單地使用這兩個獨立的。換句話說,加載所有的流星腳本,然後開始你需要的腳本加載。您需要的腳本將能夠使用Meteor的東西,而無需通過Require的裝載器「導入」任何它。

如果你想想要必須導入它,你應該爲它創建一個Require「shim」。

+0

我最後一次流星(V0.4)發揮它沒有要求/ CommonJS的支持。我認爲唯一真正的選擇是編寫自己的流星包裝插件,或手動將所需文件複製到流星文件系統。 – dsummersl

+0

https://gist.github.com/3922137 fs = __meteor_bootstrap __。require('fs') – crapthings

+0

@machineghost:謝謝,這聽起來像一個合理的方法 –

0

以下是我如何在Meteor和IronRouter中加載Aloha Editor。 Aloha使用requirejs加載它的所有依賴項。

  1. 解壓阿羅哈分佈公共/ alohaeditor
  2. 將除aloha-common-extra.css以外的所有Aloha css文件移動到client/lib/alohaeditor(不要忘記plugins文件夾中的文件)。
  3. 在所有的Aloha css文件中,將相對路徑轉換爲絕對路徑(用'/ alohaeditor /'替換所有'../')。
  4. 安裝wait-on-lib流星包。
  5. 添加下面的鉤路線:

    onBeforeAction: function(pause) 
        {   
        // Dynamically load require.js 
        var one = IRLibLoader.load('/alohaeditor/lib/require.js', 
         { 
         success: function(){ console.log('Successfully loaded require.js'); }, 
         error: function(){ console.log('Error loading require.js'); } 
         }); 
        if(!one.ready()) 
         return pause(); 
    
        // Aloha settings 
        Aloha = window.Aloha || {}; 
        Aloha.settings = Aloha.settings || {}; 
        Aloha.settings.baseUrl = '/alohaeditor/lib/'; 
        Aloha.settings.plugins = Aloha.settings.plugins || {}; 
        Aloha.settings.plugins.load = 'common/ui, common/format, common/link, common/table, common/list, common/block, common/undo, common/contenthandler, common/paste, common/commands, common/abbr'; 
    
        // Dynamically load aloha.js 
        var two = IRLibLoader.load('/alohaeditor/lib/aloha.js', 
         { 
         success: function(){ console.log('Successfully loaded aloha.js'); }, 
         error: function(){ console.log('Error loading aloha.js'); } 
         }); 
        if(!two.ready()) 
         return pause(); 
        },