AdapterRegistry.js 3.6 KB

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