Last active
February 9, 2018 13:56
-
-
Save Tosainu/e113e45d63f2f36d22beb735dabcc2b1 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width" /> | |
<title>にゃーん</title> | |
</head> | |
<body> | |
<p id="message-box"></p> | |
<button id="button-inc">inc</button> <button id="button-dec">dec</button> | |
<script src="js/app.js"></script> | |
</body> | |
</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
extern crate stdweb; | |
use std::cell::RefCell; | |
use std::rc::Rc; | |
use stdweb::web::{document, IEventTarget, INode}; | |
use stdweb::web::event::ClickEvent; | |
struct State { | |
count: i32, | |
} | |
fn update_dom(state: &Rc<RefCell<State>>) { | |
let elem = document().query_selector("#message-box").unwrap(); | |
elem.set_text_content(&format!("count: {}", state.borrow().count)); | |
} | |
fn main() { | |
stdweb::initialize(); | |
let state = Rc::new(RefCell::new(State { count: 0 })); | |
{ | |
let state = state.clone(); | |
let button = document().query_selector("#button-inc").unwrap(); | |
button.add_event_listener(move |_: ClickEvent| { | |
state.borrow_mut().count += 1; | |
update_dom(&state); | |
}); | |
} | |
{ | |
let state = state.clone(); | |
let button = document().query_selector("#button-dec").unwrap(); | |
button.add_event_listener(move |_: ClickEvent| { | |
state.borrow_mut().count -= 1; | |
update_dom(&state); | |
}); | |
} | |
update_dom(&state); | |
stdweb::event_loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment