Module

Elmish.Foreign

#CanPassToJavaScript

class CanPassToJavaScript (a :: Type) 

This class is used to assert that values of a type can be passed to JavaScript code directly (without conversion) and understood by that code. Specifically, this class is defined for primitives (strings, numbers, booleans), arrays, and records. This assertion is used in a number of places that pass complex values to JS code to restrict the set of types that can be safely passed.

It is still possible to define instances of this class for other, non-primitive types, but you have to know what you're doing and make sure that JS representation is sane and stable. For example, a common trick is to newtype-wrap known JS enumerations to provide type safety:

module HTMLButton
   ( ButtonType  -- NOTE: not exporting the constructor
   , typeButton, typeSubmit, typeReset
   , ButtonProps, button
   )
   where

newtype ButtonType = ButtonType String
instance toJsButtonType :: CanPassToJavaScript ButtonType
typeButton = ButtonType "button" :: ButtonType
typeSubmit = ButtonType "submit" :: ButtonType
typeReset = ButtonType "reset" :: ButtonType

type ButtonProps =
  { type :: ButtonType
  , ...
  }

foreign import button :: ButtonProps -> ReactElement

Instances

#readForeign

readForeign :: forall a. CanReceiveFromJavaScript a => Foreign -> Maybe a

Verifies if the given raw JS value is of the right type/shape to be represented as a, and if so, coerces the value to a.

#showForeign

#Arguments

#argumentsToArray_

argumentsToArray_ :: Arguments -> Array Foreign

Creates a new Array from an Arguments object.

#getArgument

getArgument :: Arguments -> Int -> Maybe Foreign

Gets the value at a specified index of an Arguments object. Returns Nothing if there are not enough arguments.

#mkVarArgEff_

mkVarArgEff_ :: (Arguments -> Effect Unit) -> Foreign

Creates a JS function that takes a variable number of args (via arguments) and calls the provided effectful continuation, passing the arguments as an array.

#CanReceiveFromJavaScriptRecord

class CanReceiveFromJavaScriptRecord rowList  where

This class is implementation of isForeignOfCorrectType for records. It validates a given JS hash (aka "object") against a given type row that represents a PureScript record, recursively calling isForeignOfCorrectType for each field.

Members

Instances

#CanPassToJavaScriptRecord

class CanPassToJavaScriptRecord (rowList :: RowList Type) 

This class is implementation of CanPassToJavaScript for records. It simply iterates over all fields, checking that every field is of a type that also has an instance of CanPassToJavaScript.

Instances

Re-exports from Foreign

#Foreign

data Foreign :: Type

A type for foreign data.

Foreign data is data from any external unknown or unreliable source, for which it cannot be guaranteed that the runtime representation conforms to that of any particular type.

Suitable applications of Foreign are

  • To represent responses from web services
  • To integrate with external JavaScript libraries.

Modules