]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/AdapterRegistry.js
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo.AdapterRegistry"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo.AdapterRegistry"] = true;
10 dojo
.provide("dojo.AdapterRegistry");
13 dojo
.AdapterRegistry = function(/*Boolean?*/ returnWrappers
){
15 // A registry to make contextual calling/searching easier.
17 // Objects of this class keep list of arrays in the form [name, check,
18 // wrap, directReturn] that are used to determine what the contextual
19 // result of a set of checked arguments is. All check/wrap functions
20 // in this registry should be of the same arity.
22 // | // create a new registry
23 // | var reg = new dojo.AdapterRegistry();
24 // | reg.register("handleString",
27 // | // do something with the string here
30 // | reg.register("handleArr",
33 // | // do something with the array here
37 // | // now we can pass reg.match() *either* an array or a string and
38 // | // the value we pass will get handled by the right function
39 // | reg.match("someValue"); // will call the first function
40 // | reg.match(["someValue"]); // will call the second
43 this.returnWrappers
= returnWrappers
|| false; // Boolean
46 dojo
.extend(dojo
.AdapterRegistry
, {
47 register: function(/*String*/ name
, /*Function*/ check
, /*Function*/ wrap
, /*Boolean?*/ directReturn
, /*Boolean?*/ override
){
49 // register a check function to determine if the wrap function or
50 // object gets selected
52 // a way to identify this matcher.
54 // a function that arguments are passed to from the adapter's
55 // match() function. The check function should return true if the
56 // given arguments are appropriate for the wrap function.
58 // If directReturn is true, the value passed in for wrap will be
59 // returned instead of being called. Alternately, the
60 // AdapterRegistry can be set globally to "return not call" using
61 // the returnWrappers property. Either way, this behavior allows
62 // the registry to act as a "search" function instead of a
63 // function interception library.
65 // If override is given and true, the check function will be given
66 // highest priority. Otherwise, it will be the lowest priority
68 this.pairs
[((override
) ? "unshift" : "push")]([name
, check
, wrap
, directReturn
]);
71 match: function(/* ... */){
73 // Find an adapter for the given arguments. If no suitable adapter
74 // is found, throws an exception. match() accepts any number of
75 // arguments, all of which are passed to all matching functions
76 // from the registered pairs.
77 for(var i
= 0; i
< this.pairs
.length
; i
++){
78 var pair
= this.pairs
[i
];
79 if(pair
[1].apply(this, arguments
)){
80 if((pair
[3])||(this.returnWrappers
)){
83 return pair
[2].apply(this, arguments
);
87 throw new Error("No match found");
90 unregister: function(name
){
91 // summary: Remove a named adapter from the registry
93 // FIXME: this is kind of a dumb way to handle this. On a large
94 // registry this will be slow-ish and we can use the name as a lookup
95 // should we choose to trade memory for speed.
96 for(var i
= 0; i
< this.pairs
.length
; i
++){
97 var pair
= this.pairs
[i
];
99 this.pairs
.splice(i
, 1);