Skip to content

Commit

Permalink
Make DOMObject a little more type safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
valderman committed Apr 15, 2012
1 parent 7cbcecb commit eddfa3b
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Haste/Reactive/DOM.hs
@@ -1,5 +1,4 @@
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# LANGUAGE GADTs, FlexibleInstances, MultiParamTypeClasses #-}
-- | DOM events and utilities for the Haste reactive library.
module Haste.Reactive.DOM (valueOf, valueAt, DOMObject, domObj) where
import FRP.Fursuit
Expand All @@ -8,22 +7,20 @@ import Data.String

-- | Represents a DOM object; a DOM object consists of an object ID,
-- corresponding to the object's ID attribute in the HTML, and an attribute.
data DOMObject = D {
d_object :: String,
d_attr :: String
}
data DOMObject a where
D :: (Showable a, Readable a) => ElemID -> PropID -> DOMObject a

-- | Create a 'DOMObject' from a string describing the object. For example,
-- domObj "myobject.value" corresponds to the value attribute of the object
-- with the ID "myobject".
domObj :: String -> DOMObject
domObj :: (Showable a, Readable a) => String -> DOMObject a
domObj str =
case span (/= '.') str of
([], _) -> error "domObj: No object ID given!"
(_, []) -> error "domObj: No object attribute given!"
(obj, attr) -> D obj (tail attr)

instance IsString DOMObject where
instance (Showable a, Readable a) => IsString (DOMObject a) where
fromString = domObj

-- | The value property of the given element, updated whenever an onchange
Expand All @@ -49,5 +46,5 @@ valueAt e evt = do
then error $ "Not found: " ++ e
else return sig

instance Showable a => Sink DOMObject a where
instance Sink (DOMObject a) a where
(D obj attr) << val = sink (setProp obj attr . toStr) val

0 comments on commit eddfa3b

Please sign in to comment.