Expand description
Data context service and properties.
The data property can be set on a widget to any type that can be used in variables (VarValue). The
DATA service can then be used on the widget or descendant widgets to retrieve the data and to set validation annotations
about the data.
The example below demonstrates a simple MVVM implementation using the data context to share the view-model instance with all widgets in the view. The example also uses the data annotations API to show data validation errors.
mod view {
use crate::view_model::*;
use zng::{data_context, prelude::*, window::WindowRoot};
pub fn window() -> WindowRoot {
Window! {
// set data context for entire window, using `var` to be read-write.
data = var(ViewModel::new(crate::model::connect()));
// bind title from data context.
title = DATA.req::<ViewModel>().map(|vm| vm.title());
child = content();
}
}
fn content() -> UiNode {
// `req` panics if context is not set to the same type.
let vm = DATA.req::<ViewModel>();
Container! {
child = TextInput! {
txt = vm.map_bidi_modify(|vm| vm.new_item(), |v, vm| vm.set_new_item(v.clone()));
// FieldStyle shows data errors.
style_fn = style_fn!(|_| zng::text_input::FieldStyle!());
data_context::data_error = vm.map(|vm| vm.new_item_error());
};
child_bottom = Button! {
child = Text!("Submit");
widget::enabled = vm.map(|vm| !vm.new_item().is_empty());
on_click = hn!(|_| vm.modify(|vm| vm.submit()));
};
child_spacing = 5;
padding = 5;
}
}
}
mod view_model {
use crate::model::Model;
use zng::text::*;
#[derive(Clone, Debug, PartialEq)]
pub struct ViewModel {
model: Model,
new_item: Txt,
new_item_error: Txt,
}
impl ViewModel {
pub fn new(model: Model) -> Self {
Self {
model,
new_item: Txt::from(""),
new_item_error: Txt::from(""),
}
}
pub fn title(&self) -> Txt {
formatx!("App - {} entries", self.model.read().len())
}
pub fn new_item(&self) -> Txt {
self.new_item.clone()
}
pub fn set_new_item(&mut self, new_item: Txt) {
self.new_item_error = "".into();
self.new_item = new_item;
}
pub fn new_item_error(&self) -> Txt {
self.new_item_error.clone()
}
pub fn submit(&mut self) {
match self.new_item.parse::<u32>() {
Ok(item) => {
self.model.write().push(item);
self.set_new_item(Txt::from(""));
}
Err(e) => self.new_item_error = e.to_txt(),
}
}
}
}
mod model {
use zng::{task::parking_lot::RwLock, var::ArcEq};
pub type Model = ArcEq<RwLock<Vec<u32>>>;
pub fn connect() -> ArcEq<RwLock<Vec<u32>>> {
ArcEq::new(RwLock::new(vec![]))
}
}§Full API
See zng_wgt_data for the full API.
Structs§
- DATA
- Data context and validation.
- Data
Note - Represents an annotation set in a data context.
- Data
Note Handle - Handle for a
DataNotein a context. - Data
Note Level - Classifies the kind of information conveyed by a
DataNote. - Data
Notes - Represents the data notes set in a context.
Traits§
- Data
Note Value - Represents a
DataNotevalue.
Functions§
- data
PData context.- data_
error PInsert a dataERRORnote in the context.- data_
error_ color PSet the data noteERRORcolor.- data_
info PInsert a dataINFOnote in the context.- data_
info_ color PSet the data noteINFOcolor.- data_
note PInsert a data note in the context.- data_
warn PInsert a dataWARNnote in the context.- data_
warn_ color PSet the data noteWARNcolor.- extend_
data_ note_ colors PExtend the data note level colors, thecolorsextend the parent colors, only entries of the same level are replaced.- get_
data_ error PGet allERRORdata notes set on the context.- get_
data_ error_ txt PWrite allERRORdata notes set on the context to a text.- get_
data_ info PGet allINFOdata notes set on the context.- get_
data_ info_ txt PWrite allINFOdata notes set on the context to a text.- get_
data_ notes PGet all data notes set on the context.- get_
data_ notes_ top PGets all the notes of highest data level set on the context.- get_
data_ warn PGet allWARNdata notes set on the context.- get_
data_ warn_ txt PWrite allWARNdata notes set on the context to a text.- has_
data_ error PGets if anyERRORdata notes are set on the context.- has_
data_ info PGets if anyINFOdata notes are set on the context.- has_
data_ notes PGets if any data notes are set on the context.- has_
data_ warn PGets if anyWARNdata notes are set on the context.- replace_
data_ note_ colors PSets the data note level colors, the parent colors are fully replaced.- with_
data_ note_ color - Node that inserts a data note color in
DATA_NOTE_COLORS_VAR.