Requesting server when page load

I have an endpoint for school -> “/school”. This is a page for add school. This page has a model and this model contain cities vec. But Model default is empty. When page load, i should request “/city” and get all cities to model.cities.

But i could not done it. I thought, “load cities when click city select box” and yes, it loaded but it requested every time i clicked.

At the end, i have two choices. 1- Load cities when page load. 2- Load cities when add school form load.

lib-rs->https://nest.pijul.com/eribol/libredu:master/c9af45ac8dd93fd53f01

school-rs -> https://nest.pijul.com/eribol/libredu:master/7229082aeac6f4a81401

I would send a request to /city in init function in school.rs.
Your updated init would look like this:

pub fn init(orders: &impl Orders<Msg>) -> Model {
    orders.perform_cmd(.. your request ...);
    Model::default()
}

I tried this before but it did not work. I am missing something but i dont know what.

lib.rs-> http://dpaste.com/2721JZ5
school.rs-> http://dpaste.com/1RYJ7DN

–> client/src/lib.rs:105:57
|
105 | Self::School(page::school::init(&mut page::school::Msg::Load))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait seed::app::orders::Orders<page::school::Msg> is not implemented for page::school::Msg

I made changes.
lib-rs-> http://dpaste.com/1D1FMR2

–> client/src/lib.rs:99:75
|
99 | Self::School(page::schol::init(&mut orders.proxy(page::schol::Msg::Load)))
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait std::clone::Clone is not implemented for page::school::Msg

When add Clone impl for school::Msg, it complain again for references etc.

orders.proxy expects parent’s Msg constructor - it works the same like orders.proxy in your update function. You have already almost working code in your lib.rs:

//page::school::update(msg, &mut model, &mut orders.proxy(Msg::School), ctx)

=> replace page::schol::Msg::Load with Msg::School

Finaly, it worked. Thank you so much.

lib.rs-> https://nest.pijul.com/eribol/libredu:master/c9af45ac8dd93fd53f01
school.rs -> https://nest.pijul.com/eribol/libredu:master/7229082aeac6f4a81401

OT, just code notes / general tips:
1. school.rs => fn add => I would recommend to move short if arms at the top and use early returns / “guards”. E.g.:

fn add(model: &Model, ctx: &Context)-> Node<Msg>{
   if xxx { return div!["Giriş yapınız"] }
   if xxx { return div!["Kayıtlı kurumunuz mevcut"] }
   div![ ... ]
}

2. Switch page and ctx in Model in init:
From

    Model {
        page: Page::init(url.clone(), orders),
        ctx: Context {
            base_url: url.to_base_url(),
...

to

    Model {
        ctx: Context {
            base_url: url.to_base_url(),
            ....
        page: Page::init(url, orders),

Notice removed .clone() at the url.

Yes, i mixed a lot of things for this problem and its very bad :slight_smile: I will change them soon, thank you again.