1 | define(["./_base/kernel", "./_base/lang"], function(dojo, lang) { |
---|
2 | // module: |
---|
3 | // dojo/AdapterRegistry |
---|
4 | // summary: |
---|
5 | // TODOC |
---|
6 | |
---|
7 | var AdapterRegistry = dojo.AdapterRegistry = function(/*Boolean?*/ returnWrappers){ |
---|
8 | // summary: |
---|
9 | // A registry to make contextual calling/searching easier. |
---|
10 | // description: |
---|
11 | // Objects of this class keep list of arrays in the form [name, check, |
---|
12 | // wrap, directReturn] that are used to determine what the contextual |
---|
13 | // result of a set of checked arguments is. All check/wrap functions |
---|
14 | // in this registry should be of the same arity. |
---|
15 | // example: |
---|
16 | // | // create a new registry |
---|
17 | // | var reg = new dojo.AdapterRegistry(); |
---|
18 | // | reg.register("handleString", |
---|
19 | // | dojo.isString, |
---|
20 | // | function(str){ |
---|
21 | // | // do something with the string here |
---|
22 | // | } |
---|
23 | // | ); |
---|
24 | // | reg.register("handleArr", |
---|
25 | // | dojo.isArray, |
---|
26 | // | function(arr){ |
---|
27 | // | // do something with the array here |
---|
28 | // | } |
---|
29 | // | ); |
---|
30 | // | |
---|
31 | // | // now we can pass reg.match() *either* an array or a string and |
---|
32 | // | // the value we pass will get handled by the right function |
---|
33 | // | reg.match("someValue"); // will call the first function |
---|
34 | // | reg.match(["someValue"]); // will call the second |
---|
35 | |
---|
36 | this.pairs = []; |
---|
37 | this.returnWrappers = returnWrappers || false; // Boolean |
---|
38 | }; |
---|
39 | |
---|
40 | /*===== |
---|
41 | // doc alias helpers: |
---|
42 | AdapterRegistry = dojo.AdapterRegistry; |
---|
43 | =====*/ |
---|
44 | |
---|
45 | lang.extend(AdapterRegistry, { |
---|
46 | register: function(/*String*/ name, /*Function*/ check, /*Function*/ wrap, /*Boolean?*/ directReturn, /*Boolean?*/ override){ |
---|
47 | // summary: |
---|
48 | // register a check function to determine if the wrap function or |
---|
49 | // object gets selected |
---|
50 | // name: |
---|
51 | // a way to identify this matcher. |
---|
52 | // check: |
---|
53 | // a function that arguments are passed to from the adapter's |
---|
54 | // match() function. The check function should return true if the |
---|
55 | // given arguments are appropriate for the wrap function. |
---|
56 | // directReturn: |
---|
57 | // If directReturn is true, the value passed in for wrap will be |
---|
58 | // returned instead of being called. Alternately, the |
---|
59 | // AdapterRegistry can be set globally to "return not call" using |
---|
60 | // the returnWrappers property. Either way, this behavior allows |
---|
61 | // the registry to act as a "search" function instead of a |
---|
62 | // function interception library. |
---|
63 | // override: |
---|
64 | // If override is given and true, the check function will be given |
---|
65 | // highest priority. Otherwise, it will be the lowest priority |
---|
66 | // adapter. |
---|
67 | this.pairs[((override) ? "unshift" : "push")]([name, check, wrap, directReturn]); |
---|
68 | }, |
---|
69 | |
---|
70 | match: function(/* ... */){ |
---|
71 | // summary: |
---|
72 | // Find an adapter for the given arguments. If no suitable adapter |
---|
73 | // is found, throws an exception. match() accepts any number of |
---|
74 | // arguments, all of which are passed to all matching functions |
---|
75 | // from the registered pairs. |
---|
76 | for(var i = 0; i < this.pairs.length; i++){ |
---|
77 | var pair = this.pairs[i]; |
---|
78 | if(pair[1].apply(this, arguments)){ |
---|
79 | if((pair[3])||(this.returnWrappers)){ |
---|
80 | return pair[2]; |
---|
81 | }else{ |
---|
82 | return pair[2].apply(this, arguments); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | throw new Error("No match found"); |
---|
87 | }, |
---|
88 | |
---|
89 | unregister: function(name){ |
---|
90 | // summary: |
---|
91 | // Remove a named adapter from the registry |
---|
92 | // name: String |
---|
93 | // The name of the adapter. |
---|
94 | // returns: Boolean |
---|
95 | // Returns true if operation is successful. |
---|
96 | // Returns false if operation fails. |
---|
97 | |
---|
98 | // FIXME: this is kind of a dumb way to handle this. On a large |
---|
99 | // registry this will be slow-ish and we can use the name as a lookup |
---|
100 | // should we choose to trade memory for speed. |
---|
101 | for(var i = 0; i < this.pairs.length; i++){ |
---|
102 | var pair = this.pairs[i]; |
---|
103 | if(pair[0] == name){ |
---|
104 | this.pairs.splice(i, 1); |
---|
105 | return true; |
---|
106 | } |
---|
107 | } |
---|
108 | return false; |
---|
109 | } |
---|
110 | }); |
---|
111 | |
---|
112 | return AdapterRegistry; |
---|
113 | }); |
---|