英文:
Correct way to integrate Elm with existing web-app service
问题
我有一个后端服务,是用Golang编写的,类似这样:
func PageA_RequestHandler(ctx *W.Context) {
// 初始化操作
if is_ajax {
// 处理 API 请求,返回 JSON
return
}
// 查询初始行
values := M.SX{
`rows`: model1.GetRows(10),
`columns`: model1.GetColumns(),
}
// 渲染 HTML
ctx.Render(`page_a_template`,values)
}
然后,template 文件 page_a_template.html(在第一次渲染时加载和缓存)是一个 HTML 文件,内容类似这样:
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */]
new GridBuilder('grid',cols,rows);
</script>
其中:
{/* rows */} 和 [/* columns */] 是我友好的 JavaScript 模板语法,还有其他语法,比如:/*! bar */ 或 #{yay}
new GridBuilder 是我自定义的 JavaScript 组件,用于创建类似于 datatables.net 或 editablegrid.net 的东西
问题是,如果我使用 Elm,在编译后的 HTML 中如何正确地注入 {/* rows */}?
英文:
i have a backend service that I wrote in Golang, something like this:
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)
}
then the template file page_a_template.html (that loaded and cached at the first time it rendered), is a html file, with content something like this:
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */]
new GridBuilder('grid',cols,rows);
</script>
Where's:
{/* rows */} and [/* columns */] are my javascript-friendly template syntax, there are some other syntax like: /*! bar */ or #{yay}
new GridBuilder is my custom javascript component that creates something like datatables.net or editablegrid.net
The question is, if I use Elm, what's the correct way to inject the {/* rows */} into the compiled html?
答案1
得分: 6
使用Html.programWithFlags(假设你的主模块名为App):
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */];
var node = document.getElementById('grid');
Elm.App.embed(node, { rows: rows, cols: cols });
</script>
这是你可以像初始化GridBuilder一样使用可用数据初始化你的Elm应用程序的方法。
为了能够读取这些值,你应该创建一个Flags类型别名,并且你的init函数将接收它。
type alias Flags =
{ rows : [ ... ]
, cols : [ ... ]
}
init : Flags -> ( Model, Cmd Msg )
init flags =
...
你的main函数将如下所示:
main =
Html.programWithFlags
{ init = init, ... }
我强烈建议你阅读Elm指南中的JavaScript互操作性部分。它解释了programWithFlags和其他从JavaScript接收/发送数据的方法。
英文:
Use Html.programWithFlags (supposing your main module is named App):
<div id="grid"></div>
<script>
var rows = {/* rows */};
var cols = [/* columns */];
var node = document.getElementById('grid');
Elm.App.embed(node, { rows: rows, cols: cols });
</script>
This is the way you can initialize your Elm app with available data just like you are initializing GridBuilder.
To be able to read those values, you should create a Flags type alias and your init function will receive it.
type alias Flags =
{ rows : [ ... ]
, cols : [ ... ]
}
init : Flags -> ( Model, Cmd Msg )
init flags =
...
Your main function will look like this:
main =
Html.programWithFlags
{ init = init, ... }
I highly recommend that you read the JavaScript Interop section of the Elm Guide. It explains the programWithFlags and other approaches to receive/send data from/to JavaScript.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论