简体   繁体   中英

Typeproviders, websharper, f#, builiding order

So I have added some data using typeproviders into my applications ( github link ) So here is the problem.

1) What I wanted is that my Site.fs use data and build div according to rules I gave it. Everything looks fine except....

I put back end calculation into WebSite project and probably because Web is startup project calculations in WebSite are never done and never pushed on database. It is only displayed data which is already in database.

Plus sometimes there is only ${title} and body instead of text provided in fsharp (even using default sitelet sample site). Is that normal? Restarting visual studio helps.

PS: sorry if my code doesn't look so spectacular I want it to work first and after that I will refactor it.

As you guessed, top-level values (such as your Site.items ) are computed once at the launch of the application. If you want it to be computed at every page load, it needs to be computed inside the function that receives a Context<Action> . [1]

For example, you can do something like the following:

let getItems() =
    seq {
        for i in borkData.parseData.db.ONEPOST do
            yield [i.Title; i.Link; i.Poslodavac; i.MjestoRada; i.RokZaPrijavu]
    }

let JobsPage =
    Skin.WithTemplate "Jobs" <| fun ctx -> // this is the function that gets called for every page
        let items = getItems()
        [
            for i in items ->
                Div [Class "job"] -<
                  [
                    A [Class "title"] -< [HRef i.[1]] -< [ Text i.[0]]
                    P [Class "posted top"]  -< [ Text i.[3] ]
                    P [Class "employer"]  -< [ Text i.[2] ]
                  ]
        ]

Here, since getItems is a function, it computes its return value every time you ask for it with let items = getItems() .


[1] You could also do it inside the function passed to Sitelet.Infer , if you used it. This would be useful if the Content you want to return depends on items , for example if you wanted to use a different template depending on items .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM