AdapterRegistry.js 3.7 KB

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