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

Using RequireJS for building modular JavaScript...

Sebastian Kurfürst
February 22, 2013
2.3k

Using RequireJS for building modular JavaScript applications

From the jQuery Europe 2013, in Vienna on 22.02.2013.

Sebastian Kurfürst

February 22, 2013
Tweet

Transcript

  1. <script src="jQuery.js"></script> <script src="jQuery.somePlugin.js"></script> <script src="backbone.js"></script> <script src="jQuery.otherPlugin.js"></script> <script src="app/model/foo.js"></script>

    <script src="underscore.js"></script> <script src="app/util/arrays.js"></script> <script src="app/model/bar.js"></script> <script src="app/controller/some.js"></script> <script src="d3.js"></script> <script src="app/views/test.js"></script> <script src="app.js"></script> <script src="jQuery.js"></script> <script src="jQuery.somePlugin.js"></script> <script src="backbone.js"></script> <script src="jQuery.otherPlugin.js"></script> <script src="app/model/foo.js"></script> <script src="underscore.js"></script> <script src="app/util/arrays.js"></script> <script src="app/model/bar.js"></script> <script src="app/controller/some.js"></script> <script src="d3.js"></script> <script src="app/views/test.js"></script> <script src="app.js"></script> <script src="jQuery.js"></script> <script src="jQuery.somePlugin.js"></script> <script src="backbone.js"></script> <script src="jQuery.otherPlugin.js"></script> <script src="app/model/foo.js"></script> <script src="underscore.js"></script> <script src="app/util/arrays.js"></script> <script src="app/model/bar.js"></script> <script src="app/controller/some.js"></script> <script src="d3.js"></script> <script src="app/views/test.js"></script> <script src="app.js"></script> <script src="jQuery.js"></script> <script src="jQuery.somePlugin.js"></script> <script src="backbone.js"></script> <script src="jQuery.otherPlugin.js"></script> <script src="app/model/foo.js"></script> <script src="underscore.js"></script> <script src="app/util/arrays.js"></script> <script src="app/model/bar.js"></script> <script src="app/controller/some.js"></script> <script src="d3.js"></script> <script src="app/views/test.js"></script> <script src="app.js"></script> Freitag, 22. Februar 13
  2. app view jQuery controller model I need model, controller, view

    to do my job. I need jQuery to do my job. Freitag, 22. Februar 13
  3. // app.js define( dependencies, callback ); app I need model,

    controller, view to do my job. Freitag, 22. Februar 13
  4. // app.js define( dependencies, callback ); module name == file

    name except .js file extension app I need model, controller, view to do my job. Freitag, 22. Februar 13
  5. // app.js define( dependencies, callback ); module name == file

    name as soon as those are loaded... ... execute this callback except .js file extension app I need model, controller, view to do my job. Freitag, 22. Februar 13
  6. // app.js define( ["model", "controller", "view"], callback ); app I

    need model, controller, view to do my job. Freitag, 22. Februar 13
  7. // app.js define( ["model", "controller", "view"], function(model, controller, view) {

    } ); dependent modules are passed as arguments app I need model, controller, view to do my job. Freitag, 22. Februar 13
  8. // app.js define( ["model", "controller", "view"], function(model, controller, view) {

    } ); dependent modules are passed as arguments app I need model, controller, view to do my job. Freitag, 22. Februar 13
  9. // controller.js define( ["jQuery"], function($) { var controller = {};

    // build up controller as needed return controller; } ); the module is returned from the callback function controller I need jQuery to do my job. Freitag, 22. Februar 13
  10. •define() modules •file name == module name •callback arguments ==

    module dependencies •return value == module A M D Freitag, 22. Februar 13
  11. // app.js define( ["model", "controller", "view"], function(model, controller, view) {

    } ); // controller.js define( ["jQuery"], function($) { var controller = {}; // build up controller as needed return controller; } ); 1 AMD modules Freitag, 22. Februar 13
  12. <script src="require.js" data-main="app.js"> </script> // app.js define( ["model", "controller", "view"],

    function(model, controller, view) { } ); // controller.js define( ["jQuery"], function($) { var controller = {}; // build up controller as needed return controller; } ); 1 AMD modules 2 bootstrap Freitag, 22. Februar 13
  13. // buildconfig.js baseUrl: 'src/', paths: { 'jQuery': 'lib/jquery/jquery' }, shim:

    {} // lots of other options base URL path mapping non-AMD code Freitag, 22. Februar 13
  14. // simplified var root = window; root.Backbone = {}; //

    remaining backbone here non-AMD code Freitag, 22. Februar 13
  15. define(['underscore', 'jquery'], function(_, jQuery) { var root = {}; root.Backbone

    = {}; // remaining backbone here return root.Backbone; }); non-AMD code fail Freitag, 22. Februar 13
  16. // before loading require.js require.config({ shim: { 'backbone': { exports:

    'Backbone', deps: ['underscore', 'jquery'] } } }); non-AMD code use shims Freitag, 22. Februar 13
  17. var myTemplate = 'Welcome {name} ... I hope you enjoy

    {conferenceName}'; text dependencies fail Freitag, 22. Februar 13
  18. var myTemplate = 'Welcome {name}\ \ ... I hope you

    enjoy {conferenceName}'; text dependencies Freitag, 22. Februar 13
  19. var myTemplate = 'Welcome {name}\ \ ... I hope you

    enjoy {conferenceName}'; text dependencies var myTemplate = ['Welcome {name}', '', '... I hope you enjoy {conferenceName}' ].join("\n"); Freitag, 22. Februar 13
  20. var myTemplate = 'Welcome {name}\ \ ... I hope you

    enjoy {conferenceName}'; text dependencies var myTemplate = ['Welcome {name}', '', '... I hope you enjoy {conferenceName}' ].join("\n"); fail Freitag, 22. Februar 13
  21. // app.coffee define( ['cs!model', 'cs!controller'] (model, controller) -> # do

    something here ) CoeeScript Freitag, 22. Februar 13
  22. RequireJS and jQuery // make sure to specify a shim

    config for // jquery.slider and jquery.tooltip define( ["jquery", "jquery.slider", "jquery.tooltip"], function($) { // the jquery.slider.js and jquery.tooltip.js // have been loaded. $(function() { $('body').slider(); }); }); Freitag, 22. Februar 13
  23. RequireJS and jQuery // make sure to specify a shim

    config for // jquery.slider and jquery.tooltip define( ["jquery", "jquery.slider", "jquery.tooltip"], function($) { // the jquery.slider.js and jquery.tooltip.js // have been loaded. $(function() { $('body').slider(); }); }); Freitag, 22. Februar 13
  24. Finding Missing Dependencies forgot to specify dependency app jQuery jQuery.slider

    dependency specification jQuery.tooltip Freitag, 22. Februar 13
  25. Finding Missing Dependencies forgot to specify dependency app jQuery jQuery.slider

    dependency specification jQuery.tooltip Freitag, 22. Februar 13
  26. Finding Missing Dependencies forgot to specify dependency app jQuery jQuery.slider

    dependency specification jQuery.tooltip Freitag, 22. Februar 13
  27. Finding Missing Dependencies forgot to specify dependency app jQuery jQuery.slider

    dependency specification jQuery.tooltip Freitag, 22. Februar 13
  28. Finding Missing Dependencies // in all JS files: define(..., function()

    { if (window._requirejsLoadingTrace) window._requirejsLoadingTrace.push('jquery.slider'); // source code follows here }); 1 instrumentation Freitag, 22. Februar 13
  29. Finding Missing Dependencies // for error and successful runs, //

    do the following in console: window._requirejsLoadingTrace 2 collect traces http://git.typo3.org/FLOW3/Packages/TYPO3.TYPO3.git/blob/HEAD:/Scripts/missing-javascript-dependency-finder.php put them into the dependency finder: $successfulRuns = array( json_decode('["jquery", "jquery.slider", "jquery.tooltip", "app"]') ); $errorRuns = array( 'error description' => json_decode('["jquery.slider", "jquery", "jquery.tooltip", "app"]') ) Freitag, 22. Februar 13
  30. Finding Missing Dependencies $ php missing-javascript-dependency-finder.php jquery.slider -> jquery Found

    1 orders which might be responsible for loading order problems. 3 check output Freitag, 22. Februar 13