-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
587 lines (490 loc) · 22.2 KB
/
index.html
File metadata and controls
587 lines (490 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Feedback: AngularJS + TypeScript at Serenytics</title>
<meta name="description" content="The Why, the HowTo and the Good/Bad/Ugly parts of using TypeScript with AngularJs">
<meta name="author" content="Adrien Chauve">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/solarized.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-51923804-2', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h2>Feedback</h2>
<h3>AngularJS + TypeScript <a href='http://www.serenytics.com'>at Serenytics</a></h3>
<p>
<small>TypeScript meetup #2 - Paris<br>2015-06-10</small>
</p>
<p>
<small>Adrien Chauve <a href="http://twitter.com/adrienchauve">@adrienchauve</a><br>CTO <a href="http://twitter.com/Serenytics">@Serenytics</a></small>
</p>
<aside class="notes">
<ul>
<li>I'm also a fullstack dev at the moment ;-)</li>
<li>Serenytics: early stage startup that buils a SASS in Analytics and BI; go see the website for more</li>
<li>Use AngularJS for our SPA and decided a short time ago to switch to TypeScript</li>
<li>This prez is about our feedback after a few months using Angular+TypeScript</li>
</ul>
</aside>
</section>
<section>
<h2>disclaimer</h2>
<ul>
<li>I'm not a TypeScript expert, nor an AngularJS expert!</li>
<li>But it's better if you know some to follow this talk ;-)</li>
</ul>
</section>
<section>
<h2>Contents</h2>
<ol>
<li><a href="#/part-1">What we do: Dashboards and Widgets</a></li>
<li><a href="#/part-2">Why moving to TypeScript?</a></li>
<li><a href="#/part-3">Angular + TypeScript: HowTo?</a></li>
<li><a href="#/part-4">The Good, the Bad and the Ugly</a></li>
</ol>
<aside class="notes">
<ul>
<li>First: what we do because it's a key point why we chose to begin the migration to TypeScript</li>
</ul>
</aside>
</section>
<section>
<h2>1. What we do: Dashboards and Widgets</h2>
</section>
<section>
<h3>1. What we do: Dashboards and Widgets</h3>
<iframe src="https://app.serenytics.com/viewer/webapps/423c4b62-bd6c-4fb9-aed0-50c56b39303e"
width="100%"
scrolling="no"
frameBorder="0"
class="stretch"></iframe>
<aside class="notes">
<ul>
<li>Actually we do more than that and there is a big backend part in python but it's out of topic here</li>
</ul>
</aside>
</section>
<section id="part-1">
<h3>1. What we do: Dashboards and Widgets</h3>
<p><strong>Initial question:</strong></p>
<ul>
<li>we use AngularJS with lots of different widgets</li>
<li>how to reuse as much code as possible?</li>
<li>while still being able to tune each widget appearance</li>
</ul>
<p><strong>Solutions:</strong></p>
<ul>
<li>Service: not enough (factorize logic but not UI interactions)</li>
<li>Single generic directive: single template problem</li>
<li>Directive composition: a generic base directive plus several small directives to adapt the template and behavior</li>
<li>TypeScript to the rescue, and much much more!</li>
</ul>
<aside class="notes">
<ul>
<li>code reuse as opposed to duplicating the code for by instance our ten widgets (=> ten directives, it was the case, at first!)</li>
<li>single directive, we need full customization available for each widget</li>
<li>Problem in directive composition: transclusion, shared isolated scope between several directives, reuse of Controller of other directive is *NOT* straightforward and not worth the pain in my opinion</li>
<li>TypeScript to the rescue: could actually be done in pure ES5 but lot more verbose, and loose all other goodies of TypeSCript</li>
</ul>
</aside>
</section>
<section id="part-2">
<h2>2. Why moving to TypeScript?</h2>
</section>
<section>
<h3>2. Why moving to TypeScript? (1/2)</h3>
<ul>
<li>Potential good solution to factorize our code (more on that later)</li>
<li>All the goodness of ES6 (classes, fat arrow, template strings, soon async/await, ...), plus:</li>
<li>statically typed
<ul>
<li>automatic feedback while developing (think gulp/grunt watch)</li>
<li>interfaces! description of complex types (e.g. widget data model) available in a <strong>single place</strong> and not
spread around the code (Angular is really persmissive for models)
</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>2. Why moving to TypeScript? (2/2)</h3>
<ul>
<li>It's just a Javascript superset, so the migration can be <strong>incremental and smooth</strong>, no need to rewrite the app from scratch</li>
<li>really easy integration with Angular (even if a little scary at first)</li>
<li>forces to use classes, and then better organize the code (again Angular is really permissive)</li>
<li>Angular2 is written in TypeScript: Google + Microsoft are now supporting it</li>
</ul>
<aside class="notes">
<ul>
<li>migration is an iterating process, really crucial as an early-stage startup</li>
<li>classes are also a good way to organize code when you're used to them in your backend programming language like python</li>
</ul>
</aside>
</section>
<section id="part-3">
<h2>3. Angular + TypeScript: HowTo?</h2>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics</h3>
<ul>
<li>Controller</li>
<li>Service</li>
<li>Directive</li>
</ul>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics - Controllers</h3>
<p>Using ControllerAs syntax, a controller is just a Class</p>
<pre><code data-trim>
angular
.module('my-lib')
.controller('LoginController', LoginController);
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'mylib/auth/login.template.html',
controller: 'LoginController',
controllerAs: 'vm'
})
</code></pre>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics - Controllers</h3>
<p>Example in ES5:</p>
<pre class="stretch"><code data-trim>
var LoginController = (function () {
function LoginController(loginService, $state) {
this.loginService = loginService;
this.$state = $state;
this.invalidCredentials = false;
if (loginService.isLogged) {
$state.transitionTo('home');
}
}
LoginController.prototype.login = function () {
var _this = this;
this.invalidCredentials = false;
this.loginService.loginWithCrendentials(this.email, this.password)
.catch(function () {
_this.invalidCredentials = true;
});
};
return LoginController;
})();
</code></pre>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics - Controllers</h3>
<p>Example in TypeScript: lots of goodness in it</p>
<pre class="stretch"><code data-trim>
class LoginController {
invalidCredentials = false;
email: string;
password: string;
constructor(private loginService: ILoginService,
private $state: angular.ui.IStateService) {
if (loginMgr2.isLogged) {
$state.transitionTo('home');
}
}
login () {
this.invalidCredentials = false;
this.loginService.loginWithCrendentials(this.email, this.password)
.catch(() => {
this.invalidCredentials = true;
});
}
}
</code></pre>
<aside class="notes">
<ul>
<li>really easy to use TypeScript for controller</li>
<li>force us to use ControllerAs and decouple Controller Class from $scope</li>
<li>ES6 classes -> cleaner</li>
<li>type checked! (+completion +jump to source code: super nice!)</li>
</ul>
</aside>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics - Services</h3>
<p>Just like Controllers:</p>
<pre class="stretch"><code data-trim>
class LoginService {
constructor(private Restangular: restangular.IService) {
}
loginWithCrendentials (email: string, password: string) {
return this.Restangular.one('api/token')
.then((apiData) => {
// ... save token
// ... transition to 'home' state
});
}
}
angular
.module('my-lib')
.service(loginService, LoginService);
</code></pre>
<aside class="notes">
<ul>
</ul>
</aside>
</section>
<section>
<h3>3. Angular + TypeScript: The Basics - Directives</h3>
<pre class="stretch"><code data-trim>
interface IWidgetDirectiveScope extends ng.IScope {
widgetModel: IWidgetModel;
}
class WidgetDirective {
scope = {
widgetModel: '=',
};
restrict = 'E';
replace = true;
controllerAs = 'vm'
templateUrl = 'components/widgets/widget.directive.html';
controller = WidgetController;
link = (scope: IWidgetDirectiveScope,
element: ng.IAugmentedJQuery,
attrs: ng.IAttributes,
controller: WidgetController) => {
// ...
}
}
angular.module('my-lib').directive('my-widget', () => {
return new WidgetDirective();
});
</code></pre>
<aside class="notes">
<ul>
<li>not so much work</li>
<li>need to type the scope to take full advantage of TypeScript</li>
<li>open the way to inheritance to factorize simple attributes like restrict, replace, controllerAs, and utils to setup the link function</li>
<li>Thanks to Felix Billon for mentioning the interesting 'bindToController' attribute that is clearly a good choice too :)</li>
</ul>
</aside>
</section>
<section>
<h3>3. Angular + TypeScript: Even more!</h3>
<p>ok Angular + TypeScript is cool, but what about code reuse and our initial question?</p>
<p>Lots of common behavior between</p>
<ul>
<li>table widget / value widget (= single cell table)</li>
<li>all chart widgets (pie chart, bar chart, curve chart, ...)</li>
</ul>
</section>
<section>
<h3>3. Angular + TypeScript: Reuse code!</h3>
<p><strong>Different ways:</strong></p>
<ul>
<li>keep the same controller, adapt the template, 2 directives for the same price!</li>
<li>inherit base controller to inherit basic behavior exposed to the view (think Mixins when available)
<ul>
<li>refresh state (reload data from API)</li>
<li>error handling</li>
<li>global data filtering</li>
<li>data export</li>
</ul>
</li>
<li>implement models (e.g. Widgets) as classes completely outside of Angular's world</li>
</ul>
<aside class="notes">
<ul>
<li>I'm not really comfortable with implementing models as services, as I think service first role is to be singletons, not object constructors</li>
</ul>
</aside>
</section>
<section>
<h3>3. Angular + TypeScript: Our solution for code reuse</h3>
<ul>
<li>keep directives small and simple, and have several if needed
<ul>
<li>each customized with its own template</li>
<li>with possibly one base directive to factorize $scope features and simple properties (replace, ControllerAs, ...)</li>
</ul>
<li>one base controller and several inherited controllers as needed</li>
<li>pure TypeScript Widget classes without any Angular dependency (model/business logic)</li>
</ul>
</section>
<section id="part-4">
<h2>4. The Good, the Bad and the Ugly</h2>
</section>
<section>
<h3>4. Angular + TypeScript: The Good Parts</h3>
<ul>
<li>easy integration with Angular, especially with ControllerAs since 1.2</li>
<li>even encourage to use best practises for Angular 2 (ControllerAs => Components)</li>
<li>incremental migration (superset + gradual typing with any)</li>
<li>type infos, type inference and all the good that comes with it</li>
<li>Interfaces: all the model in one place!</li>
<li>Good debugging experience using source maps with Chrome</li>
</ul>
</section>
<section>
<h3>4. Angular + TypeScript: The Bad Parts (1/2)</h3>
<ul>
<li>using 3rd party libraries (missing or outdated typed definitions): but not such a great problem</li>
<li>dev environment a little more complex (gulp, tsc, tslint, tsd): actually not so much pain</li>
<li>a little more work sometimes (adding types, directives more verbose)</li>
</ul>
<aside class="notes">
<ul>
<li>example of type definitions with use: angularjs of course, angular-ui, jquery, lodash, momentjs, numeraljs, highcharts, and more with the e2e tests in protractor</li>
</ul>
</aside>
</section>
<section>
<h3>4. Angular + TypeScript: The Bad Parts (2/2)</h3>
<small>Dealing with class hierarchies: compromise between testability
and verbosity
</small>
<pre class="stretch"><code data-trim>
class BaseWidgetController {
private _data: IData;
constructor(private globalFilterService: GlobalFilterService /* other dependencies */) { /* ... */}
filterData () { return this.globalFilterService.applyFilters(this._data); }
}
class TableWidgetController extends BaseWidgetController {
constructor(private globalFilterService: GlobalFilterService /* other dependencies */) {
super(globalFilterService, ....);
}
}
/* less verbose alternative - dangerous */
class GlobalFilterService {
/* WARNING: bypass Angular DI and make testing more complex */
static instance() {
angular.element(document.body).injector().get('globalFilterService');
}
applyFilters(...) {...}
}
class BaseWidgetController {
private _data: IData;
constructor() {}
filterData () { return GlobalFilterService.instance().applyFilters(this._data); }
}
</code></pre>
<aside class="notes">
<ul>
Compromise between easy testability with mocking and Angular native DI, and simple access to services outside of Angular but more difficult mocking in tests
</ul>
</aside>
</section>
<section>
<h3>4. Angular + TypeScript: Ugly Parts?</h3>
<p>Not really... or maybe</p>
<p>when coding e2e tests with Protractor + TypeScript: incompatible Promises types</p>
<pre class="stretch"><code data-trim>
// selenimum-webdriver type declaration
interface IThenable<T> {
then<R>(opt_callback?: (value: T) => Promise<R>, opt_errback?: (error: any) => any): Promise<R>;
then<R>(opt_callback?: (value: T) => R, opt_errback?: (error: any) => any): Promise<R>;
}
// vs. ES6 type declaration
interface Thenable<R> {
then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
}
</code></pre>
<p>Good luck if you use nodejs with<br>Q.denodeify or Bluebird.promisify</p>
<aside class="notes">
<ul>
<li>you get this use case if you want to use Q with protractor for instance</li>
<li>The kind of errors you get can remind you of old times with C++ template metaprogramming ;-)</li>
</ul>
</aside>
</section>
<section>
<h3>Towards Angular 2.0: Angular in TypeScript</h3>
<p>If you:</p>
<ul>
<li>have a growing project in Angular 1.X</li>
<li>want to invest on it for the next couple of years</li>
</ul>
<p>Do you a favor, <strong>go for TypeScript!</strong><br>
Congrats! you'll be half way through the migration to Angular2!</p>
<p>Angular 2: everything becomes a TypeScript class with annotations (Component, Directive)</p>
</section>
<section>
<h3>References</h3>
<ul>
<li><a href="http://www.pluralsight.com/courses/using-typescript-large-angularjs-apps">Using TypeScript for Large AngularJS Applications</a> Course @ Pluralsight</li>
<li><a href="http://angular-tips.com/blog/2015/06/why-will-angular-2-rock/">Why Will Angular 2 Rock?</a> by @Foxandxss</li>
<li><a href="https://github.com/johnpapa/angular-styleguide">Angular style guide</a> by @john_papa</li>
<li><a href="https://github.com/Microsoft/TypeScript/wiki/Roadmap">TypeScript Roadmap</a></li>
</ul>
</section>
<section>
<h2>Questions?</h2>
<strong>Want to work with us at <a href="http://www.serenytics.com">Serenytics?</a></strong><br>
<ul>
<li>Interested by Analytics, BI and Startups?</li>
<li>Passionate about Angular and TypeScript? Love Python?</li>
<li>Come and see me, we're looking for an amazing dev / startuper!</li>
<li>Or contact me at <a href="mailto:adrien.chauve@serenytics.com">adrien.chauve@serenytics.com</a></li>
</ul>
<aside class="notes">
<ul>
<li>Thanks, I hope you enjoyed the presentation</li>
<li>Any question?</li>
</ul>
</aside>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script type="text/javascript" src="http://static.serenytics.com/js/iframeResizer.min.js"></script>
<script type="text/javascript">
iFrameResize({
heightCalculationMethod : 'grow'
});
</script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>