faylang / fay

A proper subset of Haskell that compiles to JavaScript
https://github.com/faylang/fay/wiki
BSD 3-Clause "New" or "Revised" License
1.28k stars 86 forks source link

Is there some way to add templating support to Fay? #218

Closed qrilka closed 9 years ago

qrilka commented 11 years ago

I have looked through fay examples and everywhere I see manual DOM construction/manipulation e.g. here - https://github.com/faylang/fay-server/blob/master/src/Client.hs#L103 For relatively small application it is OK, but for large UIs I think it would be very cumbersome. Using some kind of templating would be more convenient. At the moment I find Ember.js appoach (except that it is in js) quite good - it is very easy (at least for me) to see where the data comes from into HTML and what handlers will be called for buttons etc. In fay-server I see ReactiveMVC module (which could help in reproducing some functionality of Ember.js in Fay though not about templating) but I could not find any example of its usage. BTW I'm filing this issue (mainly a question for discussion) because I did not find anything like mailing list or google group about Fay.

chrisdone commented 11 years ago

Yeah, this is a fine place to discuss Fay ideas/issues.

Probably writing a wrapper to one of the existing templates is an OK approach. I'd also like to try a blaze-like approach. Haven't got round to it yet.

bergmark commented 11 years ago

There seems to be interest to use Angular as well, but I don't know if anyone has started working on it yet: http://www.yesodweb.com/blog/2012/10/yesod-fay-js

bergmark commented 11 years ago

Something blaze-like would be nice, if we could use/write some reactive bindings for it as well that would be even better

chrisdone commented 11 years ago

@bergmark Definitely.

qrilka commented 11 years ago

Intermixing JS framework and Fay (as in Michael's post you refer to above) does not seem to be a good solution here as I'd like to define models in typesafe Fay and not in JS. And what about ReactiveMVC what it was written for?

chrisdone commented 11 years ago

@qrilka Maybe if you wrapped the JS framework with a type-safe interface it wouldn't be able to generate any non-type-safe scenarios.

The ReactiveMVC thing was just a base-level interpretation of backbone.js. It's the fundamental of FRP without being fully declarative. I simplified it a bit later. I'm sure there are much better things out there.

bergmark commented 11 years ago

@qrilka I'm guessing they want to wrap angular in yesod somehow, maybe with some julius thing for fay?

qrilka commented 11 years ago

I will try to create a wrapper prototype for Ember.js. BTW is it possible in Fay to create JS object with disired fields? I'm not sure how this could be done in typesafe manner. And without JS objects at the moment I don't see an easy way to imlement Ember.js wrapper

qrilka commented 11 years ago

@bergmark As far as I understand current Michael's plans is to improve yesod-fay package and example with Angular was done before and used only typesafe URLs for REST interface used by Angular

chrisdone commented 11 years ago

@qrilka

BTW is it possible in Fay to create JS object with disired fields? I'm not sure how this could be done in typesafe manner.

If you have a value data Foo = Foo {bar::Int,zot:String} then you can make an FFI function foo :: Foo -> … and Fay will serialize that value to {"_instance":"Foo","bar":…,"zot":…}. Or alternatively foo :: Automatic a -> …, which can automatically serialize data types.

qrilka commented 11 years ago

@chrisdone Cool, will try this out later

bergmark commented 11 years ago

And if the JS API is picky and doesn't like the instance (not _instance!) property you can do (untested!)

data Object = Object
dropInstance :: Automatic a -> Ptr Object
dropInstance = ffi "(function () { var o = %1; delete o.instance; return o; })()"
chrisdone commented 11 years ago

Sounds good.

bshepherdson commented 11 years ago

I'm planning to write some bindings for AngularJS in Fay. I'll certainly be striving for type safety, though I'm not sure how far it might get. The underlying Javascript is always untyped, but then so is x86 and ARM.

qrilka commented 11 years ago

It wiould be interesting to take a look at your solution so share some link if something will be available

bshepherdson commented 11 years ago

I draw your attention to the word "planning" :P I'm hoping to get a bunch done on that this weekend, but I'm also giving a talk on Sunday that needs sorting out between then and now. Since others are waiting to see how it goes, I'll leave the Yesod/Fay/AngularJS integration for later and just use my existing Node server, and try to rewrite my client-side Angular-powered Javascript with Fay. I can worry about porting the server later.

My general scheme is to define Fay functions to register Angular controllers, directives, and services using the FFI. Probably routes as well. The tricky part is Angular's dependency injection. What will that look like on the Fay side, and how will you access the scope variables in Fay? The solution is probably an Ng or Angular monad that captures all of these things and generates the appropriate Javascript on the other end.

I'm hoping that the injection can be mostly implicit: that you'll have functions that access $scope, or $http, and the monad will just observe that you're using them and add them to the injection list. I'll follow up here with a Github repo link once I have something to show.

MasseR commented 11 years ago

Chris, have you thought on how to do the blaze-like approach? One of the benefits for blaze-html is the use of monads and monoids to provide nice dsl. If I understood correctly, fay doesn't support typeclasses so monoids and do-syntax are out of the question.

chrisdone commented 11 years ago

Not yet. There is a way to do monads (see examples/ and tests/) but not proper monads. You can still just have an operator for combining EDSL thingies, though.

bergmark commented 9 years ago

Spring cleaning. This is too vague to belong on the tracker.

roper79 commented 8 years ago

FRP in Fay would be very nice -- something like https://github.com/Matt-Esch/virtual-dom