我想使用 Purescript 的 Halogen 编写前端的特定组件。
例如,我想使用卤素创建一个注册表单。它看起来像下面这样:
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
能够将我的纯脚本代码构建到逻辑 javascript 文件中会很好。例如,src/LoginForm.purs
可以构建为site/loginForm.js
,并且src/RegistrationForm.purs
可以构建为site/registrationForm.js
。
然后,我可以根据需要在我的实际 html 页面中包含loginForm.js
和。registrationForm.js