SteveSanderson/knockout — pull request #778 by KenPowers (2012-12-27 22:24:33+00:00)
Title: Observable modifier functions

This is a small change which can be demonstrated by some code from the first Knockout tutorial.

Previously, the tutorial code did the following:

this.capitalizeLastName = function () {
var currentVal = this.lastName();
this.lastName(currentVal.toUpperCase());
};

With this change the code can become the following:

this.capitalizeLastName = function () {
this.lastName(function (old) {
return old.toUpperCase();
});
};

Not a huge difference, but if you ask me it is cleaner. I have also included a test which passes and all other tests still pass as well.

I could further extend this to include asynchronous updating of observables. For example, consider the following:

this.capitalizeLastName = function () {
this.lastName(function (old, done) {
setTimeout(function () {
done(old.toUpperCase());
}, 5000);
});
};

This is easily accomplished by checking the `function`'s length property and changing around a few things in ko.observable. I can implement this if desired.