Juan Diego Antezana
Juan Diego Antezana

Reputation: 912

Query as Widget Datasource

I'm trying to define a widget's datasource as the result of a query, but I'm not sure if it's posible.

I'm working with SQL views and a Table, I'd like to show the values of the IDs that I have on the table that come from the views.

function queryValue(source, model, key){
  console.log("source " + source);
  app.datasources[model].query.filters.id._equals = source;
  app.datasources[model].load(function () {
    console.log(app.datasources.users.items[0][key]);
    return app.datasources.users.items[0][key];
  });
  app.datasources[model].query.clearFilters();
}

Calling it like:

queryValue(@datasource.item.[the_id], "[the_SQLView_Datasouce]", "[the_field_i_want]");

In this case the widget is a table, so the fucntion would repeat the amount of items in the talbe

The problem is that either I get the same result as meny times as the amount of items or the first one does not work!

And the second problem is that the result does not over write the widget text to show. enter image description here

It's a very simple function and I did found some workarounds but not with the datasource feature and they work too slowly, any sugestions? Is it possible to do such thing with the datasource?

Upvotes: 2

Views: 429

Answers (2)

Juan Diego Antezana
Juan Diego Antezana

Reputation: 912

Following Devin's suggestion:

Front-end

/*****************************************************************************
Front-end function that calls the querying function @queryValue(source, model, key) in controller_TransformId
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query
@wwidget => the widget making the request
This function works as a model to manage the transactions between the 
controller at the backend and the view.  
******************************************************************************/
function buildTransformID(source, model, key, widget){ 
  google.script.run.withSuccessHandler(
    function successHandler(expectedValue){
      widget.text = expectedValue;})
  .withFailureHandler(
    function failureHandler(){
      widget.text = "undefined";})
  .queryValue(source, model, key);
}

Back-end

/*****************************************************************************
Back-end function that queries the database
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query    
This function works works as a controller to query the database from the backend    ******************************************************************************/
function queryValue(source, model, key){ 
  var query = app.models[model].newQuery();
  query.filters.id._equals = source;
  var results = query.run();
  console.log("CONTROLLER return :" + results[0][key]);
  return results[0][key];
}

Is it mandatory to pass through the widget.text value? the successHandler callback is asynchronous so regular returns would just give me nulls

Upvotes: 2

Devin Taylor
Devin Taylor

Reputation: 825

If I understand the question correctly, you probably want to do the query on the server side. The issue with the sample code posted is it's triggering a load on a single data source multiple times before any of the loads can return. When this is done, the data source is only loaded with the results from one of the loads, I believe the last one. So you're probably seeing the results from the last query you did for all your callbacks.

So instead your code should instead be a server side script, and should be something like:

function queryValue(source, model, key){
  console.log("source " + source);
  var query = app.models.newQuery();
  query.filters.id._equals = source;
  var results = query.run;
  return results[0].key;
}

(Written from memory, so pardon any errors.)

Upvotes: 2

Related Questions