Last active
September 8, 2015 10:52
-
-
Save rolaveric/4cbc38a8e9d057444751 to your computer and use it in GitHub Desktop.
getterSetter generator function used in Falcor article
This file contains 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
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