Guest User

Untitled

a guest
Jun 20th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. var dispatcher = {
  2.  
  3. dispatchees: [],
  4.  
  5. connect: function (pattern, action, options) {
  6. dispatcher.dispatchees.push({
  7. pattern: pattern,
  8. action: action,
  9. options: options || {}
  10. });
  11.  
  12. return dispatcher;
  13. },
  14.  
  15. match: function (pattern, data) {
  16. if (pattern instanceof RegExp) {
  17. return pattern.exec(data.pathname);
  18. }
  19.  
  20. return false;
  21. },
  22.  
  23. dispatch: function (info, options) {
  24. var data = (typeof info == 'object' && 'pathname' in info) ? info : { pathname: info.toString() };
  25.  
  26. var length = dispatcher.dispatchees.length;
  27. for (var i = 0; i < length; i++) {
  28. var dispatchee = dispatcher.dispatchees[i];
  29. var matches = dispatcher.match(dispatchee.pattern, data);
  30.  
  31. if ( matches ) {
  32. if ( dispatchee.options.onMatchedToPath &&
  33. typeof dispatchee.options.onMatchedToPath == 'function' ) {
  34. to_process = dispatchee.options.onMatchedToPath.apply(dispatchee, [data, options]);
  35. if (!to_process) continue;
  36. }
  37.  
  38. if (dispatchee.action && typeof dispatchee.action == 'function') {
  39. var to_chain = dispatchee.action.apply(dispatchee, [data, matches, options || {}, i]);
  40. if (!to_chain) return {
  41. dispatchee: dispatchee,
  42. matches: matches,
  43. data: data
  44. };
  45.  
  46. continue;
  47. }
  48.  
  49. return true;
  50. }
  51. }
  52.  
  53. return false;
  54. }
  55. };
Add Comment
Please, Sign In to add comment