-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 63868b5
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/target | ||
Cargo.lock | ||
|
||
http://www.sheshbabu.com/posts/rust-wasm-yew-single-page-application/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "rustSPA" | ||
version = "0.1.0" | ||
authors = ["iwanbazz <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib","rlib"] | ||
|
||
[dependencies] | ||
yew = "0.17" | ||
wasm-bindgen = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[tasks.build] | ||
command = "wasm-pack" | ||
args = ["build", "--dev", "--target", "web", "--out-name", "wasm", "--out-dir", "./static"] | ||
watch = {ignore_pattern = "static/*"} | ||
|
||
[tasks.serve] | ||
command = "simple-http-server" | ||
args = ["-i", "./static/", "-p", "3000", "--nocache", "--try-file", "./static/index.html"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Web assembly on top of Rust. | ||
|
||
Single Page Applications using Rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
mod pages; | ||
|
||
use pages::Home; | ||
use wasm_bindgen::prelude::*; | ||
use yew::prelude::*; | ||
|
||
#[wasm_bindgen(start)] | ||
pub fn run_app() { | ||
App::<Home>::new().mount_to_body(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use yew::prelude::*; | ||
|
||
struct Product { | ||
id: i32, | ||
name: String, | ||
description: String, | ||
image: String, | ||
price: f64, | ||
} | ||
|
||
struct State { | ||
products: Vec<Product>, | ||
} | ||
|
||
pub struct Home { | ||
state: State, | ||
} | ||
|
||
impl Component for Home { | ||
type Message = (); | ||
type Properties = (); | ||
|
||
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self { | ||
let products: Vec<Product> = vec![ | ||
Product { | ||
id: 1, | ||
name: "Apple".to_string(), | ||
description: "An apple a day keeps the doctor away".to_string(), | ||
image: "/products/apple.png".to_string(), | ||
price: 3.65, | ||
}, | ||
Product { | ||
id: 2, | ||
name: "Banana".to_string(), | ||
description: "An old banana leaf was once young and green".to_string(), | ||
image: "/products/banana.png".to_string(), | ||
price: 7.99, | ||
}, | ||
]; | ||
Self { | ||
state: State { | ||
products, | ||
}, | ||
} | ||
} | ||
|
||
fn update(&mut self, _: Self::Message) -> ShouldRender { | ||
true | ||
} | ||
|
||
fn change(&mut self, _: Self::Properties) -> ShouldRender { | ||
true | ||
} | ||
|
||
fn view(&self) -> Html { | ||
let products: Vec<Html> = self | ||
.state | ||
.products | ||
.iter() | ||
.map(|product: &Product| { | ||
html! { | ||
<div> | ||
<img src={&product.image}/> | ||
<div>{&product.name}</div> | ||
<div>{"$"}{&product.price}</div> | ||
</div> | ||
} | ||
}) | ||
.collect(); | ||
html! { <span>{products}</span> } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod home; | ||
|
||
pub use home::Home; |