Skip to content

Instantly share code, notes, and snippets.

@danschultz
Last active December 18, 2015 17:19
Show Gist options
  • Save danschultz/5817989 to your computer and use it in GitHub Desktop.
Save danschultz/5817989 to your computer and use it in GitHub Desktop.
Proposal for computed properties in Dart. Whenever `firstName` or `lastName` changes, I want `fullName` to also dispatch a change event. This'll be very useful for binding a read-only property in WebUI. Right now, I have to write some ugly boilerplate to get this to work. Ideally there'd be a way to set this up through metadata.
// Assumes that we're using mdv_observe
// Verbose way
class Person extends ObservableBase {
@observable
String firstName;
@observable
String lastName;
String get fullName => '${firstName} ${lastName}';
void notifyPropertyChange(Symbol field, Object oldValue, Object newValue) {
super.notifyPropertyChange(field, oldValue, newValue);
if (field == new Symbol('lastName') || field == new Symbol('firstName')) {
notifyPropertyChange(new Symbol('fullName'), null, fullName);
}
}
}
// Metadata approach
// @computed is used to annotate [fullName] to dispatch a change event whenever [firstName] or [lastName] changes.
class Person extends ObservableBase {
@observable
String firstName;
@observable
String lastName;
@computed ['firstName', 'lastName']
String get fullName => '${firstName} ${lastName}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment