Skip to content

Instantly share code, notes, and snippets.

@Tosainu
Last active February 9, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tosainu/e113e45d63f2f36d22beb735dabcc2b1 to your computer and use it in GitHub Desktop.
Save Tosainu/e113e45d63f2f36d22beb735dabcc2b1 to your computer and use it in GitHub Desktop.
<!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>
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