Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to handle javascript objects in Fay.</p> <h3>Opaque data types</h3> <p>Using EmptyDataDecls:</p> <pre><code>{-# LANGUAGE EmptyDataDecls #-} [...] data JQuery </code></pre> <p>This is what fay-jquery does, it allows users of the library to define new FFI bindings easily, so your size example works. But note that it has been deprecated in jQuery, so it's better to use <code>length</code> as John B suggests.</p> <p>Whenever fay encounters something that should be of type JQuery it won't touch it so you will have a direct reference to the javascript object.</p> <h3>Records</h3> <p>The other approach is to map a javascript object to a record, this is the nicer option if the object in question maps easily, such as when handling JSON or doing client-server communication.</p> <pre><code>data User = User { name :: String } userA :: User userA = ffi "{ instance : \"User\", name : \"Adam\" }" userB :: User userB = User { name = "Adam" } </code></pre> <p>These are identical ways of constructing a User record, but userA is of course a little contrived, probably the value would be fetched from the server side or from some JS library in that case (otherwise there is no reason to use the FFI at all) </p> <p>In this record case you will not keep a reference to a JS object, an immutable User record will be instantiated.</p> <p>I also think you can skip <code>Fay</code> in the return type. I think it's safe to assume that the jQuery objects won't mutate, especially if they are instantiated from Fay.</p> <pre><code>length :: JQuery -&gt; Int length = ffi "%1['length']" </code></pre> <p>Also see the <a href="https://github.com/faylang/fay/wiki/Foreign-function-interface" rel="nofollow">FFI wiki page</a>.</p> <p>Edit: Slightly related, <a href="https://github.com/faylang/fay-uri/blob/master/src/Uri.hs" rel="nofollow">fay-uri</a> is a well documented and fairly simple example of how to build an immutable FFI layer on top of a mutable js library.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload