Yesod,如何从Javascript/Julius中的JSON数据生成类型安全链接

dav*_*dbe 9 haskell yesod

我有一条路

/notes/#NoteId    NoteR    GET
Run Code Online (Sandbox Code Playgroud)

从另一个页面,我想链接到它.使用"经典"小村庄时,很容易:

<a href=@{NoteR $ entityKey note}>notetitle
Run Code Online (Sandbox Code Playgroud)

我希望我的页面更加动态,并获取包含note-information和note-id的JSON数据.如何生成正确的类型安全的链接?

我已将此代码放在一个.julius文件中,但无法编译,因为它需要一个"NoteId".我应该obj.id在URL插值@ {..}中插入某个地方...任何线索如何做到这一点?

function loadnotes() {
var list = $("#results");
jQuery.getJSON("@{NotesR}",
    function(o){
        $.each(o, function (i, obj) {
            $('<a href=@{NoteR}/>' + obj.title + '</a>').appendTo(list);
        })});
}
window.onload = loadnotes;
Run Code Online (Sandbox Code Playgroud)

编辑:

我有这个Model.hs:

instance ToJSON (Entity Note) where
    toJSON (Entity nid (Note title content created_at updated_at userId)) = object
        [ "id" .= nid
        , "title" .= title
        , "content" .= (unTextarea content)
        , "created_at" .= created_at
        , "updated_at" .= updated_at
        , "userId" .= userId ]
Run Code Online (Sandbox Code Playgroud)

Mic*_*man 6

我建议让NotesR路由返回完全呈现的URL而不仅仅是注释ID.

编辑:我添加了一个菜谱条目来演示这种方法:https://github.com/yesodweb/yesod/wiki/Using-type-safe-urls-from-inside-javascript