Can one use extenders with multiple parameters in knockout.js
Example:
ko.extenders.currency = function(target, currencySymbol, position) {
var result = ko.computed({
read: target,
write: function(newValue) {
var current = target(),
if (position == 'left') {
target(currencySymbol+target);
} else {
target(target+currencySymbol);
}
}
}).extend({ notify: 'always' });
but then how would I bind it to observable?
this.One = ko.observable(one).extend({ currency: ???, currencySymbol: '£', position : 'left'});
You would need to use an object for that, for example:
ko.extenders.currency = function(target, options) {
var currencySymbol = options.symbol,
position = options.position;
var result = ko.computed({
read: target,
write: function(newValue) {
var current = target(),
if (position == 'left') {
target(currencySymbol+target);
} else {
target(target+currencySymbol);
}
}
}).extend({ notify: 'always' });
And then, you'll call it like this:
this.One = ko.observable(one).extend({ currency: { symbol: '£', position : 'left' } });
Or if you prefer a more readable code:
var currencyOptions = { symbol: '£', position: 'left' };
this.One = ko.observable(one).extend({ currency: currencyOptions });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With