Is there a way to use the default JavaScript attribute?

I just want to say somehow "I want all the methods in this project to use [JavaScript]" The manual annotation of each method is annoying

+5
source share
3 answers

F # 3 allows you to mark a module with the ReflectedDefinition attribute (also known as [JavaScript] in WebSharper), which marks all the methods under it.

See more about F # 3.0 language features :

(Speaking of unusual attributes, in F # 3.0, [& L; ReflectedDefinition>] can now be placed on modules and types, as a shorthand way to apply it to each person is a member of the module / type.)

+6
source

, - - , , WebSharper.

F # , - ( ) , , . , , reflect check.fs ( GitHub).

F # , (. ), , , F #: -)

+3

If you comment all your code using the JavaScript attribute, the WebSharper compiler will try to translate everything into JavaScript. The rule of thumb in developing WebSharper is to separate the code on the server side and on the client side, so you can simply annotate the module / class containing the code on the client side and not every function / member if you are targeting .NET 4.5.

namespace Website

open IntelliFactory.WebSharper

module HelloWorld =

    module private Server =

        [<Rpc>]
        let main() = async { return "World" }

    [<JavaScript>] // or [<ReflectedDefinition>]
    module Client =

        open IntelliFactory.WebSharper.Html

        let sayHello() =
            async {
                let! world = Server.main()
                JavaScript.Alert <| "Hello " + world
            }

        let btn =
            Button [Text "Click Me"]
            |>! OnClick (fun _ _ ->
                async {
                    do! sayHello()
                } |> Async.Start)

        let main() = Div [btn]

    type Control() =

        inherit Web.Control()

        [<JavaScript>]
        override __.Body = Client.main() :> _
+2
source

All Articles