Go 将Elm与现有web应用程序服务集成的正确方法
我有一个我在Go 将Elm与现有web应用程序服务集成的正确方法,go,elm,Go,Elm,我有一个我在Golang中编写的后端服务,类似这样: func PageA_RequestHandler(ctx *W.Context) { // init things if is_ajax { // handle the API request, render the JSON return } // query the initial rows values := M.SX{ `rows`: model1.GetRows(10), `co
Golang
中编写的后端服务,类似这样:
func PageA_RequestHandler(ctx *W.Context) {
// init things
if is_ajax {
// handle the API request, render the JSON
return
}
// query the initial rows
values := M.SX{
`rows`: model1.GetRows(10),
`columns`: model1.GetColumns(),
}
// render the html
ctx.Render(`page_a_template`,values)
}
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */]
new GridBuilder('grid',cols,rows);
</script>
main =
Html.programWithFlags
{ init = init, ... }
然后文件page\u a\u template.html
(在第一次呈现时加载并缓存)是一个html文件,其内容如下:
func PageA_RequestHandler(ctx *W.Context) {
// init things
if is_ajax {
// handle the API request, render the JSON
return
}
// query the initial rows
values := M.SX{
`rows`: model1.GetRows(10),
`columns`: model1.GetColumns(),
}
// render the html
ctx.Render(`page_a_template`,values)
}
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */]
new GridBuilder('grid',cols,rows);
</script>
main =
Html.programWithFlags
{ init = init, ... }
变量行={/*行*/};
var cols=[/*列*/]
新的网格生成器(“网格”、列、行);
在哪里:
{/*行*/}
和[/*列*/]
是我的javascript友好模板语法,还有一些其他语法,如:/*!条形码*/
或{yay}
newgridbuilder
是我的自定义javascript组件,它创建类似于datatables.net
或editablegrid.net
问题是,如果我使用Elm
,将{/*rows*/}
注入已编译html的正确方法是什么?使用(假设您的主模块名为App
):
您的main
函数如下所示:
func PageA_RequestHandler(ctx *W.Context) {
// init things
if is_ajax {
// handle the API request, render the JSON
return
}
// query the initial rows
values := M.SX{
`rows`: model1.GetRows(10),
`columns`: model1.GetColumns(),
}
// render the html
ctx.Render(`page_a_template`,values)
}
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */]
new GridBuilder('grid',cols,rows);
</script>
main =
Html.programWithFlags
{ init = init, ... }
我强烈建议您阅读本节的内容。它解释了带有标志的程序以及从JavaScript接收/向JavaScript发送数据的其他方法