Skip to content

Commit a8df412

Browse files
committed
Exercise 4 files
1 parent 968f59e commit a8df412

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

EXERCISE/Exercise 4/index.html

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

10-
<script type="text/javascript" src="js/main.js"></script>
10+
<div class="wrapper"></div>
11+
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>
1116

1217
</body>
1318
</html>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/***
2+
* Base view
3+
*/
4+
var app = app || {};
5+
6+
(function (){
7+
/***
8+
* @param {String} template
9+
* returns void
10+
*/
11+
function baseView (template) {
12+
13+
this.template = template;
14+
15+
console.log('base view inited');
16+
17+
this.destroy = function () {
18+
console.log('destroy', this.template);
19+
};
20+
};
21+
22+
app.view = baseView;
23+
})();

EXERCISE/Exercise 4/js/controller.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/***
2+
* Base application
3+
* - create singleton class named app
4+
* - create init method
5+
*/
6+
var app = app || {};
7+
8+
(function initApplication (global, doc) {
9+
10+
/**
11+
* Base app config
12+
*/
13+
var config = {
14+
el: '.wrapper'
15+
},
16+
17+
/***
18+
* Cached App elements
19+
*/
20+
elements = {},
21+
22+
instanceStorage = [];
23+
24+
/**
25+
* Initialize Application
26+
* @public
27+
*/
28+
app.init = function () {
29+
30+
cacheElements();
31+
setConfig();
32+
createLayout();
33+
};
34+
35+
/**
36+
* Get config
37+
* @public
38+
* returns {Object} config
39+
*/
40+
app.getConfig = function () {
41+
42+
return config;
43+
};
44+
45+
/**
46+
* Exit application
47+
* @public
48+
* returns void
49+
*/
50+
app.exit = function () {
51+
52+
for (var i = instanceStorage.length - 1; i >= 0; i--) {
53+
instanceStorage[i].destroy();
54+
};
55+
};
56+
57+
/**
58+
* Create application layout
59+
*/
60+
function createLayout () {
61+
62+
for (var template in app.templates) {
63+
instanceStorage.push(new app.view(app.templates[template]));
64+
}
65+
};
66+
67+
/**
68+
* Get user specific configuration
69+
*/
70+
function setConfig () {
71+
72+
config.appWidth = global.innerWidth;
73+
config.appHeight = global.innerHeight;
74+
}
75+
76+
/***
77+
* Cache app main elements
78+
*/
79+
function cacheElements () {
80+
81+
elements.el = doc.querySelector(config.el);
82+
}
83+
84+
})(window, document);

EXERCISE/Exercise 4/js/events.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
window.app = window.app || {};
2+
3+
/***
4+
* Subsciing and Publishing application events
5+
*/
6+
app.events = {
7+
8+
/***
9+
* Create event
10+
* @param {String} eventName
11+
* returns {Event Object}
12+
*/
13+
create: function(eventName){
14+
15+
var event = new CustomEvent(eventName);
16+
17+
return event;
18+
},
19+
20+
/***
21+
* Subscribes a listener to event and trigger a handler
22+
* @param {Dom Object} el
23+
* @param {String} eventName
24+
* @param {Function} handler
25+
* returns void
26+
*/
27+
subscribe: function (el,eventName,handler) {
28+
29+
el.addEventListener(eventName,handler,false);
30+
},
31+
32+
/***
33+
* Publishes an event to the listneres
34+
* @param {Dom Object} el
35+
* @param {String} eventName
36+
* returns void
37+
*/
38+
publish: function (el,eventName) {
39+
40+
var event = this.create(eventName);
41+
el.dispatchEvent(event);
42+
43+
},
44+
45+
/***
46+
* Removes a listener from element and detach handler function
47+
* @param {Dom Object} el
48+
* @param {String} eventName
49+
* @param {Function} handler
50+
* returns void
51+
*/
52+
unsubscribe: function (el,eventName,handler) {
53+
el.removeEventListener(eventName,handler,false);
54+
}
55+
};

EXERCISE/Exercise 4/js/templates.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/***
2+
* Stored templates as plain text
3+
*/
4+
var app = app || {};
5+
6+
(function () {
7+
8+
app.templates = {
9+
header: '<header id="header"></header>',
10+
footer: '<footer id="footer"></footer>',
11+
content: '<section id="content"></section>'
12+
};
13+
})();

0 commit comments

Comments
 (0)