Answer the question
In order to leave comments, you need to log in
Why don't Go + js templates change?
I want to change the "main" template to "page2" .
{{define "index"}}
{{template "head"}}
<div class = "scene">
{{template "main"}}
</div>
{{template "foot"}}
{{end}}
func WebSocketConnect(w http.ResponseWriter,r *http.Request) {
conn,err := upGrader.Upgrade(w, r, nil)
defer conn.Close()
if err != nil {
log.Println(err)
return
}
webSocketHandler(conn)
}
func webSocketHandler(conn *websocket.Conn) {
data := conn.ReadJSON(&msg)
if data != nil {
log.Fatal(data)
return
}
LoadVista(msg.Temp,conn)
}
var (
indexTemplate = template.Must(template.ParseFiles(indexVista,mainVista,headVista,footVista))
page2Template = template.Must(template.ParseFiles(loginVista))
)
/****************************************************************************/
func LoadVista(nameTemplate string, conn *websocket.Conn) {
switch nameTemplate {
case "page2":
templateWsRender(nameTemplate,conn)
}
}
func templateWsRender(name string,conn *websocket.Conn){
//defer conn.Close()
var buf bytes.Buffer
data := page2Template.ExecuteTemplate(&buf,name,nil)
if data!=nil{
log.Fatal(data)
}
err := conn.WriteMessage(1,buf.Bytes())
if err!=nil{
log.Fatal(err)
}
}
var socket = new WebSocket('ws://localhost:7030/ws' )
socket.onmessage = function (event) {
var message = event.data
loadVista(message)
}
elem.addEventListener( "click" , sendQuery);
function sendQuery() {
var page = {
query_type :"openLink",
temp: "page"
}
var event = JSON.stringify(page);
socket.send(event);
}
function loadVista(message) {
//alert(message)
document.getElementById("scene").innerHTML = message.data;
}
Answer the question
In order to leave comments, you need to log in
if you can see by //alert(message) that the code is coming from another template, then message.data; - incorrect expression.
var a = {'data':1};
alert(a); //[object]
// если бы там был валидный объект, вы бы не увидели содержимого через alert
message = JSON.parse(message);
document.getElementById("scene").innerHTML = message.data;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question