Getting mouse events for a single element

Well, depending on what you want to do, you're sorta forced to hardcode that data but you can do it in an "extensible" way, so to speak.

Elm applications tend to look something like this

initialModel : Model
view : Model -> Element
input : Signal Input
step : Input -> Model -> Model 

main = map view (foldp step initialModel input)

So, in a sense, you have to figure out a way to work within these constraints. As you can see, the constraints allow for me to already write the main function even if I didn't tell you what this application does. (There are variations on this idea and you can see them here). The big gains of this system is that you can swap out the "view" for some other view. You want to suddenly render everything in 3D? sure, no problem. You won't have to change any code anywhere, just swap "view" for you awesome "view3D" function and you're good. Same goes for Svg or whatever.

This means that the view part only knows what's in the model. That's all it can play with. So whatever info you want, you kinda have to stick it in there.

This is where I spoke of a config field.

type alias Model = 
  { data : ...
  , config : { screenDimensions : (Int, Int), collageDimensions : (Int, Int), ... }

If this looks too ugly for you, you can put the "config" stuff in a seperate type:

type alias ViewConfig = 
   { screenDimensions : (Int, Int)
   , collageDimensions : (Int, Int)
   ... }

Since these options can change, we would need to get a Signal of these things.

viewConfig : Signal ViewConfig 

and then you have to simply modify your view function to accept the ViewConfig

view : ViewConfig -> Model -> Element

and then you have to slightly modify your main function as follows:

main = map2 view viewConfig (foldp step initialModel input)

and now you're good. You can do the conversion in the "view" (where it should be) and you didn't have to "hardcode", so to speak, your collage dimensions. You capture what you want in the "ViewConfig".

Oh, and remember, if you want some of the fields in the ViewConfig to never change, remember that there is the "constant" function which will give you a signal of things that'll never change.

/r/elm Thread