1 if(!dojo._hasResource["dojo.AdapterRegistry"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.AdapterRegistry"] = true;
3 dojo.provide("dojo.AdapterRegistry");
5 dojo.AdapterRegistry = function(/*Boolean?*/ returnWrappers){
7 // A registry to make contextual calling/searching easier.
9 // Objects of this class keep list of arrays in the form [name, check,
10 // wrap, directReturn] that are used to determine what the contextual
11 // result of a set of checked arguments is. All check/wrap functions
12 // in this registry should be of the same arity.
14 // | // create a new registry
15 // | var reg = new dojo.AdapterRegistry();
16 // | reg.register("handleString",
19 // | // do something with the string here
22 // | reg.register("handleArr",
25 // | // do something with the array here
29 // | // now we can pass reg.match() *either* an array or a string and
30 // | // the value we pass will get handled by the right function
31 // | reg.match("someValue"); // will call the first function
32 // | reg.match(["someValue"]); // will call the second
35 this.returnWrappers = returnWrappers || false; // Boolean
38 dojo.extend(dojo.AdapterRegistry, {
39 register: function(/*String*/ name, /*Function*/ check, /*Function*/ wrap, /*Boolean?*/ directReturn, /*Boolean?*/ override){
41 // register a check function to determine if the wrap function or
42 // object gets selected
44 // a way to identify this matcher.
46 // a function that arguments are passed to from the adapter's
47 // match() function. The check function should return true if the
48 // given arguments are appropriate for the wrap function.
50 // If directReturn is true, the value passed in for wrap will be
51 // returned instead of being called. Alternately, the
52 // AdapterRegistry can be set globally to "return not call" using
53 // the returnWrappers property. Either way, this behavior allows
54 // the registry to act as a "search" function instead of a
55 // function interception library.
57 // If override is given and true, the check function will be given
58 // highest priority. Otherwise, it will be the lowest priority
60 this.pairs[((override) ? "unshift" : "push")]([name, check, wrap, directReturn]);
63 match: function(/* ... */){
65 // Find an adapter for the given arguments. If no suitable adapter
66 // is found, throws an exception. match() accepts any number of
67 // arguments, all of which are passed to all matching functions
68 // from the registered pairs.
69 for(var i = 0; i < this.pairs.length; i++){
70 var pair = this.pairs[i];
71 if(pair[1].apply(this, arguments)){
72 if((pair[3])||(this.returnWrappers)){
75 return pair[2].apply(this, arguments);
79 throw new Error("No match found");
82 unregister: function(name){
83 // summary: Remove a named adapter from the registry
85 // FIXME: this is kind of a dumb way to handle this. On a large
86 // registry this will be slow-ish and we can use the name as a lookup
87 // should we choose to trade memory for speed.
88 for(var i = 0; i < this.pairs.length; i++){
89 var pair = this.pairs[i];
91 this.pairs.splice(i, 1);