Skip to content

Commit ef83ab6

Browse files
committed
pub / sub design pattern example - 16.08.2015
1 parent a01ed46 commit ef83ab6

File tree

8 files changed

+146
-59
lines changed

8 files changed

+146
-59
lines changed

EXERCISE/Exercise 4/js/events.js renamed to EXERCISE/Exercise 4/app/components/events.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
window.app = window.app || {};
2-
31
/***
42
* Subsciing and Publishing application events
53
*/
6-
app.events = {
4+
var Events = {
75

86
/***
97
* Create event

EXERCISE/Exercise 4/js/controller.js renamed to EXERCISE/Exercise 4/app/controllers/controller.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,50 @@ var app = app || {};
1313
var config = {
1414
el: '.wrapper'
1515
},
16-
16+
1717
/***
1818
* Cached App elements
1919
*/
2020
elements = {},
21-
2221
instanceStorage = [];
23-
22+
2423
/**
2524
* Initialize Application
2625
* @public
2726
*/
2827
app.init = function () {
29-
28+
3029
cacheElements();
31-
setConfig();
32-
createLayout();
30+
this.bindEvents();
31+
app.events.notify('app:layout:cached');
32+
app.events.notify('app:ready');
33+
};
34+
35+
app.layout = {};
36+
37+
/***
38+
* Binds apllication wide events
39+
*/
40+
app.bindEvents = function () {
41+
42+
app.events.listen('app:ready', setConfig.bind(this));
43+
app.events.listen('app:layout:cached', createLayout.bind(this));
44+
};
45+
46+
/***
47+
* Internal application events
48+
*/
49+
app.events = {
50+
51+
listen: function (eventName, handler) {
52+
53+
Events.subscribe(elements.el, eventName, handler);
54+
},
55+
56+
notify: function (eventName) {
57+
58+
Events.publish(elements.el, eventName);
59+
}
3360
};
3461

3562
/**
@@ -38,8 +65,8 @@ var app = app || {};
3865
* returns {Object} config
3966
*/
4067
app.getConfig = function () {
41-
42-
return config;
68+
69+
return config;
4370
};
4471

4572
/**
@@ -48,27 +75,33 @@ var app = app || {};
4875
* returns void
4976
*/
5077
app.exit = function () {
51-
78+
5279
for (var i = instanceStorage.length - 1; i >= 0; i--) {
5380
instanceStorage[i].destroy();
5481
};
5582
};
56-
83+
5784
/**
5885
* Create application layout
5986
*/
6087
function createLayout () {
61-
62-
for (var template in app.templates) {
63-
instanceStorage.push(new app.view(app.templates[template]));
88+
89+
for (var templateName in this.templates.layout) {
90+
91+
app.layout[templateName] = new this.layoutView({
92+
template: this.templates.layout[templateName],
93+
name: templateName
94+
});
95+
96+
app.events.notify('view:' + templateName + ':render');
6497
}
6598
};
6699

67100
/**
68101
* Get user specific configuration
69102
*/
70103
function setConfig () {
71-
104+
72105
config.appWidth = global.innerWidth;
73106
config.appHeight = global.innerHeight;
74107
}
@@ -77,7 +110,7 @@ var app = app || {};
77110
* Cache app main elements
78111
*/
79112
function cacheElements () {
80-
113+
81114
elements.el = doc.querySelector(config.el);
82115
}
83116

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***
2+
* Stored templates as plain text
3+
*/
4+
var app = app || {};
5+
6+
(function () {
7+
8+
app.templates = {
9+
10+
layout: {
11+
header: '<header id="template-header">Header</header>',
12+
footer: '<footer id="template-footer">Footer</footer>',
13+
content: '<section id="template-content">CONTENT</section>'
14+
},
15+
16+
windowTemplate: '<div>Window :) yeeeh</div>'
17+
};
18+
})();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/***
2+
* Layout view
3+
*/
4+
var app = app || {};
5+
6+
(function (){
7+
8+
/***
9+
* @param {Object} options
10+
* returns void
11+
*/
12+
function layoutView (options) {
13+
14+
this.template = options.template;
15+
this.name = options.name;
16+
this.placeholder = document.getElementById('placeholder-' + options.name);
17+
18+
19+
20+
21+
this.render = function () {
22+
23+
this.placeholder.innerHTML = this.template;
24+
this.el = document.getElementById('template-' + options.name)
25+
};
26+
27+
this.destroy = function () {
28+
console.log('destroy', this.template);
29+
};
30+
31+
app.events.listen('view:' + this.name + ':render', this.render.bind(this));
32+
};
33+
34+
app.layoutView = layoutView;
35+
})();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/***
2+
* Window view
3+
*/
4+
var app = app || {};
5+
6+
(function (){
7+
8+
/***
9+
* @param {Object} options
10+
* returns void
11+
*/
12+
function windowView (options) {
13+
14+
//Execpional template or default template
15+
this.template = options.template || app.templates.windowTemplate;
16+
this.id = options.id;
17+
this.type = options.type; //minimized or maximized
18+
19+
this.render = function () {
20+
21+
app.layout.content.el.innerHTML += this.template;
22+
};
23+
24+
this.destroy = function () {
25+
console.log('destroy', this.template);
26+
};
27+
28+
app.events.listen('window:' + this.id + ':render', this.render.bind(this));
29+
};
30+
31+
window.windowView = windowView;
32+
})();

EXERCISE/Exercise 4/index.html

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
</head>
88
<body>
99

10-
<div class="wrapper"></div>
10+
<div class="wrapper">
11+
<div id="placeholder-header">
1112

12-
<script type="text/javascript" src="js/templates.js"></script>
13-
<script type="text/javascript" src="js/events.js"></script>
14-
<script type="text/javascript" src="js/base-view.js"></script>
15-
<script type="text/javascript" src="js/controller.js"></script>
13+
</div>
14+
<div id="placeholder-content"></div>
15+
<div id="placeholder-footer"></div>
16+
</div>
17+
18+
<script type="text/javascript" src="app/components/events.js"></script>
19+
<script type="text/javascript" src="app/templates/templates.js"></script>
20+
<script type="text/javascript" src="app/views/layout-view.js"></script>
21+
<script type="text/javascript" src="app/views/window-view.js"></script>
22+
<script type="text/javascript" src="app/controllers/controller.js"></script>
1623

1724
</body>
1825
</html>

EXERCISE/Exercise 4/js/base-view.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

EXERCISE/Exercise 4/js/templates.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)