Last active
January 23, 2017 19:24
-
-
Save anowell/531dda3ffe1146ad6553cfa896481c41 to your computer and use it in GitHub Desktop.
Quasar To Do Concept (using maud templating)
This file contains hidden or 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
use quasar::*; | |
struct TodoItem { | |
label: String, | |
complete: bool, | |
} | |
struct TodoList { | |
items: Vec<TodoItem>, | |
} | |
impl Renderable for TodoList { | |
fn render(&self, _props: Properties) -> String { | |
(html! { | |
h3 { "To Do List (" (self.items.len()) " items)" } | |
ul id="todo-ul" { | |
@for item in self.items { | |
li class={ "todo-item " (item.complete) } { | |
input type="checkbox" | |
(item.label) | |
} | |
} | |
} | |
input id="message" type="text" | |
button { "Add" } | |
}).into_string() | |
} | |
} | |
pub fn init<B>(app: &QuasarApp) -> View<TodoList> { | |
let component = TodoList { | |
items: vec![ | |
TodoItem { label: "Example Task".to_string(), complete: false }, | |
] | |
}; | |
let view = app.bind("#todo-list", component); | |
view.query("button") | |
.on(EventType::Click, |mut evt| { | |
let message = evt.binding.query("#message").get("value"); | |
let item = TodoItem { label: message, complete: false }; | |
evt.binding.data_mut().items.push(item); | |
}); | |
view.on_each(EventType::Click, '.todo-item input[type=checkbox]', |i, mut evt| { | |
let state = evt.target.get("value") == "checked"; | |
let mut item_list = evt.binding.data_mut(); | |
item_list[i].complete = state; | |
}) | |
view | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment