2012-06-19 51 views
1

我在我的項目中使用require.js,它的插件use.js。在require.js和use.js中順序加載腳本

require.config({ 
// Initialize the application with the main application file 
deps: ["main"], 

paths: { 
    // JavaScript folders 
    libs: "../assets/js/libs", 
    plugins: "../assets/js/plugins", 

    // Libraries 
    jquery: "../assets/js/libs/jquery", 
    underscore: "../assets/js/libs/underscore", 
    backbone: "../assets/js/libs/backbone", 
    jqueryUI: "../assets/js/libs/jqueryUI", 
    jquerySlider: "../assets/js/libs/slider", 
    jqueryMouseWheel: "../assets/js/libs/jquery.mousewheel",  
    jquerySelectBox: "../assets/js/libs/selectBox", 
    jqueryMouse: "../assets/js/libs/mouse", 
    bookReader: "../assets/js/libs/newReader", 

    // Shim Plugin 
    use: "../assets/js/plugins/use" 
}, 

use: { 
    backbone: { 
     deps: ["use!underscore", "jquery"], 
     attach: "Backbone" 
    }, 

    underscore: { 
     attach: "_" 
    }, 

    jqueryUI: { 
     deps: ["jquery"] 
    }, 

    jquerySlider: { 
     deps: ["jquery", "jqueryMouse"] 
    }, 

    jquerySelectBox: { 
     deps: ["jquery"] 
    }, 

    jqueryMouse: { 
     deps: ["jquery", "jqueryUI"] 
    }, 

    jqueryMouseWheel: { 
     deps: ["jquery", "jqueryUI"] 
    }, 

    bookReader: { 
     deps: ["jquery", "jqueryUI", "jqueryMouse", "jquerySlider", "jqueryMouseWheel", "jquerySelectBox"], 
     attach: "Reader" 
    } 
} 
}); 

的問題是,腳本同時啓動負荷,因爲有不能滿足的依賴,往往有這兩個錯誤

Uncaught TypeError: Object function (a, b) {return new e.fn.init (a, b, h)} has no method 'widget': 4444/assets/js/libs/mouse.js: 15 
Uncaught TypeError: Cannot read property 'mouse' of undefined: 4444/assets/js/libs/slider.js: 20 

一旦文件是在緩存中,該問題不再發生。 所以,我的問題。我如何指定腳本必須一致加載?

可以構建將它們包裝到一個文件中的腳本。但是這個選項不是很理想

謝謝!

回答

1

看看這個名爲STEP的AMD加載器插件,它允許您按順序加載腳本。

https://github.com/requirejs/step

+0

它不清潔如何在requirejs中安裝步驟。我試圖在require.js之前和之後調用step.js,但都沒有成功。有什麼建議麼? –