API Docs for: 3.10.3
Show:

File: jsonp/js/jsonp-url.js

  1. var JSONPRequest = Y.JSONPRequest,
  2. getByPath = Y.Object.getValue,
  3. noop = function () {};
  4.  
  5. /**
  6. * Adds support for parsing complex callback identifiers from the jsonp url.
  7. * This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
  8. * in the YUI instance.
  9. *
  10. * @module jsonp
  11. * @submodule jsonp-url
  12. * @for JSONPRequest
  13. */
  14.  
  15. Y.mix(JSONPRequest.prototype, {
  16. /**
  17. * RegExp used by the default URL formatter to insert the generated callback
  18. * name into the JSONP url. Looks for a query param callback=. If a value
  19. * is assigned, it will be clobbered.
  20. *
  21. * @property _pattern
  22. * @type RegExp
  23. * @default /\bcallback=.*?(?=&|$)/i
  24. * @protected
  25. */
  26. _pattern: /\bcallback=(.*?)(?=&|$)/i,
  27.  
  28. /**
  29. * Template used by the default URL formatter to add the callback function
  30. * name to the url.
  31. *
  32. * @property _template
  33. * @type String
  34. * @default "callback={callback}"
  35. * @protected
  36. */
  37. _template: "callback={callback}",
  38.  
  39. /**
  40. * <p>Parses the url for a callback named explicitly in the string.
  41. * Override this if the target JSONP service uses a different query
  42. * parameter or url format.</p>
  43. *
  44. * <p>If the callback is declared inline, the corresponding function will
  45. * be returned. Otherwise null.</p>
  46. *
  47. * @method _defaultCallback
  48. * @param url {String} the url to search in
  49. * @return {Function} the callback function if found, or null
  50. * @protected
  51. */
  52. _defaultCallback: function (url) {
  53. var match = url.match(this._pattern),
  54. keys = [],
  55. i = 0,
  56. locator, path, callback;
  57.  
  58. if (match) {
  59. // Strip the ["string keys"] and [1] array indexes
  60. locator = match[1]
  61. .replace(/\[(['"])(.*?)\1\]/g,
  62. function (x, $1, $2) {
  63. keys[i] = $2;
  64. return '.@' + (i++);
  65. })
  66. .replace(/\[(\d+)\]/g,
  67. function (x, $1) {
  68. keys[i] = parseInt($1, 10) | 0;
  69. return '.@' + (i++);
  70. })
  71. .replace(/^\./, ''); // remove leading dot
  72.  
  73. // Validate against problematic characters.
  74. if (!/[^\w\.\$@]/.test(locator)) {
  75. path = locator.split('.');
  76. for (i = path.length - 1; i >= 0; --i) {
  77. if (path[i].charAt(0) === '@') {
  78. path[i] = keys[parseInt(path[i].substr(1), 10)];
  79. }
  80. }
  81.  
  82. // First look for a global function, then the Y, then try the Y
  83. // again from the second token (to support "callback=Y.handler")
  84. callback = getByPath(Y.config.win, path) ||
  85. getByPath(Y, path) ||
  86. getByPath(Y, path.slice(1));
  87. }
  88. }
  89.  
  90. return callback || noop;
  91. },
  92.  
  93. /**
  94. * URL formatter that looks for callback= in the url and appends it
  95. * if not present. The supplied proxy name will be assigned to the query
  96. * param. Override this method by passing a function as the
  97. * &quot;format&quot; property in the config object to the constructor.
  98. *
  99. * @method _format
  100. * @param url { String } the original url
  101. * @param proxy {String} the function name that will be used as a proxy to
  102. * the configured callback methods.
  103. * @return {String} fully qualified JSONP url
  104. * @protected
  105. */
  106. _format: function (url, proxy) {
  107. var callback = this._template.replace(/\{callback\}/, proxy),
  108. lastChar;
  109.  
  110. if (this._pattern.test(url)) {
  111. return url.replace(this._pattern, callback);
  112. } else {
  113. lastChar = url.slice(-1);
  114. if (lastChar !== '&' && lastChar !== '?') {
  115. url += (url.indexOf('?') > -1) ? '&' : '?';
  116. }
  117. return url + callback;
  118. }
  119. }
  120.  
  121. }, true);
  122.