Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

yql.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('yql', function(Y) {
  9. /**
  10. * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
  11. * @module yql
  12. */
  13. /**
  14. * Utility Class used under the hood my the YQL class
  15. * @class YQLRequest
  16. * @constructor
  17. * @param {String} sql The SQL statement to execute
  18. * @param {Function/Object} callback The callback to execute after the query (Falls through to JSONP).
  19. * @param {Object} params An object literal of extra parameters to pass along (optional).
  20. * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url)
  21. */
  22. var YQLRequest = function (sql, callback, params, opts) {
  23. if (!params) {
  24. params = {};
  25. }
  26. params.q = sql;
  27. //Allow format override.. JSON-P-X
  28. if (!params.format) {
  29. params.format = Y.YQLRequest.FORMAT;
  30. }
  31. if (!params.env) {
  32. params.env = Y.YQLRequest.ENV;
  33. }
  34. this._params = params;
  35. this._opts = opts;
  36. this._callback = callback;
  37. };
  38. YQLRequest.prototype = {
  39. /**
  40. * @private
  41. * @property _jsonp
  42. * @description Reference to the JSONP instance used to make the queries
  43. */
  44. _jsonp: null,
  45. /**
  46. * @private
  47. * @property _opts
  48. * @description Holder for the opts argument
  49. */
  50. _opts: null,
  51. /**
  52. * @private
  53. * @property _callback
  54. * @description Holder for the callback argument
  55. */
  56. _callback: null,
  57. /**
  58. * @private
  59. * @property _params
  60. * @description Holder for the params argument
  61. */
  62. _params: null,
  63. /**
  64. * @method send
  65. * @description The method that executes the YQL Request.
  66. * @chainable
  67. * @returns {YQLRequest}
  68. */
  69. send: function() {
  70. var qs = [], url = ((this._opts && this._opts.proto) ? this._opts.proto : Y.YQLRequest.PROTO);
  71. Y.each(this._params, function(v, k) {
  72. qs.push(k + '=' + encodeURIComponent(v));
  73. });
  74. qs = qs.join('&');
  75. url += ((this._opts && this._opts.base) ? this._opts.base : Y.YQLRequest.BASE_URL) + qs;
  76. var o = (!Y.Lang.isFunction(this._callback)) ? this._callback : { on: { success: this._callback } };
  77. if (o.allowCache !== false) {
  78. o.allowCache = true;
  79. }
  80. if (!this._jsonp) {
  81. this._jsonp = Y.jsonp(url, o);
  82. } else {
  83. this._jsonp.url = url;
  84. if (o.on && o.on.success) {
  85. this._jsonp._config.on.success = o.on.success;
  86. }
  87. this._jsonp.send();
  88. }
  89. return this;
  90. }
  91. };
  92. /**
  93. * @static
  94. * @property FORMAT
  95. * @description Default format to use: json
  96. */
  97. YQLRequest.FORMAT = 'json';
  98. /**
  99. * @static
  100. * @property PROTO
  101. * @description Default protocol to use: http
  102. */
  103. YQLRequest.PROTO = 'http';
  104. /**
  105. * @static
  106. * @property BASE_URL
  107. * @description The base URL to query: query.yahooapis.com/v1/public/yql?
  108. */
  109. YQLRequest.BASE_URL = ':/'+'/query.yahooapis.com/v1/public/yql?';
  110. /**
  111. * @static
  112. * @property ENV
  113. * @description The environment file to load: http://datatables.org/alltables.env
  114. */
  115. YQLRequest.ENV = 'http:/'+'/datatables.org/alltables.env';
  116. Y.YQLRequest = YQLRequest;
  117. /**
  118. * This class adds a sugar class to allow access to YQL (http://developer.yahoo.com/yql/).
  119. * @class YQL
  120. * @constructor
  121. * @param {String} sql The SQL statement to execute
  122. * @param {Function} callback The callback to execute after the query (optional).
  123. * @param {Object} params An object literal of extra parameters to pass along (optional).
  124. * @param {Object} opts An object literal of configuration options (optional): proto (http|https), base (url)
  125. */
  126. Y.YQL = function(sql, callback, params, opts) {
  127. return new Y.YQLRequest(sql, callback, params, opts).send();
  128. };
  129. }, '3.4.0' ,{requires:['jsonp', 'jsonp-url']});