| 1 | <html> |
| 2 | <head> |
| 3 | <title>OO/mixin</title> |
| 4 | <style type="text/css"> |
| 5 | @import "../../../dojo/resources/dojo.css"; |
| 6 | </style> |
| 7 | <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug:true"></script> |
| 8 | <script type="text/javascript"> |
| 9 | dojo.require("dojox.lang.oo.mixin"); |
| 10 | dojo.require("dojox.lang.oo.general"); |
| 11 | dojo.require("dojox.lang.oo.aop"); |
| 12 | |
| 13 | var oo = dojox.lang.oo, oog = oo.general, ooa = oo.aop; |
| 14 | |
| 15 | var test = function(){ |
| 16 | // test chaining of constructor() and destroy() |
| 17 | |
| 18 | var a = { |
| 19 | init: function(){ console.log("init #1"); }, |
| 20 | abc: function(){ console.log("abc #1: args = ", arguments); return 1; }, |
| 21 | destroy: function(){ console.log("destroy #1"); } |
| 22 | }, |
| 23 | b = { |
| 24 | init: function(){ console.log("init #2"); }, |
| 25 | abc: function(){ console.log("abc #2: args = ", arguments); return 2; }, |
| 26 | destroy: function(){ console.log("destroy #2"); } |
| 27 | }, |
| 28 | c = { |
| 29 | init: function(){ console.log("init #3"); }, |
| 30 | abc: function(){ console.log("abc #3: args = ", arguments); return 3; }, |
| 31 | destroy: function(){ console.log("destroy #3"); } |
| 32 | }; |
| 33 | |
| 34 | console.log("=== test default mixin: a"); |
| 35 | var t = oo.mixin({}, a); |
| 36 | t.init(); |
| 37 | t.abc(42); |
| 38 | t.destroy(); |
| 39 | |
| 40 | console.log("=== test default mixin: a + b"); |
| 41 | var t = oo.mixin({}, a, b); |
| 42 | t.init(); |
| 43 | t.abc(42); |
| 44 | t.destroy(); |
| 45 | |
| 46 | console.log("=== test default mixin: a + b + c"); |
| 47 | var t = oo.mixin({}, a, b, c); |
| 48 | t.init(); |
| 49 | t.abc(42); |
| 50 | t.destroy(); |
| 51 | |
| 52 | console.log("=== test default mixin: a + (b - 'abc')"); |
| 53 | var t = oo.mixin({}, a, oo.filter(b, {abc: ""})); |
| 54 | t.init(); |
| 55 | t.abc(42); |
| 56 | t.destroy(); |
| 57 | |
| 58 | console.log("=== test default mixin: a + (b - 'destroy' * 'abc'/'def')"); |
| 59 | var t = oo.mixin({}, a, oo.filter(b, {abc: "def", destroy: ""})); |
| 60 | t.init(); |
| 61 | t.abc(42); |
| 62 | console.log("the next line shoud print 'abc #2'"); |
| 63 | t.def(42); |
| 64 | t.destroy(); |
| 65 | |
| 66 | console.log("=== test default mixin: a + 'abc2'/after + 'abc3'/before"); |
| 67 | var t = oo.mixin({}, a, |
| 68 | oo.filter(oog.after(b), {init: "", destroy: ""}), |
| 69 | oo.filter(oog.before(c), {init: "", destroy: ""}) |
| 70 | ); |
| 71 | t.init(); |
| 72 | t.abc(42); |
| 73 | t.destroy(); |
| 74 | |
| 75 | console.log("=== test augment and override (negative case)"); |
| 76 | var t = oo.mixin({}, a, { |
| 77 | abc: oog.augment(function(){ console.log("augmented"); }), // can't override |
| 78 | def: oog.override(function(){ console.log("overidden"); }) // nothing to override |
| 79 | }); |
| 80 | t.init(); |
| 81 | t.abc(42); |
| 82 | console.log("should be 'undefined': ", t.def); |
| 83 | t.destroy(); |
| 84 | |
| 85 | console.log("=== test augment and override (positive case)"); |
| 86 | var t = oo.mixin({}, a, { |
| 87 | def: oog.augment(function(){ console.log("augmented"); }), // can't override |
| 88 | abc: oog.override(function(){ console.log("overidden"); }) // nothing to override |
| 89 | }); |
| 90 | t.init(); |
| 91 | t.abc(42); |
| 92 | t.def(42); |
| 93 | t.destroy(); |
| 94 | |
| 95 | console.log("=== test augment and general.after (no chaining for existing functions)"); |
| 96 | var t = oo.mixin({}, a, { |
| 97 | abc: oog.augment(oog.after( |
| 98 | function(){ console.log("overidden"); } |
| 99 | )) |
| 100 | }); |
| 101 | t.init(); |
| 102 | t.abc(42); |
| 103 | t.destroy(); |
| 104 | |
| 105 | console.log("=== test override and general.after (chaining only for existing functions)"); |
| 106 | var t = oo.mixin({}, a, { |
| 107 | abc: oog.override(oog.after( |
| 108 | function(){ console.log("overidden? not really"); } |
| 109 | )) |
| 110 | }); |
| 111 | t.init(); |
| 112 | t.abc(42); |
| 113 | t.destroy(); |
| 114 | |
| 115 | console.log("=== test decorated objects"); |
| 116 | var t = oo.mixin({}, a, oog.after({ |
| 117 | abc: function(){ console.log("added after abc"); }, |
| 118 | def: function(){ console.log("added after def"); } |
| 119 | })); |
| 120 | t.init(); |
| 121 | t.abc(42); |
| 122 | t.def(42); |
| 123 | t.destroy(); |
| 124 | |
| 125 | console.log("=== test aop-like decorators"); |
| 126 | var t = oo.mixin({}, a, |
| 127 | { |
| 128 | abc: ooa.around(function(f, args){ |
| 129 | console.log("aop.around: args = ", args); |
| 130 | var ret = f.apply(this, args); |
| 131 | console.log("aop.around: ret = ", ret); |
| 132 | return ret; |
| 133 | }) |
| 134 | }, |
| 135 | { |
| 136 | abc: ooa.before(function(){ |
| 137 | console.log("aop.before: args = ", arguments); |
| 138 | }) |
| 139 | }, |
| 140 | { |
| 141 | abc: ooa.afterReturning(function(ret){ |
| 142 | console.log("aop.afterReturning: ret = ", ret); |
| 143 | }) |
| 144 | }, |
| 145 | { |
| 146 | abc: ooa.after(function(){ |
| 147 | console.log("aop.after"); |
| 148 | }) |
| 149 | } |
| 150 | ); |
| 151 | t.init(); |
| 152 | t.abc(42); |
| 153 | t.destroy(); |
| 154 | }; |
| 155 | //dojo.addOnLoad(test); |
| 156 | </script> |
| 157 | </head> |
| 158 | <body> |
| 159 | <p>This test is meant to run with Firebug. Open the console to see the output.</p> |
| 160 | <p><button onclick="test()">Start</button></p> |
| 161 | </body> |
| 162 | </html> |