mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 14:12:13 +00:00
Update todomvc example to the 2018 edition
This commit is contained in:
parent
a8fb4c3bf8
commit
2c30818b7c
@ -2,6 +2,7 @@
|
|||||||
name = "todomvc"
|
name = "todomvc"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["The wasm-bindgen Developers"]
|
authors = ["The wasm-bindgen Developers"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib"]
|
crate-type = ["cdylib"]
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
extern crate wasm_bindgen;
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use web_sys::EventTarget;
|
use web_sys::EventTarget;
|
||||||
@ -62,7 +61,7 @@ impl Element {
|
|||||||
where
|
where
|
||||||
T: 'static + FnMut(web_sys::Event),
|
T: 'static + FnMut(web_sys::Event),
|
||||||
{
|
{
|
||||||
let cb = Closure::wrap(Box::new(handler) as Box<FnMut(_)>);
|
let cb = Closure::wrap(Box::new(handler) as Box<dyn FnMut(_)>);
|
||||||
if let Some(el) = self.el.take() {
|
if let Some(el) = self.el.take() {
|
||||||
let el_et: EventTarget = el.into();
|
let el_et: EventTarget = el.into();
|
||||||
el_et
|
el_et
|
||||||
@ -117,7 +116,7 @@ impl Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}) as Box<FnMut(_)>);
|
}) as Box<dyn FnMut(_)>);
|
||||||
|
|
||||||
dyn_el
|
dyn_el
|
||||||
.add_event_listener_with_callback_and_bool(
|
.add_event_listener_with_callback_and_bool(
|
||||||
|
@ -2,16 +2,10 @@
|
|||||||
//!
|
//!
|
||||||
//! A [TODO MVC](https://todomvc.com/) implementation written using [web-sys](https://rustwasm.github.io/wasm-bindgen/web-sys/overview.html)
|
//! A [TODO MVC](https://todomvc.com/) implementation written using [web-sys](https://rustwasm.github.io/wasm-bindgen/web-sys/overview.html)
|
||||||
|
|
||||||
extern crate wasm_bindgen;
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
extern crate js_sys;
|
|
||||||
extern crate web_sys;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
extern crate askama;
|
|
||||||
extern crate console_error_panic_hook;
|
|
||||||
|
|
||||||
/// Controller of the program
|
/// Controller of the program
|
||||||
pub mod controller;
|
pub mod controller;
|
||||||
/// Element wrapper to the DOM
|
/// Element wrapper to the DOM
|
||||||
|
@ -43,7 +43,7 @@ impl Store {
|
|||||||
let title = item_array.shift().as_string()?;
|
let title = item_array.shift().as_string()?;
|
||||||
let completed = item_array.shift().as_bool()?;
|
let completed = item_array.shift().as_bool()?;
|
||||||
let id = item_array.shift().as_string()?;
|
let id = item_array.shift().as_string()?;
|
||||||
let mut temp_item = Item {
|
let temp_item = Item {
|
||||||
title,
|
title,
|
||||||
completed,
|
completed,
|
||||||
id,
|
id,
|
||||||
@ -59,7 +59,7 @@ impl Store {
|
|||||||
fn sync_local_storage(&mut self) {
|
fn sync_local_storage(&mut self) {
|
||||||
let array = js_sys::Array::new();
|
let array = js_sys::Array::new();
|
||||||
for item in self.data.iter() {
|
for item in self.data.iter() {
|
||||||
let mut child = js_sys::Array::new();
|
let child = js_sys::Array::new();
|
||||||
let s = item.title.clone();
|
let s = item.title.clone();
|
||||||
child.push(&JsValue::from(&s));
|
child.push(&JsValue::from(&s));
|
||||||
child.push(&JsValue::from(item.completed));
|
child.push(&JsValue::from(item.completed));
|
||||||
@ -82,7 +82,7 @@ impl Store {
|
|||||||
/// let data = db.find(ItemQuery::Completed {completed: true});
|
/// let data = db.find(ItemQuery::Completed {completed: true});
|
||||||
/// // data will contain items whose completed properties are true
|
/// // data will contain items whose completed properties are true
|
||||||
/// ```
|
/// ```
|
||||||
pub fn find(&mut self, query: ItemQuery) -> Option<ItemListSlice> {
|
pub fn find(&mut self, query: ItemQuery) -> Option<ItemListSlice<'_>> {
|
||||||
Some(
|
Some(
|
||||||
self.data
|
self.data
|
||||||
.iter()
|
.iter()
|
||||||
@ -160,7 +160,7 @@ pub trait ItemListTrait<T> {
|
|||||||
fn get(&self, i: usize) -> Option<&T>;
|
fn get(&self, i: usize) -> Option<&T>;
|
||||||
fn length(&self) -> usize;
|
fn length(&self) -> usize;
|
||||||
fn push(&mut self, item: T);
|
fn push(&mut self, item: T);
|
||||||
fn iter(&self) -> std::slice::Iter<T>;
|
fn iter(&self) -> std::slice::Iter<'_, T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ItemList {
|
pub struct ItemList {
|
||||||
@ -173,7 +173,7 @@ impl ItemList {
|
|||||||
{
|
{
|
||||||
self.list.retain(f);
|
self.list.retain(f);
|
||||||
}
|
}
|
||||||
fn iter_mut(&mut self) -> std::slice::IterMut<Item> {
|
fn iter_mut(&mut self) -> std::slice::IterMut<'_, Item> {
|
||||||
self.list.iter_mut()
|
self.list.iter_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ impl ItemListTrait<Item> for ItemList {
|
|||||||
fn push(&mut self, item: Item) {
|
fn push(&mut self, item: Item) {
|
||||||
self.list.push(item)
|
self.list.push(item)
|
||||||
}
|
}
|
||||||
fn iter(&self) -> std::slice::Iter<Item> {
|
fn iter(&self) -> std::slice::Iter<'_, Item> {
|
||||||
self.list.iter()
|
self.list.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,7 +224,7 @@ impl<'a> ItemListTrait<&'a Item> for ItemListSlice<'a> {
|
|||||||
fn push(&mut self, item: &'a Item) {
|
fn push(&mut self, item: &'a Item) {
|
||||||
self.list.push(item)
|
self.list.push(item)
|
||||||
}
|
}
|
||||||
fn iter(&self) -> std::slice::Iter<&'a Item> {
|
fn iter(&self) -> std::slice::Iter<'_, &'a Item> {
|
||||||
self.list.iter()
|
self.list.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use askama::Template as AskamaTemplate;
|
use askama::Template as AskamaTemplate;
|
||||||
use store::{ItemList, ItemListTrait};
|
use crate::store::{ItemList, ItemListTrait};
|
||||||
|
|
||||||
#[derive(AskamaTemplate)]
|
#[derive(AskamaTemplate)]
|
||||||
#[template(path = "row.html")]
|
#[template(path = "row.html")]
|
||||||
|
@ -12,7 +12,6 @@ use crate::template::Template;
|
|||||||
const ENTER_KEY: u32 = 13;
|
const ENTER_KEY: u32 = 13;
|
||||||
const ESCAPE_KEY: u32 = 27;
|
const ESCAPE_KEY: u32 = 27;
|
||||||
|
|
||||||
extern crate wasm_bindgen;
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
/// Messages that represent the methods to be called on the View
|
/// Messages that represent the methods to be called on the View
|
||||||
@ -53,7 +52,7 @@ pub struct View {
|
|||||||
main: Element,
|
main: Element,
|
||||||
toggle_all: Element,
|
toggle_all: Element,
|
||||||
new_todo: Element,
|
new_todo: Element,
|
||||||
callbacks: Vec<(web_sys::EventTarget, String, Closure<FnMut()>)>,
|
callbacks: Vec<(web_sys::EventTarget, String, Closure<dyn FnMut()>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View {
|
impl View {
|
||||||
@ -95,7 +94,7 @@ impl View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}) as Box<FnMut()>);
|
}) as Box<dyn FnMut()>);
|
||||||
|
|
||||||
let window_et: web_sys::EventTarget = window.into();
|
let window_et: web_sys::EventTarget = window.into();
|
||||||
window_et
|
window_et
|
||||||
@ -422,7 +421,7 @@ impl View {
|
|||||||
impl Drop for View {
|
impl Drop for View {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
exit("calling drop on view");
|
exit("calling drop on view");
|
||||||
let callbacks: Vec<(web_sys::EventTarget, String, Closure<FnMut()>)> =
|
let callbacks: Vec<(web_sys::EventTarget, String, Closure<dyn FnMut()>)> =
|
||||||
self.callbacks.drain(..).collect();
|
self.callbacks.drain(..).collect();
|
||||||
for callback in callbacks {
|
for callback in callbacks {
|
||||||
callback
|
callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user