2017-01-23 34 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

感謝您提供最終的工作解決方案! –