faylang/fay-jquery

Can't see how to add content to the document

Closed this issue · 4 comments

When compiling the following with an HTML wrapper, I'd expect the resulting page to say "Hello, world" and to have two console log messages in it.

{-# LANGUAGE OverloadedStrings, RebindableSyntax #-}
module JQueryProblem where

import Fay.Text
import FFI
import JQuery
import Prelude

writeLog :: Text -> Fay ()
writeLog = ffi "console.log(%1)"

main = do
    writeLog "before appending"
    select "<h1>Hello, world</h1>" >>= appendTo "body"
    writeLog "after appending"

I get the log messages, but not the <h1>, and I can't see why.

However, typing the following into the console does work:

jQuery('<h1>Hello from JS</h1>').appendTo('body')

I think I had problems with this the other day as well, but I didn't dig into it.

I have an inkling that this would fix the problem:

appendTo :: a -> JQuery -> Fay JQuery
appendTo = ffi "%2['appendTo'](Fay$$_(%1))"

I don't have a computer I can test this on right now, can you try it? If it does fix it we may want to change the behavior of free type variables to implicitly force the argument.

Btw, we should probably change this type signature to be Selectable a => a, though that doesn't fix the issue.

Unfortunately not: https://gist.github.com/hdgarrood/8115955

When I open the HTML file in a browser, none of the three actions to add content seem to have worked, but "done" does appear in the console.

Got a chance to test this out now, you need to wait for document ready:

main = ready $ do
    writeLog "before appending"
    select "<h1>Hello, world</h1>" >>= appendTo "body"
    writeLog "after appending"

ohhh! facepalm thanks