当变量更新golang时,动态刷新模板的一部分 [英] Dynamically refresh a part of the template when a variable is updated golang

查看:357
本文介绍了当变量更新golang时,动态刷新模板的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Golang中,更新变量时可以刷新模板的一部分?
这是我们可以在Angular.js中找到的东西。



基本上在我的代码中,我用ajax中的邮编查找地址,它显示我的找到此邮递区号的地址列表。

以下是模板示例:



目前,ajax称为GO例程。我不想在JS中获得结果,并从ajax调用的结果中构建html。



如果可能,我想只更新地址值(它是一个数组),因为我已经动态地创建了addr列表。

任何线索?

解决方案

模板引擎不支持这种开箱即用的功能。



所以你必须自己实现它。这并不难。以下是您应该遵循的步骤来实现它:



1。重构您的模板



通过使用 {{definename}} 来分隔呈现地址的模板c $ c>行动。您可以将它放在不同的模板文件中,并使用 {{templatename}} 动作将其包含在当前位置,也可以将其保留在原始位置并使用定义模板并立即执行的新的 {{blockname管道}} T1 {{end}} 动作(在Go 1.6中引入)。 / p>

2。修改/创建处理程序



确保您有一个只执行这个呈现收件人的新模板的处理程序。执行Addressees模板的结果应直接发送到输出( w http.ResponseWriter )。



注意您可以使用1个处理程序执行完整页面模板和仅执行收件人模板(例如,根据URL参数进行判断),或者您可能有2个不同的处理程序,这真的取决于您。



3。修改客户端



现在在客户端,当您想要刷新/重新显示地址时,请对仅执行并呈现地址模板的处理程序进行AJAX调用。 p>

当此AJAX调用完成时,您可以简单地使用AJAX调用的响应文本来替换地址包装器标记的HTML内容。



它可能看起来像这样:

  var e = document.getElementById(addressees); 
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4&& xhr.status == 200){
e.outerHTML = xhr.responseText;


xhr.open(GET,path-to-addresses-render,true);
尝试{
xhr.send();
} catch(err){
//处理错误
}

Gowut (这是一个用于构建单页Web应用程序的Web框架使用纯粹的Go)做类似的更新只有部分网页没有完整的页面重新加载(尽管它不使用后端模板)。 (披露:我是Gowut的作者。)



您可以查看确切的Javascript代码,Gowut是如何在 js.go 文件(寻找 rerenderComp() Javascript函数)。


In Golang, it is possible to refresh a part of a template when a variable is updated ? This is something that we can find in Angular.js for instance.

Basically in my code, I lookup adresses by a postcode in ajax, which display my list of addr found for this postcode.

Here is a sample of the template :

At the moment, the ajax calls a GO routine. I don't want to get the result in the JS and build the html from the result of the ajax call.

I would like, if possible, to update only the Addresses value (it's an array) as I already build the list of addr dynamically.

Any clue ?

解决方案

The template engine does not support this out-of-the-box.

So you have to implement it yourself. It's not that hard. Here are the steps you should follow to achieve it:

1. Refactor your templates

Separate the template that renders the Addresses to be on its own by using a {{define "name"}} action. You may put it in a different template file and include it in its current place by using {{template "name"}} action, or you may leave it in the original place and use the new {{block "name" pipeline}} T1 {{end}} action (introduced in Go 1.6) which defines the template and executes it at once.

2. Modify/create handlers

Make sure you have a handler which executes only this new template that renders the Addressees. The result of executing the Addressees template should be sent directly to the output (w http.ResponseWriter).

Note that you may use 1 handler to execute both the "full" page template and only the Addressees template (e.g. deciding based on a URL parameter), or you may have 2 distinct handlers, it's really up to you.

3. Modify client side

Now at client side when you want to refresh / rerender the Addressees, make an AJAX call to the handler that only executes and renders the Addressees template.

When this AJAX call completes, you may simply use the response text of the AJAX call to replace the HTML content of the wrapper tag of Addressees.

It may look something like this:

var e = document.getElementById("addressees");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        e.outerHTML = xhr.responseText;
    }
}
xhr.open("GET", "path-to-addresses-render", true);
try {
    xhr.send();
} catch (err) {
    // handle error
}

Gowut (which is a web framework for building single-page web applications using pure Go) does something similar to update only parts of the web page without full page reload (although it doesn't use templates at the back-end side). (Disclosure: I'm the author of Gowut.)

You may check out the exact Javascript code how Gowut does it in its js.go file (look for the rerenderComp() Javascript function).

这篇关于当变量更新golang时,动态刷新模板的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆