Reputation: 3034
In Knockout.js, the typical syntax for invoking an observable extender is as follows:
this.myObservable = ko.observable('foo').extend({myExtenderKey: myExtenderOption});
However, what if I have an extender that doesn't take a parameter? A simple logger, for instance, might not need any arguments. Nor would an auto-updating 'hasChanged' flag.
Most examples in the wild just pass an unused boolean:
this.myObservable = ko.observable('foo').extend({logsChanges : true});
This looks nice because a naive reader will guess that a logsChanges
behaviour is being activated. But the boolean is misleading, because the following will also work:
this.myObservable = ko.observable('foo').extend({logsChanges : false});
We could also pass a null value, but it seems a little redundant:
this.myObservable = ko.observable('foo').extend({logsChanges : null});
It also implies that a parameter might be sent to the logsChanges extender, even when it should never be.
Is there a way of calling an extender that doesn't include the extra parameter?
Upvotes: 3
Views: 923
Reputation: 159855
Looking at the source for extend
it does not appear to be the case right now. Baring a pull request, that's just how things work.
However, what you could do is update your extender to actually honor the boolean:
function logsChanges(target, shouldLog) {
if (!shouldLog) return target;
// Alternatively, you could return `false`
// but returning `target` is more explicit
return function loggingHandler() {
// Log changes and update here.
};
}
Upvotes: 1