A Dart library that helps working with beans and event streams.
To know something more about the released version have a look at the CHANGELOG.
1. Add the following dependency to your pubspec.yaml
and run pub get
:
dependencies:
dartbeans: any
2. Add dartbeans
to your code and run it:
import "package:dartbeans/dartbeans.dart";
void main() {
DartBean person = new DartBean();
person.onPropertyChangedEvents["name"]
.listen((PropertyChangedEvent event) =>
print("Changed: ${event.property} = ${event.newValue}"));
person["name"] = "Hans";
}
The common way to use DartBeans is by extending the DartBean
class and by adding extra code for accessing properties:
class Person extends DartBean {
static const NAME = "name";
static const AGE = "age";
String get name => this[NAME];
void set name(String name) {
this[NAME] = name;
}
int get age => this[AGE];
void set age(int age) {
this[AGE] = age;
}
}
Property changed event streams are exposed by the onPropertyChangedEvents
getter on the DartBean
class and can be accessed in a convenient way:
Stream<PropertyChangedEvent> get onNameChanged =>
onPropertyChangedEvents[NAME];
Stream<PropertyChangedEvent> get onAgeChanged =>
onPropertyChangedEvents[AGE];
Custom events can be easily implemented:
static const REFRESHED_EVENT_TYPE = "refreshed";
Stream<FLEvent> get onRefreshed => onEvents[REFRESHED_EVENT_TYPE];
void refresh() {
dispatch(REFRESHED_EVENT_TYPE); // dispatch an FLEvent
}
Then working with our beans is very simple:
void main() {
Person person = new Person();
// listen to changes on name property
person.onNameChanged.listen((PropertyChangedEvent event) {
print("1. Property change ${event.property} = ${event.newValue}"
+ " on ${event.target}");
});
// listen to changes on age property
person.onAgeChanged.listen((PropertyChangedEvent event) {
print("2. Property change ${event.property} = ${event.newValue}"
+ " on ${event.target}");
});
// listen to refreshes
person.onRefreshed.listen((FLEvent event) {
print("3. Refresh on ${event.target}");
});
person.name = "Hans";
person.age = 18;
person.refresh();
}
TODOC
TODOC
TODOC
###Event providers###
TODOC
###Discriminated events###
TODOC
###Event bubbling###
TODOC
Examples that show the use of DartBeans are in the example folder.
To run the tests just run the test/dartbeans_test.dart file.
You're welcome! Just visit the GitHub page and open a new issue or mail me.