1 | <!DOCTYPE html>
|
2 | <html>
|
3 | <head>
|
4 | <script type="text/javascript" src="../lib/advisable.js"></script>
|
5 | </head>
|
6 | <body>
|
7 | <h1>simple browser example</h1>
|
8 | <p id="advisable-sync-target"></p>
|
9 | <p id="advisable-async-target"></p>
|
10 | <script>
|
11 | var syncTarget = document.getElementById('advisable-sync-target')
|
12 | , asyncTarget = document.getElementById('advisable-async-target')
|
13 | , target
|
14 | , equation = '((10 * 3) + (100*3) + 1) + 123 = ';
|
15 |
|
16 | function Target(val) {
|
17 | this.val = val;
|
18 | }
|
19 |
|
20 | Target.prototype.syncFunc = function (a, b) {
|
21 | return a + b + this.val;
|
22 | };
|
23 |
|
24 | Target.prototype.asyncFunc = function (a, b, callback) {
|
25 | setTimeout(function () {
|
26 | callback(null, a + b + this.val);
|
27 | }.bind(this), 1500);
|
28 | };
|
29 |
|
30 | advisable.sync.call(Target.prototype);
|
31 | advisable.async.call(Target.prototype);
|
32 |
|
33 | Target.prototype.aroundSync(
|
34 | 'syncFunc'
|
35 | , function (a, b) {
|
36 | return [ a * 3, b * 3 ];
|
37 | }
|
38 | , function (v) {
|
39 | return v + 123;
|
40 | }
|
41 | , { mutate: true }
|
42 | );
|
43 |
|
44 | Target.prototype.around(
|
45 | 'asyncFunc'
|
46 | , function (a, b, callback) {
|
47 | callback(null, a * 3, b * 3);
|
48 | }
|
49 | , function (v, callback) {
|
50 | callback(null, v + 123);
|
51 | }
|
52 | , { mutate: true }
|
53 | );
|
54 |
|
55 | target = new Target(1);
|
56 | syncTarget.textContent = 'sync: ' + equation + target.syncFunc(10, 100);
|
57 |
|
58 | asyncTarget.textContent = 'waiting for async...';
|
59 |
|
60 | target = new Target(1);
|
61 | target.asyncFunc(10, 100, function (err, result) {
|
62 | asyncTarget.textContent = 'async: ' + equation + result;
|
63 | });
|
64 | </script>
|
65 | <body>
|
66 | </html>
|