Javascript/requirejs
提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 02:59時点における127.0.0.1 (トーク)による版 (ページの作成:「==ダウンロード== こちらからrequire.jsを落とす http://requirejs.org/docs/start.html wget http://requirejs.org/docs/release/2.0.2/comments/require.js ==...」)
ダウンロード
こちらからrequire.jsを落とす http://requirejs.org/docs/start.html
wget http://requirejs.org/docs/release/2.0.2/comments/require.js
サンプルコード
- index.html
<!DOCTYPE html>
<html>
<head>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body>
<h1>My Sample Project</h1>
</body>
</html>
- scripts/main.js
require.config({
paths: {
jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min'
}
});
require(['jquery', 'model/util'], function($, AppView, Model_Util) {
var model = new Model_Util();
console.log(model.hoge()); // hogehoge
console.log(model.getName()); // utilname
});
- scripts/views/app.js
define(['jquery'], function($) {
var Model_Util = function() {
this.name = 'utilname';
};
Model_Util.prototype.hoge = function() {
return 'hogehoge';
};
Model_Util.prototype.getName = function() {
return this.name;
};
return Model_Util;
});
参考
http://requirejs.org/docs/start.html
