-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
83 lines (67 loc) · 1.38 KB
/
example.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
var view = new View();
/*
The Todo component
*/
function Todo (parent)
{
var parent = parent;
var widget = new Widget(this);
var elements = ['Fish', 'Mangoes', 'Nougat'];
function list () {
var items = [];
elements.forEach( function (element) {
items.push(view.h('p', element));
});
return view.h('div', items);
}
this.draw = function () {
var vdom= view.h('div', [
view.h('p#plugin'),
widget.draw('Example app'), //external component
list(), //internal component
view.h('input#value', { type: 'text' } ),
view.h('button#add', { onclick: clickHandler }, 'Add')
]);
return vdom;
}
function clickHandler () {
elements.push($('#value').val());
parent.draw();
}
}
/*
A reusable Widget component
*/
function Widget (parent)
{
var parent = parent;
this.draw = function (message) {
return view.h('p.w-message.w-information', message);
}
}
/*
A jQuery plugin (e.g. to show integration with lagacy code)
*/
(function($) {
$.fn.foo = function() {
this.each( function() {
$(this).text('Example integrated jQuery plugin');
});
}
}(jQuery));
/*
The page controller
*/
function Page ()
{
var todo = new Todo(this);
this.draw = function () {
var vdom = todo.draw();
view.render(vdom);
$('#plugin').foo(); //call jQuery plugin
}
}
$(document).ready( function() {
var page = new Page();
page.draw();
});