1. Einführung RequireJS Legacy-Code
RequireJS im Einsatz
Andreas Wolf
SYN Systems GmbH
9. Juni 2013
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
3. Einführung RequireJS Legacy-Code
$ whois referent
Andreas Wolf
aw@syn-systems.com
Jabber: awolf@jabber.ccc.de
Twitter: @andreaswo
aus Karlsruhe, Baden(-Württemberg)
TYPO3-User seit 2003
Mitglied im TYPO3-Core-Team seit März 2011
RequireJS-User seit irgendwann 2012
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
4. Einführung RequireJS Legacy-Code
Motivation
In traditionellem JS-Code:
Abhängigkeiten verwalten ist schwer
Nachladen ist kompliziert (script-Tags manuell in DOM einhängen)
globaler Namensraum verschmutzt schnell
nur eine Version einer Bibliothek gleichzeitig ladbar
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
5. Einführung RequireJS Legacy-Code
Was ist RequireJS?
RequireJS is a JavaScript le and module loader. It is
optimized for in-browser use, [. . . ]. Using a modular script
loader like RequireJS will improve the speed and quality of
your code.
(von requirejs.org)
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
6. Einführung RequireJS Legacy-Code
RequireJS
implementiert AMD-Standard (Asynchronous Module De nition)
lädt Module, löst dabei Abhängigkeiten auf
kann auch Legacy-Code verwendbar machen
kommt meistens mit maximal zwei script-Tags aus
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
7. Einführung RequireJS Legacy-Code
Einfaches Beispiel
De nieren (Library/Modulname.js):
1 define('Library/Modulname', ['dependency1', 'dependency2
'], function() {
2 // Here be dragons
3 });
Einbinden (Main.js):
1 require(['Library/Modulname'], function(Modul) {
2 // More dragons here
3 });
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
8. Einführung RequireJS Legacy-Code
Kon guration
1 require.config({
2 paths: {
3 "flot": 'Vendor/flot/jquery.flot'
4 },
5 shim: {
6 "flot": {
7 deps: 'jquery'
8 }
9 }
10 });
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
9. Einführung RequireJS Legacy-Code
Modulde nition
1 <head>
2 ...
3 <script type="text/javascript" src="require-config.js"
></script>
4 <script type="text/javascript" src="require-2.1.6.js"
data-main="main.js"></script>
5 </head>
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
10. Einführung RequireJS Legacy-Code
Nutzen von Dependencies
1 require(['MeinModul', 'jquery'], function(Modul, $) {
2 $('a').click(function() {
3 alert("Link geklickt!");
4 });
5
6 Modul.meineFunktion();
7 });
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share
11. Einführung RequireJS Legacy-Code
Umgehen mit Legacy-Code
nicht jedes Modul benutzt de ne()
die Lösung: "shim"
shim ermöglicht
RequireJS kapselt Module, die in globalen Namensraum schreiben
(siehe shim[<modulname>]['exports'])
RequireJS im Einsatz Andreas Wolf, SYN Systems GmbH
Inspiring people to
share