-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpresentation.js
579 lines (288 loc) · 8.71 KB
/
presentation.js
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
/**
* EMBER.JS
*
* 1. Ember in a nutshell
* 2. Model objects and classical inheritance
* 3. Building an application
* 4. Building UI
* 5. Game Changer
*/
/*******************************************************************************
1. EMBER IN A NUTSHELL
*
* Ember is a javascript library which facilitates
* building rich applications on the client side
*/
/**
* OKAY... WHAT ABOUT OUTSIDE OF A NUTSHELL?
*
* Ember provides js infrastructure for the following:
*
* - data bindings
*
* - binding-aware templates
*
* - observers and dependant, computed properties
*
* - classical object inheritance
*
* - buffered DOM update
*/
/**
* CHECK OUT THIS CHESS APP! Get it...? "Check"... huhn? Anybody?
*
* To show off Ember and demonstrate how it works, we're going to
* become best MATES with an application which plays through chess games.
*/
/*******************************************************************************
2.MODEL OBJECTS AND CLASSICAL INHERITANCE
*/
/**
* Base class for chess pieces
*
* Note the 'extend' method
*/
Chess.Piece = Em.Object.extend({
symbol: '',
color: null,
unicode: {black: '', white: ''},
position: {rank: -1, file: ''},
toString: function() {
return this.get('color') + this.get('symbol');
}
});
Chess.King = Chess.Piece.extend({
symbol: 'K',
unicode: {
black: '♚',
white: '♔'
}
});
Chess.Queen = Chess.Piece.extend({
symbol: 'Q',
unicode: {
black: '♛',
white: '♕'
}
});
/* and so on...*/
/**
* Note that for our Piece objects, we're calling 'create' rather than
* 'extend' now
*/
Chess.chessSet = Em.ArrayProxy.extend({
content: [
Chess.Rook.create({ color: 'white', position: {rank: 1, file: 'a'} }),
Chess.Knight.create({ color: 'white', position: {rank: 1, file: 'b'} }),
/** ...snip!... **/
Chess.Pawn.create({ color: 'black', position: {rank: 7, file: 'g'} }),
Chess.Pawn.create({ color: 'black', position: {rank: 7, file: 'h'} })
]
});
/**
* Digression: ArrayProxy
*
* ArrayProxy is one of the base classes Ember provides. It's often
* the workhorse of Ember apps since I've found it to be great for data
* driven apps.
*
* The reason we would proxy an array is two-fold:
* - so that we can cleanly enhance the array with convenience functions
* (like `.forEach` and `.filter`)
* - to enable data binding/observing on arrays
* (spoilers! We're not at bindings yet...)
*/
/**
* Note that we're really taking this "modelling" business seriously here...
* this is the essence of chess game, without anything telling us how
* to play it, or how to display it.
*/
Chess.theImmortalGame = Chess.Game.create({
title: 'The Immortal Game',
whitePlayer: 'Adolf Anderssen',
blackPlayer: 'Lionel Kieseritzky',
moves: [
{
pieceSymbol: 'P',
color: 'white',
fromPos: {file: 'e', rank: 2},
toPos: {file: 'e', rank: 4},
comment: ''
},
{
pieceSymbol: 'P',
color: 'black',
fromPos: {file: 'e', rank: 7},
toPos: {file: 'e', rank: 5},
comment: ''
}
/** ... snip! a whole bunch of moves ... **/
]
});
/*******************************************************************************
3. BUILDING AN APPLICATION
*
* Alright! We've modelled chess... now can we play?
*/
/**
* Chess.gameController is the heart of this app, taking care of the
* actual business of managing the state of the game
*/
Chess.gameController = Em.Object.create({
/**
* The game. Ideally set this to something, otherwise
* this isn't a very interesting app...
*/
gameDefinition: Chess.Game.create(),
/**
* We maintain the lists of which pieces have been captured as the game
* progresses
*
* Still in `Chess.gameController = Em.Object.create({`
*/
captured: Em.Object.create({
white: Em.ArrayProxy.create({content: []}),
black: Em.ArrayProxy.create({content: []})
}),
/**
* A 'board' to place the game's pieces on. By mirroring an actual
* board, this makes piece lookup really easy
*
* Still in `Chess.gameController = Em.Object.create({`
*/
board: {
/** ... snip! create 8x8 board ...**/
},
/**
* Place a 'chess set' on the 'board'.
*
* Note that `init` is automatically called by Ember on construction
*
* Note also the "_super" invocation
*
* Still in `Chess.gameController = Em.Object.create({`
*/
init: function() {
var ret = this._super();
// neat-o ArrayProxy.forEach
Chess.chessSet.get('content').forEach(function(piece) {
this.board[piece.get('position').rank][piece.get('position').file]
= piece;
}, this);
return ret;
}
/**
* Still in `Chess.gameController = Em.Object.create({`
*
* MEGA SNIP!!! the following methods are also defined, but they're
* pretty boring in that they work exactly as you'd expect
* - currentMove: move the game is on
* - nextMove: play the next move
* - play: autoplay moves
* - pause: pause autoplay of moves
* - playing: whether or not the game is autoplaying
*/
});
/**
* AAAANNNNNDD... WE'RE DONE!
*
* We have a fully functioning app which can be used to step through
* a given chess game.
*
* What's that? You want a UI? Oh, all right...
*/
/*******************************************************************************
4. BUILDING UI
*
* Ember's real power comes into play when we're displaying all this
* stuff and making it interactive. Recall my list of features from earlier:
* a. data bindings
* b. binding-aware templates
* c. observers and dependant, computed properties
* d. classical object inheritance
* e. buffered DOM updates
*
* We just built a totally sweet (headless)chess app, and we've really only
* talked only about one of these.
*
* Let's slap a UI on it an check out the rest...
*/
/**
* Markup time!
*
* This is a fine time to peruse index.html, noting how the templates
* look. We're going to inspect {{view capturedWhiteView}} pretty closely
* next, so that's worth giving a gander...
*/
/**
* POWER: BINDINGS, TEMPLATES, OBSERVERS AND COMPUTED PROPERTIES
*
* I like to think of Ember as letting you develop you app as if
* you only code to a single moment in time: take this value, do this thing.
*
* Then, Ember takes care of reliving that moment every time the value
* changes.
*
* Consider {{view capturedWhiteView}} from our template...
*/
Chess.GameInfoView = Em.View.extend({
/** snip! **/
capturedWhiteView: Em.CollectionView.extend({
// 'Binding' suffix is magic... but, like, good magic
contentBinding: 'parentView.game.captured.white',
itemViewClass: Chess.PieceView.extend({
pieceBinding: 'content'
})
})
/**... snip! **/
});
/**
* Dependant, computed property example
*/
Chess.PlayButton = Chess.Button.extend({
playingBinding: 'Chess.gameController.playing',
/**... snip! **/
/**
* This determines the correct text for the "Play" button
*/
text: function() {
return this.get('playing') ? 'Pause' : 'Play';
}.property('playing')
});
/**
* Observers in action
*/
Chess.PieceView.extend({
/** snip! **/
positionDidChange: function() {
/** snip! out some code which does the following: **/
// computes the desired top/left DOM position for this piece
// as determined by its rank/file board position, then
// animates it to that position
}.observes('piece.position')
});
/**
* Buffered DOM updates
*
* Just take my word for it.
*/
/*******************************************************************************
5. GAME CHANGER
*
* To show off how this app can view any chess game defined in an object
* of type Chess.Game, in index.html, swap out the line:
* `Chess.gameController.set('game', Chess.theImmortalGame);`
* with:
* `Chess.gameController.set('game', Chess.theAmateurGame);`
*
* You may now view a replay of one of my historical games! (I'm black...)
*/
/**
* Fun fact: we've actually seen most of the code in the Chess app.
*
* The app is pretty polished (not just a demo sham!), so if anyone wants a
* closer look at the details, I'd love to share.
*/
/**
* QUESTIONS?
*/