2012-11-30 31 views
9

我正在尝试Yesod Yesod网页上记录的示例示例。其中一个例子是Chat application embedded in Wiki。当我尝试在ghci中(加载从Wiki.hsChat模块,其中包含维基示例代码),我得到下面的错误Chat.hs:尝试一些Yesod示例时编译错误

Chat.hs:122:34: 
    Could not deduce (Text.Julius.ToJavascript Text) 
     arising from a use of `Text.Julius.toJavascript' 
    from the context (YesodChat master) 
     bound by the type signature for 
       chatWidget :: YesodChat master => 
           (Route Chat -> Route master) -> GWidget sub master() 
     at Chat.hs:(81,15)-(83,35) 
    Possible fix: 
     add an instance declaration for (Text.Julius.ToJavascript Text) 
    In the first argument of `Text.Julius.Javascript', namely 
     `Text.Julius.toJavascript output' 
    In the expression: 
     Text.Julius.Javascript (Text.Julius.toJavascript output) 
    In the first argument of `Data.Monoid.mconcat', namely 
     `[Text.Julius.Javascript 
      ((Data.Text.Lazy.Builder.fromText . Text.Shakespeare.pack') 
      "\ 
      \// Set up the receiving end\ 
      \var output = document.getElementById(\""), 
     Text.Julius.Javascript (Text.Julius.toJavascript output), 
     Text.Julius.Javascript 
      ((Data.Text.Lazy.Builder.fromText . Text.Shakespeare.pack') 
      "\");\ 
      \var src = new EventSource(\""), 
     Text.Julius.Javascript 
      (Data.Text.Lazy.Builder.fromText 
      (_render_a3Yr (toMaster ReceiveR) [])), 
     ....]' 

我不是很熟悉耶索德库呢。所以,我被上述错误难住了 - 我在其他的Yesod例子中也看到了这个错误,其中toWidget函数被调用。所以,这似乎与toWidget函数有关。我将不胜感激帮助解决上述错误。

我正在使用ghc 7.6.1yesod 1.1.4.1

更新:

固定以下哈马的建议。我在Chat.hs中做了两处修改。对于rawJS

  1. 添加import语句:

    import Text.Julius (rawJS)

  2. 追查所有的#{}块内julius whamletChat.hs情况,并与{# rawJS ...}

    var output = document.getElementById("#{rawJS output}");

    替换它们

    var input = document.getElementById("#{rawJS input}");

回答

12

我目前没有安装Yesod进行测试,但根据this blog post,当插入JavaScript标识符时,您只需将#{output}更改为#{rawJS output}等等。

+0

谢谢你,@hammar。这正是问题所在。现在在我追踪了julius whamlet中的每个#{}块后,立即修复,并用rawJS替换它。 – Sal

5

shakespeare-js-1.1.0,在ToJavascript实例为Text和一些其他类型已被删除:

-- | A typeclass for types that can be interpolated in CoffeeScript templates. 
class ToJavascript a where 
    toJavascript :: a -> Builder 
#if 0 
instance ToJavascript [Char] where toJavascript = fromLazyText . TL.pack 
instance ToJavascript TS.Text where toJavascript = fromText 
instance ToJavascript TL.Text where toJavascript = fromLazyText 
instance ToJavascript Javascript where toJavascript = unJavascript 
instance ToJavascript Builder where toJavascript = id 
#endif 
instance ToJavascript Value where toJavascript = fromValue 

而在包装之前的版本,他们只是有条件地禁用:

#ifndef SAFER_INTERPOLATION 

我不知道这是否是故意的,是否意味着保持这种状态,或者只是一个被遗忘的发展变化。

要按照原样使用该示例,您需要对shakespeare-js < 1.1重建yesod。这意味着要首先卸载大量的软件包,或者新的沙箱(如果您使用的是cabal-dev或其他沙盒工具)。在shakespeare-js-1.1.0ToJavascript

唯一实例是RawJavascriptaeson包(一个newtype周围Builder包装器)和Value(一种用于JSON值)。

你可以只包output

Text.Julius.toJavascript output 

RawJavascript . Data.Text.Lazy.Builder.fromText获得RawJavascript价值,并如果它是真正的代码它的工作,但因为它是TH-产生的,你需要修复的TH输出或者quasiquoter - 我都不知道该怎么做。

+0

谢谢@ daniel-fischer。事实上,哈马尔指出,rawJS失踪了。 – Sal