Last active
September 7, 2017 17:58
-
-
Save anowell/00913a33a1b84473e0be71186dce690a to your computer and use it in GitHub Desktop.
Quasar codegen idea
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
// Current example without codegen | |
impl Component for TodoList { | |
fn onload(view: &View<Self>) { | |
view.query("button").expect("missing todo list button") | |
.on(EventType::Click, |mut evt| { | |
match evt.app.query("#message") { | |
Some(node) => { | |
let item = TodoItem::new(&node.get("value")); | |
evt.binding.data_mut().items.push(item); | |
} | |
None => println!("Query #message returned nothing.") | |
} | |
}); | |
view.on_each(EventType::Change, ".todo-item input", |mut evt| { | |
let state = evt.target.checked(); | |
let mut item_list = evt.binding.data_mut(); | |
item_list.items[evt.index].complete = state; | |
}); | |
} | |
} |
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
// Using codegen to get closer to this | |
// | |
// Basic idea is to let users specify what their handlers want | |
// and generate the code to call it with the data required | |
// | |
// Examples: | |
// &self -> evt.binding.data() | |
// &mut self -> evt.binding.data_mut() | |
// &item -> evt.binding.data().items[evt.index] | |
// &mut item -> evt.binding.data_mut().items[evt.index] | |
// Document -> evt.app | |
// | |
// Also it becomes possible to have different return type, e.g. () or Result<(), E> | |
// | |
// | |
impl TodoList { | |
#[on(event="click", el="button")] | |
fn add_item(&mut self, document: Document) -> Result<()> { | |
let message = document.query("#message") | |
.ok_or_else(|| bail!("#message element not found")?; | |
let item = TodoItem::new(message.get("value")); | |
self.items.push(item); | |
Ok(()) | |
} | |
#[on(event="change", el=".todo-item input")] | |
fn toggle_check(evt: Event, item: &mut TodoItem) { | |
item.complete = evt.target.checked(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment