1

I'd like to return a reference to a Javascript object from an FFI function definition in order to be able to manipulate the object later on:

import           Data.Aeson
import           Foreign.JavaScript (JSObject, NewJSObject, unsafeCreateJSObject)
import           Foreign.RemotePtr (RemotePtr)
import           Graphics.UI.Threepenny.Core hiding (text)

mkJSObject :: JSFunction NewJSObject -> UI JSObject
mkJSObject f = askWindow >>= liftIO . flip unsafeCreateJSObject f

polyLine :: [[Double]] -> Value -> UI JSObject
polyLine points = mkJSObject . f points
  where
    f :: [[Double]] -> Value -> JSFunction NewJSObject
    f = ffi "L.polyline(%1, %2).addTo(map)"

But the underlying Javascript Window doesn't seem to be publicly accessible. Is there another way for creating references to Javascript objects?

1 Answers1

1

It's actually much simpler: Just return a JSObject from JSFunction and it will be marshalled correctly:

polyLine :: [[Double]] -> Value -> UI JSObject
polyLine points = callFunction . f points
  where
    f :: [[Double]] -> Value -> JSFunction JSObject
    f = ffi "L.polyline(%1, %2).addTo(map)"