我想要创建一个根据给定参数和地图生成HTML的服务。给定参数,服务在映射中搜索html,并在客户端启动一个函数。
type sample =
(string (* little text *)*
Html5_types.html Eliom_content.Html5.elt (* html page *) *
(unit -> unit)(* Demonstration function *))考虑到函数将在客户端启动,我将它作为客户端值插入到映射中:
{client{
let demo_function = ignore (Ojquery.add_html
(Ojquery.jQ "li") "<p id='test1'>new paragraph</p>") }}
let get_samples () =
let samples_map = Samples.empty in
let samples_map = Samples.add "add_html"
("text",
(Eliom_tools.F.html
(** html stuff **)
),
{unit->unit{demo_function}}) samples_map in
samples_map然后我像这样注册服务:
let sample_service =
Eliom_service.service
~path:["examples"]
~get_params:Eliom_parameter.(string "entry")
()
let () =
Examples_app.register
~service:sample_service
(fun (entry) () ->
try
(let entry = Samples.find entry samples_map in
let html = ((function (name, html, func) -> html) entry) in
let func = ((function (name, html, func) -> func) entry) in
ignore {unit{%func ()}};
Lwt.return (html))
with Not_found -> Lwt.return (not_found)
)代码的其余部分基本上只是一个经典的eliom酿酒厂的结果,其中包含了用于客户端函数的ojquery包。编译阶段进行得很顺利,但是当我试图启动服务器时,我会得到以下错误消息:
ocsigenserver: main: Fatal - Error in configuration file: Error while parsing configuration file: Eliom: while loading local/lib/examples/examples.cma: Failure("That function cannot be called here because it needs information about the request or the site.")
我最初的猜测是,这是因为我将客户端值存储在服务之外,但是有什么方法可以在服务器上存储这种值吗?
我试图将它们包装成常规函数:let demo_serv_func () = {unit{demo_client_func ()}}
但问题依然存在..。
发布于 2013-08-05 15:20:20
我发现了问题。问题不是因为我存储了客户端函数,而是因为我在服务之外使用了Eliom_tools.F.html。
碰巧,Eliom_tools需要服务的上下文才能起作用,而且由于我将它存储在服务之外,所以它无法工作。
通过在服务中使用Eliom_tools并将HTML页面的主体存储在地图中,我解决了这个问题。
https://stackoverflow.com/questions/18023145
复制相似问题