1 | dojo.provide("dojox.lang.oo.general"); |
---|
2 | |
---|
3 | dojo.require("dojox.lang.oo.Decorator"); |
---|
4 | |
---|
5 | (function(){ |
---|
6 | var oo = dojox.lang.oo, md = oo.makeDecorator, oog = oo.general, |
---|
7 | isF = dojo.isFunction; |
---|
8 | |
---|
9 | // generally useful decorators |
---|
10 | |
---|
11 | oog.augment = md(function(name, newValue, oldValue){ |
---|
12 | // summary: |
---|
13 | // add property, if it was not defined before |
---|
14 | return typeof oldValue == "undefined" ? newValue : oldValue; |
---|
15 | }); |
---|
16 | |
---|
17 | oog.override = md(function(name, newValue, oldValue){ |
---|
18 | // summary: |
---|
19 | // override property only if it was already present |
---|
20 | return typeof oldValue != "undefined" ? newValue : oldValue; |
---|
21 | }); |
---|
22 | |
---|
23 | oog.shuffle = md(function(name, newValue, oldValue){ |
---|
24 | // summary: |
---|
25 | // replaces arguments for an old method |
---|
26 | return isF(oldValue) ? |
---|
27 | function(){ |
---|
28 | return oldValue.apply(this, newValue.apply(this, arguments)); |
---|
29 | } : oldValue; |
---|
30 | }); |
---|
31 | |
---|
32 | oog.wrap = md(function(name, newValue, oldValue){ |
---|
33 | // summary: |
---|
34 | // wraps the old values with a supplied function |
---|
35 | return function(){ return newValue.call(this, oldValue, arguments); }; |
---|
36 | }); |
---|
37 | |
---|
38 | oog.tap = md(function(name, newValue, oldValue){ |
---|
39 | // summary: |
---|
40 | // always returns "this" ignoring the actual return |
---|
41 | return function(){ newValue.apply(this, arguments); return this; }; |
---|
42 | }); |
---|
43 | |
---|
44 | oog.before = md(function(name, newValue, oldValue){ |
---|
45 | // summary: |
---|
46 | // creates a chain of calls where the new method is called |
---|
47 | // before the old method |
---|
48 | return isF(oldValue) ? |
---|
49 | function(){ |
---|
50 | newValue.apply(this, arguments); |
---|
51 | return oldValue.apply(this, arguments); |
---|
52 | } : newValue; |
---|
53 | }); |
---|
54 | |
---|
55 | oog.after = md(function(name, newValue, oldValue){ |
---|
56 | // summary: |
---|
57 | // creates a chain of calls where the new method is called |
---|
58 | // after the old method |
---|
59 | return isF(oldValue) ? |
---|
60 | function(){ |
---|
61 | oldValue.apply(this, arguments); |
---|
62 | return newValue.apply(this, arguments); |
---|
63 | } : newValue; |
---|
64 | }); |
---|
65 | })(); |
---|