Skip to content

Instantly share code, notes, and snippets.

@rolaveric
Last active September 8, 2015 10:52
Show Gist options
  • Save rolaveric/4cbc38a8e9d057444751 to your computer and use it in GitHub Desktop.
Save rolaveric/4cbc38a8e9d057444751 to your computer and use it in GitHub Desktop.
getterSetter generator function used in Falcor article
class MyController {
// ... snip
getterSetter(path) {
// Need to return an unbound function
var ctrl = this;
return function (newValue) {
// If arguments.length === 1 then the function is used as a setter,
// otherwise it's a getter
return arguments.length
// setValue() returns an Observable.
// So we call $scope.$evalAsync() when we subscribe() to it,
// which keeps us in sync with the digest loop
? ctrl.model.setValue(path, newValue).subscribe(_ => ctrl.$scope.$evalAsync())
// Here we'll reuse viewValue()
: ctrl.viewValue(path);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment