2017-07-26 99 views
0

我有一個路由設置,它使用動態HTML模板進行響應。Golang在等待數據加載時顯示靜態HTML模板

package main 

import (
    "net/http" 
    "html/template" 
) 

func index(w http.ResponseWriter, r *http.Request) { 
    showWwResult, _ := GetWw() 
    showHoursResult, _ := GetHours() 

    type Data struct { 
     ShowWwResult []IssueResult 
     ShowHoursResult Page 
    } 

    data := Data{showWwResult, showHoursResult} 

    var templates = template.Must(template.ParseFiles("templates/index.html", "templates/ww.html", "templates/hours.html")) 
    templates.ExecuteTemplate(w, "indexPage", data) 
} 

我的問題是,它需要很長時間才能收集數據,並且頁面在呈現HTML之前等待它返回。

我怎樣才能得到它返回東西,什麼,而我等待GetWw()GetHours()完成?有什麼方法可以顯示我的HTML模板的靜態部分,然後在ShowWwResultShowHoursResult準備好時填充頁面?

+0

我認爲你可以做的最好的是提供一個「請稍候」的重定向頁面,但我不是一個HTTP專家...... –

回答

2

不,最好的方法是提供模板,然後使用Ajax調用來填充它,然後使用您要使用的數據返回json的端點。