将Elm与现有的Web应用程序服务集成的正确方法是什么?

huangapple go评论113阅读模式
英文:

Correct way to integrate Elm with existing web-app service

问题

我有一个后端服务,是用Golang编写的,类似这样:

  1. func PageA_RequestHandler(ctx *W.Context) {
  2. // 初始化操作
  3. if is_ajax {
  4. // 处理 API 请求,返回 JSON
  5. return
  6. }
  7. // 查询初始行
  8. values := M.SX{
  9. `rows`: model1.GetRows(10),
  10. `columns`: model1.GetColumns(),
  11. }
  12. // 渲染 HTML
  13. ctx.Render(`page_a_template`,values)
  14. }

然后,template 文件 page_a_template.html(在第一次渲染时加载和缓存)是一个 HTML 文件,内容类似这样:

  1. <div id="grid"></div>
  2. <script>
  3. var rows = {/* rows */};
  4. var cols = [/* columns */]
  5. new GridBuilder('grid',cols,rows);
  6. </script>

其中:

{/* rows */}[/* columns */] 是我友好的 JavaScript 模板语法,还有其他语法,比如:/*! bar */#{yay}

new GridBuilder 是我自定义的 JavaScript 组件,用于创建类似于 datatables.neteditablegrid.net 的东西

问题是,如果我使用 Elm,在编译后的 HTML 中如何正确地注入 {/* rows */}

英文:

i have a backend service that I wrote in Golang, something like this:

  1. func PageA_RequestHandler(ctx *W.Context) {
  2. // init things
  3. if is_ajax {
  4. // handle the API request, render the JSON
  5. return
  6. }
  7. // query the initial rows
  8. values := M.SX{
  9. `rows`: model1.GetRows(10),
  10. `columns`: model1.GetColumns(),
  11. }
  12. // render the html
  13. ctx.Render(`page_a_template`,values)
  14. }

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:

  1. &lt;div id=&quot;grid&quot;&gt;&lt;/div&gt;
  2. &lt;script&gt;
  3. var rows = {/* rows */};
  4. var cols = [/* columns */]
  5. new GridBuilder(&#39;grid&#39;,cols,rows);
  6. &lt;/script&gt;

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):

  1. <div id="grid"></div>
  2. <script>
  3. var rows = {/* rows */};
  4. var cols = [/* columns */];
  5. var node = document.getElementById('grid');
  6. Elm.App.embed(node, { rows: rows, cols: cols });
  7. </script>

这是你可以像初始化GridBuilder一样使用可用数据初始化你的Elm应用程序的方法。

为了能够读取这些值,你应该创建一个Flags类型别名,并且你的init函数将接收它。

  1. type alias Flags =
  2. { rows : [ ... ]
  3. , cols : [ ... ]
  4. }
  5. init : Flags -> ( Model, Cmd Msg )
  6. init flags =
  7. ...

你的main函数将如下所示:

  1. main =
  2. Html.programWithFlags
  3. { init = init, ... }

我强烈建议你阅读Elm指南中的JavaScript互操作性部分。它解释了programWithFlags和其他从JavaScript接收/发送数据的方法。

英文:

Use Html.programWithFlags (supposing your main module is named App):

  1. &lt;div id=&quot;grid&quot;&gt;&lt;/div&gt;
  2. &lt;script&gt;
  3. var rows = {/* rows */};
  4. var cols = [/* columns */];
  5. var node = document.getElementById(&#39;grid&#39;);
  6. Elm.App.embed(node, { rows: rows, cols: cols });
  7. &lt;/script&gt;

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.

  1. type alias Flags =
  2. { rows : [ ... ]
  3. , cols : [ ... ]
  4. }
  5. init : Flags -&gt; ( Model, Cmd Msg )
  6. init flags =
  7. ...

Your main function will look like this:

  1. main =
  2. Html.programWithFlags
  3. { 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.

huangapple
  • 本文由 发表于 2017年2月22日 14:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/42384326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定