-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
31 lines (27 loc) · 1.28 KB
/
example.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
<head>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="subscriber.js"></script>
</head>
<div id="example">
<button onclick="addExistingFoo()">Add existing subscriber: "foo"</button>
<button onclick="addNewFoo()">Add new subscriber: "foo"</button>
<input id="bar" type="number" value="0">
<input id="biz" type="number" value="1">
</div>
<script>
// Create reactive subscriber for an element (or elements) that will exist in the future.
const sub = new Subscriber($("#example input#foo"), (e) => parseInt($("div#example input#bar").val()) + parseInt($("div#example input#biz").val()), "foo");
sub.subscribe("div#example input#bar", "div#example input#biz");
function addExistingFoo() {
const el = $("<input></input>");
el.attr("id", "foo");
$("#example").append(el);
sub.resubscribe();
}
function addNewFoo() {
const newSub = new Subscriber("<input></input>", e => parseInt($("div#example input#bar").val()) - parseInt($("div#example input#biz").val()));
$("#example").append(newSub.element);
newSub.subscribe("div#example input#bar", "div#example input#biz");
}
</script>