我想用Purescript的Halogen编写我前端的特定组件。如何组件化purescript卤素应用
例如,我想创建一个使用卤素的注册表格。它看起来像下面:
module RegistrationForm where
import Prelude
...
-- | The state of the application
newtype State = State { email :: String, password :: String }
derive instance genericState :: Generic State
instance showState :: Show State where show = gShow
instance eqState :: Eq State where eq = gEq
initialState :: State
initialState = State { email: "", password: "" }
-- | Inputs to the state machine
data Input a = FormSubmit a
| UpdateEmail String a
| UpdatePassword String a
| NoAction a
type AppEffects eff = HalogenEffects (ajax :: AJAX, console :: CONSOLE | eff)
ui :: forall eff . Component State Input (Aff (AppEffects eff))
ui = component render eval
where
render :: Render State Input
render (State state) = H.div_
[ H.h1_ [ H.text "Register" ]
, ...
]
eval :: Eval Input State Input (Aff (AppEffects eff))
eval (NoAction next) = pure next
eval (FormSubmit next) = do
...
eval (UpdateEmail email next) = do
...
eval (UpdatePassword password next) = do
...
runRegistrationForm :: Eff (AppEffects()) Unit
runRegistrationForm = runAff throwException (const (pure unit)) $ do
{ node: node, driver: driver } <- runUI ui initialState
appendTo ".registration" node
同样,我有一个LoginForm
模块,处理用户登录到应用程序。
我想知道如何去组织我的源代码,构建我的源代码,并从Javascript调用我的Purescript代码?
目前,我的源代码被组织如下所示:
$ cd my-application/
$ tree
.
├── bower.json
├── README.md
├── site/
│ └── index.html
├── src/
│ ├── LoginForm.purs
│ └── RegistrationForm.purs
└── test/
└── Test.purs
然而,由于我没有做Main.purs
,我不能做以下打造我的源代码中的任何:
$ pulp build --to site/app.js
$ pulp browserify --to site/app.js
$ pulp server
这将是很好能够建立我的purescript代码到逻辑JavaScript文件。例如,src/LoginForm.purs
可以构建为site/loginForm.js
,并且src/RegistrationForm.purs
可以构建为site/registrationForm.js
。
然后,我可以包括loginForm.js
和registrationForm.js
在我的实际HTML页面中的需要。
这个解决方案的问题是需要你运行'pulp'两次。我需要额外的'gulpfile'或'Makefile'来指定'pulp'的所有不同的调用。我不妨使用'gulp'。 – illabout
这里的常规方法是将其放在package.json中的“脚本”中。这个解决方案确实具有两条线而不是约40条的明显优点。 – hdgarrood
我对Javascript生态系统不是很熟悉。你可以编辑你的答案并添加一个示例'package.json'来显示它的样子吗?或者甚至只是添加一个链接到描述它的页面? – illabout