-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.html
74 lines (70 loc) · 2.17 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jBlocks demo</title>
</head>
<body>
<div class="js-counter" data-component="counter" data-props='{ "initialValue": 2 }'>
<button class="js-inc">+</button>
<button class="js-dec">-</button>
</div>
<button class="js-button-inc">Increase the counter</button>
<div class="js-console"></div>
<script src="./dist/main.js"></script>
<script>
// declare a new component
jBlocks.define('counter', {
events: {
'click .js-inc': 'inc',
'click .js-dec': 'dec'
},
methods: {
oninit: function() {
this._currentValue = Number(this.props.initialValue);
},
ondestroy: function() {
this._currentValue = null;
},
/**
* Increases the counter, emits changed event
*/
inc: function() {
this._currentValue++;
this.emit('changed', {
value: this._currentValue
});
},
/**
* Decreases the counter, emits changed event
*/
dec: function() {
this._currentValue--;
this.emit('changed', {
value: this._currentValue
});
},
/**
* Returns the current value
* @return {Number}
*/
getCurrentValue: function() {
return this._currentValue;
}
}
});
// create an instance of the counter
var counter = jBlocks.get(document.querySelector('.js-counter'));
// use event to react on what happens during lifecycle
counter.on('changed', function(data) {
document.querySelector('.js-console').innerText = data.value;
});
// use component api to interact with somewhere in the app
document.querySelector('.js-button-inc').addEventListener('click', function() {
counter.inc();
});
</script>
</body>
</html>