RequireJSã«ã¦Backbone.jsã¨Mustache.jsãèªã¿è¾¼ã
RequireJS ã®2.0 ããå°å ¥ããã shim ãå©ç¨ãã¦ç°¡åã«èªã¿è¾¼ããã®ã§ã¡ã¢ã
æåã«ã¾ã¨ããæ¸ã
- RequireJS ã§èªã¿è¾¼ãã
- html ãã¡ã¤ã«ã§ script ãä¸ã¤ã§æ¸ãã®ã§å¬ããã
ãã£ã¬ã¯ããªæ§æ
$ tree . âââ index.html âââ js â   âââ libs â   â   âââ backbone-min.js â   â   âââ mustache.js â   â   âââ require.js â   â   âââ underscore-min.js â   â   âââ zepto.min.js â   âââ main.js âââ style.css
ä»åã¯ç°¡åã« js/libs ã«å¿ è¦ãªã©ã¤ãã©ãªãå ¥ããã
ãããããã¡ã¤ã«åãã©ã¤ãã©ãªåã«ãªã£ã¦ããã®ã§ããã®è¨äºãåç §ã«ããéã¯ãã¦ã³ãã¼ããã¦é ç½®ãããã¨ã
main.js
ã©ã¤ãã©ãªèªã¿è¾¼ã¿é¨æç²ã¯ä»¥ä¸ã
Zepto.jsãBackbone.jsãMustache.jsãããããRequireJSã®AMDã«åãã¦ããªãã¿ãããªã®ã§è¨å®ããã詳ããã¯ãã®ãã¼ã¸ä¸é¨ã®åè 2 ãåç §ãããã¨ã
require.config({ shim: { 'zepto': { exports: '$' }, 'backbone': { deps: ['underscore', 'zepto'], exports: 'Backbone' }, 'mustache': { exports: 'Mustache' } }, paths: { backbone: 'libs/backbone-min', zepto: 'libs/zepto.min', underscore: 'libs/underscore-min', mustache: 'libs/mustache' } });
以ä¸ã main.js ã®å ¨ä½ã主æã¯é©å½ã
require.config({ shim: { 'zepto': { exports: '$' }, 'backbone': { deps: ['underscore', 'zepto'], exports: 'Backbone' }, 'mustache': { exports: 'Mustache' } }, paths: { backbone: 'libs/backbone-min', zepto: 'libs/zepto.min', underscore: 'libs/underscore-min', mustache: 'libs/mustache' } }); require(['backbone', 'mustache'], function(Backbone, Mustache) { var TestView = Backbone.View.extend({ el: $('<div>'), initialize: function() { this.render(); }, render: function() { var person = { firstName: 'Bar', lastName: 'Foo', blogURL: 'http://example.com' }; var tmp = '<h1>{{firstName}} {{lastName}}</h1>' tmp += '<p>Blog: {{blogURL}}</p>'; $(this.el).html(Mustache.to_html(tmp, person)); } }); var v = new TestView(); $('#contents').html(v.el); });
ãã®ä»ã®ãã¡ã¤ã«
index.html
script ã®èªã¿è¾¼ã¿ãã²ã¨ã¤ã«ãªãã®ã§å¬ããã
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>hello, world.</h1> <div id="contents"></div> <!-- script include --> <script data-main="js/main" src="js/libs/require.js"></script> </body> </html>
style.css
ç¹ã«ã¹ã¿ã¤ã«ã¯å¿ è¦ç¡ãã®ã§ç©ºã
æå¾ã«ã¾ã¨ããæ¸ã
- RequireJS ã§èªã¿è¾¼ãã
- html ãã¡ã¤ã«ã§ script ãä¸ã¤ã§æ¸ãã®ã§å¬ããã
èªåãä½ããµã¤ãã¨ã㦠script ã¿ã°ãããããå¿ è¦ãªã®ã¯é¿ãããã£ãã®ã§ãRequireJS ã使ã£ã¦æåã«èªã¿è¾¼ããã¡ã¤ã«ãã²ã¨ã¤ã«ã§ããã®ã¯å¬ããã
RequireJS ã®ä¾å管çã¯è©³ããã¯ç¥ãã¾ããããä¸åº¦èªã¿è¾¼ãã ãã¡ã¤ã«ã¯èªã¿è¾¼ã¾ãªããªã©ã
ããã¯ãããªã«ãã£ã¦ããããã ã¨æããã
åè
1. Loading mustache using requirejs
2. RequireJS API - Configuration Options
3. Backbone.js入門 「Events」
4. Christophe Coenraets - Tutorial: HTML Templates with Mustache.js
5. sitepoint - A Minimal HTML Document (HTML5 Edition)
追è¨
å½åã¯ãRequireJS ã®2.1 ããå°å
¥ããããã¨è¨è¼ããã¦ãã¾ãããã
2.0 ã®èª¤ãã§ãããææãããã¨ããããã¾ãããä¿®æ£ãã¾ããã