Revision ad2c90ade9e4 () - Diff

Link to this snippet: https://friendpaste.com/2d93AykCSlJ05AVYE37SJ4
Embed:
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
/*

Class: Controllers

This is a namespace for all the classes that control interactive behavior on the
page. It also manages event messaging so that controllers can communicate with
each other.

*/
var Controllers = new Class({

Implements: Events,
initialize: function(cssPrefix) {
this.cssPrefix = cssPrefix;
this.controllers = [];
this.countdown = 2;
window.addEvent('domready', this.onReady.bind(this));
this.addEvent('onReady', this.onReady.bind(this));
},
onReady: function() {
// In order to initialize controllers safely, wait to make sure that both
// the DOM is ready and that the 'onReady' custom event has been fired.
// Firing 'onReady' indicates that all controllers have been added.
this.countdown--;
if (this.countdown == 0) {
this.initializeControllers();
}
},
initializeControllers: function() {
// After the page loads, search for elements with particular CSS classes
// that correspond to JavaScript behavior controllers.
for (property in this) {
if ($type(this[property]) != 'class') {
continue;
}
$$('.' + this.cssPrefix + property).each(function(el) {
try {
var controller = this[property];
this.controllers.push(new controller(el, this));
} catch(e) {
dbug.log(e.message);
}
}.bind(this));
}
// Notify any listeners that all controllers are done initializing. Also,
// set a top-level CSS class to allow for progressive enhancement.
this.fireEvent('onInitialize');
$(document.body).addClass(this.cssPrefix + 'enabled');
}
});

dbug.enable();
var MoMA = new Controllers('JS_');

MoMA.Calendar = new Class({ /* ... */ });
MoMA.Promotions = new Class({ /* ... */ });
// Plus a bunch more

MoMA.fireEvent('onReady');