Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed typo which caused open tables to stop working.. evn > env.

Added syncColumnTypes. They can be overwitten using the regular setColumnType options

Fixed naming issues.. I named a method query and a propperty query.. Not gonna happen ;) Same counts for timeout.

Fixed issue with callbacks. Tested onTimeout and onError observers. Seem to work correctly. Once timeout has been called. It will cancel the complete request and no data should be recieved once the script finishes executing.
  • Loading branch information...
commit b5a1647f6f4afcd22167b16e2724452fdd094251 1 parent ba90e91
@3rd-Eden authored
Showing with 83 additions and 16 deletions.
  1. +83 −16 SpryDataYQLDataSet.js
View
99 SpryDataYQLDataSet.js
@@ -5,14 +5,15 @@
Spry.Data.YQLDataSet = function( query, path, options ){
- this.YQLServer = "http://query.yahooapis.com/v1/public/yql?";
- this.env = "http://datatables.org/alltables.env";
+ this.YQLServer = "http:/"+"/query.yahooapis.com/v1/public/yql?";
+ this.env = "http:/"+"/datatables.org/alltables.env";
this.format = "json";
this.timeout = 10000;
this.query = query;
this.prefix = "yql_";
this.path = path || "";
this.doc = null;
+ this.preparseFunc = null;
Spry.Data.DataSet.call( this, options );
};
@@ -26,13 +27,13 @@
// stores all public callbacks
Spry.Data.YQLDataSet.receiver = {};
- Spry.Data.YQLDataSet.prototype.query = function query( callback ){
+ Spry.Data.YQLDataSet.prototype.yql = function query( callback ){
var id = ++Spry.Data.YQLDataSet.id,
parameters = {
q: this.query,
format: this.format,
- evn: this.env,
+ env: this.env,
callback: "Spry.Data.YQLDataSet.receiver." + this.prefix + "" + id,
},
@@ -57,7 +58,7 @@
// process the data
if( data.query ){
- callback( data.query );
+ callback.call( that, data.query );
}
// clean up
@@ -91,11 +92,11 @@
// starting the request
this.notifyObservers( "onPreLoad" );
- // find a target to append to
+ // find a target to append to, there is always one script on the page.. or we couldn't be executing this :)!
document.getElementsByTagName( "script" )[0].appendChild( reciever.script );
// set our timeout
- timeouttimer = setTimeout( function(){ self.timeout( reciever, id ) }, this.timeout );
+ timeouttimer = setTimeout( function(){ that.timedOut( reciever, id ) }, this.timeout );
};
@@ -103,16 +104,56 @@
this.dataWasLoaded = false;
if( !this.pendingRequest ){
- this.query();
+ this.yql();
this.pendingRequest = true;
}
};
+ /*
+ By using a JSON data set based setup we can hopefully attach the nestedJSON dataset on
+ the YQLDataSet.
+ */
+ Spry.Data.YQLDataSet.flattenObject = function( obj, basename ){
+ var row = {};
+
+ if( typeof obj == "object" ){
+ Spry.Data.YQLDataSet.copyProps( row, obj );
+ } else {
+ row[ basename || "column0" ] = obj;
+ }
+
+ row.ds_JSONObject = obj;
+ return row;
+ };
+
+ Spry.Data.YQLDataSet.copyProps = function( target, source, ignore ){
+ if( target && source ){
+ for( var row in source){
+ if( ignore && typeof source[ row ] == "object" ){
+ continue;
+ }
+
+ target[ row ] = source[ row ];
+ }
+ }
+
+ return target;
+ };
+
Spry.Data.YQLDataSet.prototype.loadDataIntoDataSet = function loadDataIntoDataSet( data ){
+
this.doc = data;
- var dataset = [],
- hash = {};
+ var dataset = [], // we gonna store the rows in this
+ hash = {}, // our datahash
+ result = data.results ? data.results : data;
+
+ /*
+ As we have no indication on how to parse the data we depend on the users to parse the
+ data for us and make sure we have the correct results to fuse to the dataset
+ */
+ if( this.preparseFunc )
+ result = this.preparseFunc( result );
// todo process the data based on path
@@ -128,18 +169,44 @@
this.filterAndSortData();
this.enableNotifications();
- this.notifyObservers("onPostLoad");
- this.notifyObservers("onDataChanged");
+ this.notifyObservers( "onPostLoad" );
+ this.notifyObservers( "onDataChanged" );
+ };
+
+ Spry.Data.YQLDataSet.prototype.syncColumnTypesToData = function syncColumnTypesToData(){
+
+ // Run through every column in the first row and set the column type
+ // to match the type of the value currently in the column, but only
+ // if the column type is not already set.
+ //
+ // For the sake of performance, there are a couple of big assumptions
+ // being made here. Specifically, we are assuming that *every* row in the
+ // data set has the same set of column names defined, and that the value
+ // for a specific column has the same type as a value in the same column
+ // in any other row.
+
+ // this snipped is based on the JSON Dataset version :), but with 1 less fn call ;)
+
+ var row = this.data[0],
+ colName, type;
+
+ for ( colName in row ){
+ if( !this.columnTypes[ colName ] ) {
+ type = typeof row[ colName ] == "number";
+ if ( type )
+ this.setColumnType( colName, type );
+ }
+ }
};
// call a timeout instance
- Spry.Data.YQLDataSet.prototype.timeout = function timeout( reciever, id ){
+ Spry.Data.YQLDataSet.prototype.timedOut = function timedOut( reciever, id ){
+ var that = this;
this.notifyObservers( "onTimeOut" );
-
// remove script
reciever.script.parentNode.removeChild( reciever.script );
- reciever = function(){}; // ignore the rest
+ // ignore the rest, remove reciever from memory
+ Spry.Data.YQLDataSet.receiver[ that.prefix + "" + id ] = function(){ delete Spry.Data.YQLDataSet.receiver[ that.prefix + "" + id ]; };
- delete Spry.Data.YQLDataSet.receiver[ this.prefix + "" + id ];
}
})()
Please sign in to comment.
Something went wrong with that request. Please try again.