2017-01-23 81 views
1

我试图定义一个小部件的数据源作为查询的结果,但我不确定它是否可行。查询为小工具数据源

我正在使用SQL视图和Table,我想显示来自视图的表上的ID的值。

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(); 
} 

调用它像:

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

在这种情况下,小部件是一个表,所以温控功能会重复在talbe

的问题是,无论是我得到的项目数量与物品数量或第一个不起作用的时间相同的结果!

而第二个问题是,结果不会写出要显示的小部件文本。 enter image description here

这是一个非常简单的功能,我找到了一些解决方法,但没有与数据源功能,他们工作得太慢,任何sugestions?数据源可以做这样的事吗?

+0

从屏幕截图看来,您似乎试图显示关系的ID。如果你有两个模型(表),比如Employee和Manager,或者你有自引用的Employee模型(表),那么你可以为你的数据源添加预取。在这种情况下,您可以像这样绑定Manager Id:@ datasource.item.Manager.Id。文档:https://developers.google.com/appmaker/models/datasources#prefetch –

+0

由于它是一个SQL视图,它没有关系,所以我必须手工查询=) –

+0

在这种情况下,如果表是仅供查看,我会建议使用计算的数据源。这将减少对服务器和数据库的调用次数,因此您可以提高整体页面性能并摆脱闪烁的标签。在目前的实现中,对于具有N行应用的表格,至少会有(N + 1个调用服务器)+(N + 1个调用数据库)。使用计算的数据源,您可以将其降至1 + 1 = 2个呼叫。 –

回答

2

如果我理解正确的问题,你可能想在服务器端进行查询。发布示例代码的问题是它在任何负载可能返回之前多次触发单个数据源的负载。完成此操作后,数据源仅加载其中一个加载的结果,我相信是最后一个。所以你可能会看到你为所有回调所做的最后一个查询的结果。

因此,而不是你的代码,而不是成为一个服务器端脚本,应该是这样的:

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; 
} 

(书面从内存中,所以请原谅任何错误。)

+0

再次感谢德文! 经过一些小的tweeks解决方案工作! –

2

继德文的建议:

前端

/***************************************************************************** 
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 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]; 
} 

是否必须通过widget.text值? successHandler回调是异步的,所以正常返回只会给我空值

+0

感谢您提供最终的工作解决方案! –