Download html5-boilerplate_v5.0.0.zip from HTML5 Boilerplate. And put files in your public folder.
You will see files:
user@host:public$ tree
.
├── 404.html
├── apple-touch-icon.png
├── browserconfig.xml
├── crossdomain.xml
├── css
│ ├── main.css
│ └── normalize.css
├── doc
│ ├── css.md
│ ├── extend.md
│ ├── faq.md
│ ├── html.md
│ ├── js.md
│ ├── misc.md
│ ├── TOC.md
│ └── usage.md
├── favicon.ico
├── humans.txt
├── img
├── index.html
├── js
│ ├── main.js
│ ├── plugins.js
│ └── vendor
│ ├── jquery-1.11.2.min.js
│ └── modernizr-2.8.3.min.js
├── robots.txt
├── tile.png
└── tile-wide.png
5 directories, 24 files
Open your favorite HTML5 browser and access code base. Mine is http://centos.xiaoqiang.loc/.
We already have a code base which HTML5 Boilerplate. In the code base, we have only One page index.html with entrance js/main.js. We are using the same code structure to support RequireJS.
Now let’s add require.js in.
Download require.js (v2.1.16) and locate it besides js/main.js as js/require.js.
Then change <script src="js/main.js"></script>
to <script src="js/require.js" data-main="js/main"></script>
in index.html web page.
<script>window.jQuery || document.write(‘<script src="js/vendor/jquery-1.11.2.min.js"><\/script>‘)</script>
<script src="js/plugins.js"></script>
<!-- <script src="js/main.js"></script> -->
<script src="js/require.js" data-main="js/main.js"></script>
For single page support, we just insert requirejs.config( configs ) in entrance code js/main.js.
/* file: js/main.js */
/**
* RequireJS configuration
**/
requirejs.config({
baseUrl: ‘js‘
});
/**
* Entrance
**/
require([], function(){
alert( "Hello, world." );
});
Then browser your site and you will get “Hello, world.” as alert message.
When there are more than one web page is going to develop. We need some configures.
requirejs.config()
Put RequireJS configuration in js/common.js file.
/* file: js/common.js */
requirejs.config({
baseUrl: ‘js‘
});
/****************** SAMPLE ******************
requirejs.config({
baseUrl: ‘js‘,
shim: {
jquery: {
exports: ‘jQuery‘
},
underscore: {
exports: ‘_‘
},
backbone: {
deps: [ ‘underscore‘, ‘jquery‘ ],
exports: ‘Backbone‘
},
backboneLocalstorage: {
deps: [ ‘backbone‘ ],
exports: ‘Store‘
}
},
paths: {
modernizr: ‘vendor/modernizr-2.8.3.min‘,
jquery: ‘vendor/jquery-1.11.2.min‘,
underscore: ‘vendor/underscore‘,
backbone: ‘vendor/backbone-min‘,
backboneLocalstorage: ‘vendor/backbone.localStorage-min‘,
text: ‘vendor/text‘
},
map: {
‘*‘: {
‘loadCSS‘: ‘helpers/loadCSS/wrapper‘
}
},
config: {
‘i18n‘: { locale: ‘zh-cn‘ }
}
});
****************** /SAMPLE ******************/
/* file: js/main.js */
/**
* load js/common.js in each web page is required.
*/
require([ ‘common‘ ], function(){
require([], function(){
alert( "Hello, world." );
});
}); // js/common.js loader
data-main="js/page1/main.js"
require([ ‘../common‘ ], ...
Actually, dependencies or other shared configures in js/common.js can work in pageXXXX.html well, by loading it before main logic.
See RequireJS i18n bundle API can help more.
Download i18n.js and put besides js/main.js as js/i18n.js.
It is suggest that nls folder should locate besides page entrance codes, for example js/nls/.
/* file: js/main.js */
/**
* load js/common.js in each web page is required.
*/
require([ ‘common‘ ], function(){
require([
‘i18n!nls/main‘ /* Use ./i18n.js to load `nls/main` locale files */
], function( main){
alert( main.helloworld );
});
}); // js/common.js loader
Then, create the following sample files.
/* file: js/nls/main.js */
define({
"root": {
"helloworld": "Hello, world."
},
"zh-cn": true
});
/* file: js/nls/zh-cn/main.js */
define({
"helloworld": "你好,世界。"
});
/* file: js/common.js */
requirejs.config({
baseUrl: ‘js‘,
config: {
‘i18n‘: { locale: ‘zh-cn‘ }
}
});
“你好,世界!” will be in alert message as js/common.js configures locale is “zh-cn”.
HTML5 Boilerplate with RequireJS (持续更新)
原文地址:http://blog.csdn.net/wxqee/article/details/44178735