Skip to content
Browse files

Refactored and swapped opts and params, defaulting to params first

  • Loading branch information...
1 parent 3add0d5 commit 9577897c42cbcf5a8fd14893ebff38c6e79ba4fa @davglass committed Oct 1, 2009
Showing with 278 additions and 13 deletions.
  1. +263 −0 tests/tests.html
  2. +3 −3 timeout_test.html → tests/timeout.html
  3. +1 −1 yql-min.js
  4. +11 −9 yql.js
View
263 tests/tests.html
@@ -0,0 +1,263 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <title>YUI: YUI3 YQL Module</title>
+ <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/reset-fonts-grids/reset-fonts-grids.css">
+ <link rel="stylesheet" href="http://blog.davglass.com/files/yui/css/davglass.css" type="text/css">
+ <link rel="stylesheet" type="text/css" href="http://developer.yahoo.com/yui/assets/dpSyntaxHighlighter.css">
+ <style type="text/css" media="screen">
+ p, h2 {
+ margin: 1em;
+ }
+
+ .mod {
+ border: 1px solid black;
+ padding: 10px;
+ width: 240px;
+ margin: .5em;
+ }
+ .mod .twitter li {
+ border: 1px solid #ccc;
+ margin-bottom: 3px;
+ padding: 5px;
+ }
+ .mod .twitter li.odd {
+ background-color: #ccc;
+ border-color: #ddd;
+ }
+ ul.listing li {
+ list-style-type: square;
+ margin-left: 4em;
+ }
+ </style>
+</head>
+<body class="yui-skin-sam">
+<div id="davdoc" class="yui-t7">
+ <div id="hd"><h1 id="header"><a href="http://blog.davglass.com/">YUI: YUI3 YQL Module</a></h1></div>
+ <div id="bd">
+ <div class="yui-gd">
+ <div class="yui-u first">
+ <h2>Examples</h2>
+ <div id="res"></div>
+ </div>
+ <div class="yui-u">
+ <p><strong>The example may load a little slow (I'm pulling the YUI files from GitHub), so give it a sec.</strong></p>
+ <p>This module adds a <code>YQL</code> class to allow you to use <a href="http://developer.yahoo.com/yql/">YQL</a> from YUI3.</p>
+ <p>It already loads the <code>alltables.env</code> file from <a href="http://datatables.org">datatables.org</a>, so all the external user created Open Tables are available to query against.</p>
+ <p><em>Note: this is using the latest YUI 3 sources from GitHub (Not PR2).</em></p>
+ <ul class="listing">
+ <li><a href="./docs/">API Docs are available here.</a></li>
+ <li><a href="http://github.com/davglass/yui-yql/">Full Source</a></li>
+ <li><a href="http://github.com/davglass/yui-yql/raw/master/yql.js">Full Source File</a></li>
+ <li><a href="http://github.com/davglass/yui-yql/raw/master/yql-min.js">Minned Source File</a></li>
+ </ul>
+ <textarea name="code" class="HTML">
+<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.0.0/build/yui/yui-min.js"></script>
+<script type="text/javascript" src="yql-min.js"></script>
+</textarea>
+ <textarea name="code" class="JScript">
+ YUI().use('yql', function(Y) {
+ //Using events
+ var q1 = new Y.yql('select * from github.user.info where (id = "davglass")');
+ q1.on('query', function(r) {
+ //Do something here.
+ });
+ q1.on('error', function(r) {
+ //Do something here.
+ });
+ //Or the callback approach
+ new Y.yql('select * from github.user.info where (id = "davglass")', function(r) {
+ //Do something here.
+ r.query; //The result
+ r.error; //The error message
+ });
+ });
+</textarea>
+ <h2>GitHub Query</h2>
+ <textarea name="code" class="JScript">
+ var res = Y.get('#res');
+
+ var q1 = new Y.yql('select * from github.user.info where (id = "davglass")');
+ q1.on('query', function(r) {
+ var el = Y.Node.create('<div class="mod"></div>'),
+ user = r.results.user;
+ el.set('innerHTML', '<h2>Github (' + user.name + ')</h2>' +
+ 'URL: <a href="http://github.com/' + user.login + '">http://github.com/' + user.login +
+ '</a><br>Public Repos: (' + user['public-repo-count'].content + ')<br>Followers: ('
+ + user['followers-count'].content + ')<br>Following: (' + user['following-count'].content + ')');
+ res.appendChild(el);
+ });
+</textarea>
+ <h2>Weather Query</h2>
+ <textarea name="code" class="JScript">
+ var q2 = new Y.yql('select * from weather.forecast where location=90210');
+ q2.on('query', function(r) {
+ var el = Y.Node.create('<div class="mod"></div>');
+ el.set('innerHTML', '<h2>Weather for 90210</h2>' +
+ r.results.channel.item.description);
+ res.appendChild(el);
+
+ });</textarea>
+ <h2>Twitter Query</h2>
+ <textarea name="code" class="JScript">
+ var q3 = new Y.yql('select * from twitter.user.timeline where (id = "davglass")', function(r) {
+ var data = r.query.results.entry;
+ var h2 = Y.Node.create('<h2><img src="' + data[0].link[1].href + '"> Twitter: ' + data[0].author.name + '</h2>');
+ var ul = Y.Node.create('<ul class="twitter"></ul>');
+ Y.each(data, function(v) {
+ var title = v.title;
+ var len = title.indexOf(':');
+ title = title.substring(len + 2);
+ title = title.replace(/[@]+[A-Za-z0-9-_]+/g, function(f, n, s) {
+ return '<a href="http://twitter.com/' + f.replace('@', '') + '">' + f + '</a>';
+ });
+ var li = Y.Node.create('<li>' + title + '<span></span></li>');
+ ul.appendChild(li);
+ });
+ var wrap = Y.Node.create('<div id="twitter" class="mod"></div>');
+ wrap.appendChild(h2);
+ wrap.appendChild(ul);
+ res.appendChild(wrap);
+ ul.queryAll('li').each(function(v, k) {
+ if (!(k % 2)) {
+ v.addClass('odd');
+ }
+ });
+ });
+</textarea>
+ <h2>Full Source of Examples</h2>
+ <textarea name="code" class="JScript">
+YUI({
+ logExclude: {
+ 'YUI': true,
+ Event: true,
+ Base: true,
+ Attribute: true,
+ augment: true
+ }
+}).use('node', 'yql', function(Y) {
+ var res = Y.get('#res');
+
+ var q1 = new Y.yql('select * from github.user.info where (id = "davglass")');
+ q1.on('query', function(r) {
+ if (r.results) {
+ var el = Y.Node.create('<div class="mod"></div>'),
+ user = r.results.user;
+ el.set('innerHTML', '<h2>Github (' + user.name + ')</h2>' + 'URL: <a href="http://github.com/' + user.login + '">http://github.com/' + user.login + '</a><br>Public Repos: (' + user['public-repo-count'].content + ')<br>Followers: (' + user['followers-count'].content + ')<br>Following: (' + user['following-count'].content + ')');
+ res.appendChild(el);
+ }
+ });
+
+
+ var q2 = new Y.yql('select * from weather.forecast where location=90210');
+ q2.on('query', function(r) {
+ if (r.results) {
+ var el = Y.Node.create('<div class="mod"></div>');
+ el.set('innerHTML', '<h2>Weather for 90210</h2>' + r.results.channel.item.description);
+ res.appendChild(el);
+ }
+ });
+
+ var q3 = new Y.yql('select * from twitter.user.timeline where (id = "davglass")', function(r) {
+ if (r.query.results) {
+ var data = r.query.results.entry;
+ var h2 = Y.Node.create('<h2><img src="' + data[0].link[1].href + '"> Twitter: ' + data[0].author.name + '</h2>');
+ var ul = Y.Node.create('<ul class="twitter"></ul>');
+ Y.each(data, function(v) {
+ var title = v.title;
+ var len = title.indexOf(':');
+ title = title.substring(len + 2);
+ title = title.replace(/[@]+[A-Za-z0-9-_]+/g, function(f, n, s) {
+ return '<a href="http://twitter.com/' + f.replace('@', '') + '">' + f + '</a>';
+ });
+ var li = Y.Node.create('<li>' + title + '<span></span></li>');
+ ul.appendChild(li);
+ });
+ var wrap = Y.Node.create('<div id="twitter" class="mod"></div>');
+ wrap.appendChild(h2);
+ wrap.appendChild(ul);
+ res.appendChild(wrap);
+ ul.queryAll('li').each(function(v, k) {
+ if (!(k % 2)) {
+ v.addClass('odd');
+ }
+ });
+ }
+ });
+});
+ </textarea>
+ </div>
+ </div>
+ </div>
+ <div id="ft">&nbsp;</div>
+</div>
+<script src="http://developer.yahoo.com/yui/assets/dpSyntaxHighlighter.js"></script>
+<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.0.0b1/build/yui/yui-min.js"></script>
+<script type="text/javascript" src="http://github.com/davglass/yui-yql/raw/master/yql-min.js"></script>
+
+
+
+<script type="text/javascript">
+YUI({
+ logExclude: {
+ 'YUI': true,
+ Event: true,
+ Base: true,
+ Attribute: true,
+ augment: true
+ }
+}).use('node', 'yql', function(Y) {
+ var res = Y.get('#res');
+
+ var q1 = new Y.yql('select * from github.user.info where (id = "davglass")');
+ q1.on('query', function(r) {
+ if (r.results) {
+ var el = Y.Node.create('<div class="mod"></div>'),
+ user = r.results.user;
+ el.set('innerHTML', '<h2>Github (' + user.name + ')</h2>' + 'URL: <a href="http://github.com/' + user.login + '">http://github.com/' + user.login + '</a><br>Public Repos: (' + user['public-repo-count'].content + ')<br>Followers: (' + user['followers-count'].content + ')<br>Following: (' + user['following-count'].content + ')');
+ res.appendChild(el);
+ }
+ });
+
+
+ var q2 = new Y.yql('select * from weather.forecast where location=90210');
+ q2.on('query', function(r) {
+ if (r.results) {
+ var el = Y.Node.create('<div class="mod"></div>');
+ el.set('innerHTML', '<h2>Weather for 90210</h2>' + r.results.channel.item.description);
+ res.appendChild(el);
+ }
+ });
+
+ var q3 = new Y.yql('select * from twitter.user.timeline where (id = "davglass")', function(r) {
+ if (r.query.results) {
+ var data = r.query.results.entry;
+ var h2 = Y.Node.create('<h2><img src="' + data[0].link[1].href + '"> Twitter: ' + data[0].author.name + '</h2>');
+ var ul = Y.Node.create('<ul class="twitter"></ul>');
+ Y.each(data, function(v) {
+ var title = v.title;
+ var len = title.indexOf(':');
+ title = title.substring(len + 2);
+ title = title.replace(/[@]+[A-Za-z0-9-_]+/g, function(f, n, s) {
+ return '<a href="http://twitter.com/' + f.replace('@', '') + '">' + f + '</a>';
+ });
+ var li = Y.Node.create('<li>' + title + '<span></span></li>');
+ ul.appendChild(li);
+ });
+ var wrap = Y.Node.create('<div id="twitter" class="mod"></div>');
+ wrap.appendChild(h2);
+ wrap.appendChild(ul);
+ res.appendChild(wrap);
+ ul.queryAll('li').each(function(v, k) {
+ if (!(k % 2)) {
+ v.addClass('odd');
+ }
+ });
+ }
+ });
+});
+
+dp.SyntaxHighlighter.HighlightAll('code');
+</script>
+</body>
+</html>
View
6 timeout_test.html → tests/timeout.html
@@ -9,7 +9,7 @@
<body class="yui-skin-sam">
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js" type="text/javascript"></script>
- <script src="yql.js" type="text/javascript"></script>
+ <script src="../yql-min.js" type="text/javascript"></script>
<script type="text/javascript">
YUI().use('yql', 'console', 'dump', function(Y){
@@ -25,7 +25,7 @@
console.log('console loaded');
//Using events
- q1 = new Y.yql('select * from github.user.info where (id = "davglass")', null, { timeout:TIMEOUT });
+ q1 = new Y.yql('select * from github.user.info where (id = "davglass")', null, {}, { timeout:TIMEOUT });
q1.on('query', function(r) {
console.log('RESULTS: '+Y.dump(r.results), 'info', 'q1');
});
@@ -47,7 +47,7 @@
console.log('TIMEOUT: '+Y.dump(r), 'warn', 'q2');
}
- }, { timeout:TIMEOUT });
+ }, {}, { timeout:TIMEOUT });
});
View
2 yql-min.js
@@ -1 +1 @@
-YUI.add("yql",function(c){if(!YUI.yql){YUI.yql={}}var b="http://query.yahooapis.com/v1/public/yql?",a=function(e,f,d){a.superclass.constructor.apply(this);this._query(e,f,d)};c.extend(a,c.EventTarget,{_cb:null,_stamp:null,_receiver:function(d){if(d.query){this.fire("query",d.query)}if(d.error){this.fire("error",d.error)}if(this._cb){this._cb(d)}delete YUI.yql[this._stamp]},_query:function(h,i,g){var f=c.stamp({}),d="",e;f=f.replace(/-/g,"_");this._stamp=f;this._cb=i;YUI.yql[f]=c.bind(this._receiver,this);if(!g){g={}}g.q=h;g.format="json";g.callback="YUI.yql."+f;if(!g.env){g.env="http://datatables.org/alltables.env"}c.each(g,function(l,j){d+=j+"="+encodeURIComponent(l)+"&"});e=b+d;c.Get.script(e,{autopurge:true});return this}});c.yql=a},"1.0",{requires:["get","event-custom"],skinnable:false});
+YUI.add("yql",function(c){if(!YUI.yql){YUI.yql={}}var b="http://query.yahooapis.com/v1/public/yql?",a=function(f,g,e,d){a.superclass.constructor.apply(this);this._query(f,g,e,d)};c.extend(a,c.EventTarget,{_cb:null,_stamp:null,_receiver:function(d){if(d.query){this.fire("query",d.query)}if(d.error){this.fire("error",d.error)}if(this._cb){this._cb(d)}delete YUI.yql[this._stamp]},_query:function(i,j,h,g){var f=c.stamp({}),d="",e;f=f.replace(/-/g,"_");this._stamp=f;this._cb=j;YUI.yql[f]=c.bind(this._receiver,this);if(!h){h={}}h.q=i;h.format="json";h.callback="YUI.yql."+f;if(!h.env){h.env="http://datatables.org/alltables.env"}c.each(h,function(m,l){d+=l+"="+encodeURIComponent(m)+"&"});if(!g){g={}}g.autopurge=true;g.context=this;g.onTimeout=function(k){this.fire("timeout",k);if(this._cb){this._cb(k);this._cb=null}};e=b+d;c.Get.script(e,g);return this}});c.yql=a},"1.0",{requires:["get","event-custom"],skinnable:false});
View
20 yql.js
@@ -16,11 +16,12 @@ YUI.add('yql', function(Y) {
* @param {String} sql The SQL statement to execute
* @param {Function} callback The callback to execute after the query (optional).
* @param {Object} params An object literal of extra parameters to pass along (optional).
+ * @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
*/
var BASE_URL = 'http:/'+'/query.yahooapis.com/v1/public/yql?',
- yql = function (sql, callback, options, params) {
+ yql = function (sql, callback, params, opts) {
yql.superclass.constructor.apply(this);
- this._query(sql, callback, options, params);
+ this._query(sql, callback, params, opts);
};
Y.extend(yql, Y.EventTarget, {
@@ -61,9 +62,10 @@ YUI.add('yql', function(Y) {
* @param {String} sql The SQL statement to execute
* @param {Function} callback The callback to execute after the query (optional).
* @param {Object} params An object literal of extra parameters to pass along (optional).
+ * @param {Object} opts An object literal of extra options to pass along to the Get Utility (optional).
* @return Self
*/
- _query: function(sql, callback, options, params) {
+ _query: function(sql, callback, params, opts) {
var st = Y.stamp({}), qs = '', url;
//Must replace the dashes with underscrores
st = st.replace(/-/g, '_');
@@ -88,12 +90,12 @@ YUI.add('yql', function(Y) {
qs += k + '=' + encodeURIComponent(v) + '&';
});
- if (!options) {
- options = {};
+ if (!opts) {
+ opts = {};
}
- options.autopurge = true;
- options.context = this;
- options.onTimeout = function(o){
+ opts.autopurge = true;
+ opts.context = this;
+ opts.onTimeout = function(o){
this.fire('timeout', o);
if (this._cb) {
this._cb(o);
@@ -102,7 +104,7 @@ YUI.add('yql', function(Y) {
};
url = BASE_URL + qs;
- Y.Get.script(url, options);
+ Y.Get.script(url, opts);
return this;
}
});

0 comments on commit 9577897

Please sign in to comment.
Something went wrong with that request. Please try again.