Yesod: Using typesafe URLs in AJAX calls

家住魔仙堡 提交于 2019-12-04 02:57:56

After some searching I found this discussion, where it is suggested that you add the url as a "data-url" attribute to some element. And then load the url from the element. Something like this:

<div id="thread-1" data-hide-url=@{ApiHideTHreadR}>

var url = $("#thread-1").data("hide-url")

What I always do is get rid of the explicit parameter passing in the route call and instead replace it with :

getApiHideThreadR::Handler JSON
getApiHideThreadR = do 
  rawTextParam <- lookupGetParam "text"
  rawThreadId  <- lookupGetParam "table"
  (textParam,threadParam) <- someParseFunction rawTextParam rawThreadId
  ... 

Then you can use regular ajax style:

$.getJSON("@{ApiHideThreadR}",{text:"sometext",tabe:"sometable"},success()...

For more complex get to haskell type requests: https://github.com/yesodweb/yesod/wiki/Convert-get-params-into-a-haskell-record

Is a nice template.

You can't use a type-safe route to check at compile-time somthing that is known only at run-time. I suspect you know this too, but that is the only sense I can make of your question. Thus, your only option is doing it manually.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!