RequireJs學習筆記之data-main Entry Point

c3tc3tc3t發表於2013-11-03

You will typically use a data-main script to set configuration options and then load the first application module. Note: the script tag require.js generates for your data-main module includes the async attribute. This means that you cannot assume that the load and execution of your data-main script will finish prior to other scripts referenced later in the same page.

翻譯:通常會使用data-main設定的指令碼里寫一些配置選項,然後讀取你配置裡的第一個程式模組,注意require.js生成的data-main模組裡的配置包含非同步屬性,注意你無法確定在同一個頁面裡面,你的data-main設定的指令碼會在你其他指令碼之前執行完,其他就是隻的在檔案位置上data-main後面的指令碼

script data-main="scripts/main" src="scripts/require.js"></script>
<script src="scripts/other.js"></script>---》這個就是其他所指的檔案

 

For example, this arrangement will fail randomly when the require.config path for the 'foo' module has not been set prior to it being require()'d later:

例如這個寫法就會很容易造成問題,require.config配置的這個path屬性沒有在require()執行前設定,而是是在他執行後設定

<script data-main="scripts/main" src="scripts/require.js"></script>--》標籤1
<script src="scripts/other.js"></script>--》標籤2

// contents of main.js:
require.config({
    paths: {
        foo: 'libs/foo-1.1.3'
    }
});

// contents of other.js:

// This code might be called before the require.config() in main.js-》
// has executed. When that happens, require.js will attempt to-》
// load 'scripts/foo.js' instead of 'scripts/libs/foo-1.1.3.js'
//翻譯:下面的程式碼或許在require.config()執行之前就被呼叫了,
//會造成require.js會載入script目錄下的foo.js。而不是scripts/libs下的foo-1.1.3,
//在不設定baseUrl情況下requirejs會預設載入引用了requirejs檔案所在的目錄裡的模組-》src="scripts/require.js"的
//總結就是說 不要以為標籤2在標籤1下面。標籤1裡的東西就會執行完
require( ['foo'], function( foo ) { });

 


相關文章