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

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

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.neteditablegrid.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:

&lt;div id=&quot;grid&quot;&gt;&lt;/div&gt;
&lt;script&gt;
  var rows = {/* rows */};
  var cols = [/* columns */]
  new GridBuilder(&#39;grid&#39;,cols,rows);
&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):

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

&lt;div id=&quot;grid&quot;&gt;&lt;/div&gt;
&lt;script&gt;
  var rows = {/* rows */};
  var cols = [/* columns */];
  var node = document.getElementById(&#39;grid&#39;);
  Elm.App.embed(node, { rows: rows, cols: cols });
&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.

type alias Flags =
    { rows : [ ... ]
    , cols : [ ... ]
    }

init : Flags -&gt; ( 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.

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:

确定