Question

I'm using the Yesod scaffolded site (yesod 1.1.9.2) and spent a few hours yesterday wrapping my head around basic usage of Fay with Yesod. I think I now understand the intended workflow for using Fay to add a chunk of AJAX functionality to a page (I'm going to be a little pedantic here just because someone else might find the step-by-step helpful):

  • Add a data constructor Example a to SharedTypes.Command.
  • In the expression case readFromFay Command of ... in Handler.Fay.onCommand, add a case that matches on my new data constructor.
  • Create a Fay file 'Example.hs' in /fay, patterned after fay/Home.hs. Somewhere in here, use the expression call (Example "foo") $ myFayCallback.
  • Define a route and handler for the page that will use the Javascript I'm generating. In the handler, use $(fayFile' (ConE 'ScriptR) "Example.hs").

My question: In the current Yesod/Fay architecture, how should I go about sharing my Persistent model types with my Fay code?

Using import Model in a Fay file doesn't work -- when I try to load the page that's using this Fay file, I get an error in the browser (Fay's standard way of alerting me to errors, I guess) indicating that it couldn't find module 'Model' but that it only searched the following directories:

  • projectroot/cabal-dev//share/fay-0.14.2.0/src
  • projectroot/cabal-dev/share/fay-base-0.14.2.0/src
  • projectroot/cabal-dev/share/fay-base-0.14.2.0
  • projectroot/fay
  • projectroot/fay-shared

I also tried importing and re-exporting Model in SharedTypes.hs, but that produced the same error.

Is there a way to do this? If not, why not? (I'm a relative noob in both Haskell and Yesod, so the answer to the "why not?" question would be really helpful.)

EDIT:

I just realized that mentioning Persistent in this question's title might be misleading. To be clearer about what I'm trying to do: I just want to be able to represent data in my Fay code using the same datatypes Yesod defines for my models. E.g. if I define a model thusly in config/models...

Foo
    bar BarId
    textThatCanBeNull Text Maybe
    deriving Show

... I want to be able to define an AJAX 'command' that receives and/or returns a value of type Foo and have my Fay code deal in Foos without me having to write any de/serialization code. I understand that I won't be able to use any of Persistent's query functionality directly from my Fay code; I only mentioned Persistent in the title because I mentally associate everything in Model.hs and config/models with Persistent.

Was it helpful?

Solution

This currently is not supported; there are many features leveraged by Persistent which Fay does not support (e.g., Template Haskell). For now, it probably makes sense to have an intermediate data type which is shared by both Fay and Yesod and convert your Persistent data to/from that type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top