Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started with RequireJS

Ayumu Sato
November 16, 2012

Getting Started with RequireJS

社内勉強会で利用した資料から、いろいろ調整したりしてうp

Ayumu Sato

November 16, 2012
Tweet

More Decks by Ayumu Sato

Other Decks in Programming

Transcript

  1. 6 Bad Context Plugins Libs of choice jQuery Backbone Your

    Code source. Dependency Management with RequireJS
  2. 7 Good Context Plugins Libs of choice jQuery Backbone Your

    Code source. Dependency Management with RequireJS
  3. 8 Dependencies Plugins Libs of choice jQuery Backbone Your Code

    source. Dependency Management with RequireJS
  4. FH5)&#*(0/& (function() { var Application = { foo: function() {...},

    bar: function() {...} }; // long historical stacking code... new Application(); })(); 12 ߦ௒͑ͨ͋ͨΓͰڰؾ ࡍݶͳ͘ॏ͘ͳΔΤσΟλ
  5. FH/BNFTQBDF  13 // main.js window.NS = {}; // page.js

    window.NS.Page = { initialize: function() {}, getContent: function() {}, render : function() {} }; // util.js window.NS.Util = {};
  6. FH/BNFTQBDF  // finally <script src=”js/main.js”></script> <script src=”js/util.js”></script> <script src=”js/module_1.js”></script>

    <script src=”js/module_6.js”></script> <script src=”js/module_5.js”></script> <script src=”js/module_2.js”></script> <script src=”js/module_3.js”></script> <script src=”js/module_4.js”></script> <script src=”js/module_7.js”></script> <script src=”js/page.js”></script> 15 ಡΈࠐΈॱͱ͍͏໊ͷґଘੑ஍ࠈ ͍ͯ͏͔खॻ͖ϜϦ
  7. AEFpOFA /** * @method define * * @param {String} [id]

    * @param {Array} dependencies * @param {Function} factory */ define(id, dependencies, factory); 17
  8. +BWB4DSJQU QBHFKT // page.js (depend moduleA & Util) define(['moduleA', 'util'],

    function(moduleA, Util) { function Page($elm) { this.$elm = $elm Util.greatFunction(); } Page.prototype.render = function() { return this.$elm.html(moduleA.getContent()); } return Page; }); 18
  9. +BWB4DSJQU NBJOKT // main.js define(['jquery', 'page'], function($, Page) { //

    initialize new Page($('#container')); // some business Page.render(); }); 19
  10. )5.- <!-- Simple! --> <script data-main="main" src="require.js"> </script> 20 ໰୊͸ղܾ͞Εͨʂ

    <!-- Same code --> <script src="require.js"></script> <script> require(['main'], function(Main) { }); </script>
  11. 6TJOHSKT % r.js -o out=js/all.js\ baseUrl=js\ name=bootstrap\ mainConfigFile=js/bootstrap.js # mainConfigFileʹؚ·ΕΔbaseUrl͸ɺ

    # ࢦఆϑΝΠϧ͔Βͷ૬ରʹͳͬͯ͠·͏ͷͰɺ # ΦϓγϣϯͰࢦఆ͢Δ΄͏͕Α͍ 39
  12. "MNPOE % r.js -o out=js/all.js\ baseUrl=js\ name=lib/almond\ include=bootstrap\ mainConfigFile=js/bootstrap.js #

    ඇಉظϩʔυॲཧͳͲɺෆཁͳػೳΛআ͍ͨshim # 1ϑΝΠϧʹ·ͱΊΔ͜ͱ # ґଘϞδϡʔϧ໊Λಈతʹࢦఆ͠ͳ͍͜ͱ 40
  13. define(['backbone'], function(Backbone) { return Backbone.Model.extend({ url: 'path/to', defaults: { foo:

    'bar' }, validation: { // ... } }); // ऄ଍ newͨ͠ͷΛreturn͢ΔͱsingletonΆ͍ }); 42 "QQFOEJYXJUI#BDLCPOF
  14. "QQFOEJYXJUI+BTNJOF // SpecRunner.html require(['spec/util', 'spec/main'], function() { var env =

    jasmine.getEnv(); env.addReporter(new jasmine.HtmlReporter); env.execute(); }); // spec.coffee define ['util'], (Util)-> describe 'Commmon Utilities', -> util = null beforeEach -> util = new Util it 'Should `plzTruthy` returns true', -> expect(Util.plzTruthy()).toBe true 43
  15. "QQFOEJY&YJTUJOH.PEVMF // ͨͱ͑͹ Zepto ($) // `define`͕ఆٛ͞Ε͍ͯͯɺAMDΆ͔ͬͨΒ... if (typeof define

    === 'function' && define.amd) { define([], function() { return Zepto; }); } 44
  16. "QQFOEJY$PNNPO+44VHBS define(function(require) { var hoge = require('hoge'), fuga = require('fuga'),

    piyo = require('piyo'); return { hogehoge: hoge.toString(), fugafuga: fuga.toString(), piyopiyo: piyo.toString() } }); 45
  17. "QQFOEJYNBEHF # https://github.com/pahen/node-madge % npm install -g madge % brew

    install graphviz # Ϟδϡʔϧͷґଘؔ܎ΛՄࢹԽ % madge --image out.png --format amd ./app 46
  18.  1BSJT+43FRVJSF+4 TMJEF  .PEVMBS+BWB4DSJQUXJUI".%BOE3FRVJSF+4  3FRVJSF+4 1IPOF(BQ  (FUUJOH4UBSUFE.BOBHJOH$MJFOU4JEF4DSJQUTXJUI3FRVJSFKT

    m*"N/PU.ZTFMG  /BNFTQBDFTBSF0ME4DIPPM]#MPH].JMMFS.FEFJSPT  ".%JT/PUUIF"OTXFS5PN%BMF  +BWB4DSJQUNPEVMFMPBEFSTOFDFTTBSZFWJM ]3VCFO 7FSCPSHI  KBQI S CZ$ISJT4USPN1SFUUZ#BDLCPOFKT7JFXTXJUI 3FRVJSFKT 49