1 | dojo.provide("dojox.lang.oo.aop"); |
---|
2 | |
---|
3 | dojo.require("dojox.lang.oo.Decorator"); |
---|
4 | dojo.require("dojox.lang.oo.general"); |
---|
5 | |
---|
6 | (function(){ |
---|
7 | var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general, ooa = oo.aop, |
---|
8 | isF = dojo.isFunction; |
---|
9 | |
---|
10 | // five decorators implementing light-weight AOP weaving |
---|
11 | |
---|
12 | /*===== |
---|
13 | ooa.before = md(function(name, newValue, oldValue){ |
---|
14 | // summary: creates a "before" advise, by calling new function |
---|
15 | // before the old one |
---|
16 | |
---|
17 | // dummy body |
---|
18 | }); |
---|
19 | |
---|
20 | ooa.around = md(function(name, newValue, oldValue){ |
---|
21 | // summary: creates an "around" advise, |
---|
22 | // the previous value is passed as a first argument and can be null, |
---|
23 | // arguments are passed as a second argument |
---|
24 | |
---|
25 | // dummy body |
---|
26 | }); |
---|
27 | =====*/ |
---|
28 | |
---|
29 | // reuse existing decorators |
---|
30 | ooa.before = oog.before; |
---|
31 | ooa.around = oog.wrap; |
---|
32 | |
---|
33 | ooa.afterReturning = md(function(name, newValue, oldValue){ |
---|
34 | // summary: creates an "afterReturning" advise, |
---|
35 | // the returned value is passed as the only argument |
---|
36 | return isF(oldValue) ? |
---|
37 | function(){ |
---|
38 | var ret = oldValue.apply(this, arguments); |
---|
39 | newValue.call(this, ret); |
---|
40 | return ret; |
---|
41 | } : function(){ newValue.call(this); }; |
---|
42 | }); |
---|
43 | |
---|
44 | ooa.afterThrowing = md(function(name, newValue, oldValue){ |
---|
45 | // summary: creates an "afterThrowing" advise, |
---|
46 | // the exception is passed as the only argument |
---|
47 | return isF(oldValue) ? |
---|
48 | function(){ |
---|
49 | var ret; |
---|
50 | try{ |
---|
51 | ret = oldValue.apply(this, arguments); |
---|
52 | }catch(e){ |
---|
53 | newValue.call(this, e); |
---|
54 | throw e; |
---|
55 | } |
---|
56 | return ret; |
---|
57 | } : oldValue; |
---|
58 | }); |
---|
59 | |
---|
60 | ooa.after = md(function(name, newValue, oldValue){ |
---|
61 | // summary: creates an "after" advise, |
---|
62 | // it takes no arguments |
---|
63 | return isF(oldValue) ? |
---|
64 | function(){ |
---|
65 | var ret; |
---|
66 | try{ |
---|
67 | ret = oldValue.apply(this, arguments); |
---|
68 | }finally{ |
---|
69 | newValue.call(this); |
---|
70 | } |
---|
71 | return ret; |
---|
72 | } : function(){ newValue.call(this); } |
---|
73 | }); |
---|
74 | })(); |
---|