gpt4 book ai didi

javascript - 具有多个参数的 Knockout JS 中的扩展器

转载 作者:数据小太阳 更新时间:2023-10-29 04:31:02 28 4
gpt4 key购买 nike

一个可以使用extenders在 knockout.js 中有多个参数

示例:

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' });

但是我如何将它绑定(bind)到 observable?

this.One = ko.observable(one).extend({ currency: ???, currencySymbol: '£', position : 'left'});

最佳答案

你需要为此使用一个对象,例如:

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' });

然后,您可以这样调用它:

this.One = ko.observable(one).extend({ currency: { symbol: '£', position : 'left' } });

或者如果您更喜欢可读性更强的代码:

var currencyOptions = { symbol: '£', position: 'left' };
this.One = ko.observable(one).extend({ currency: currencyOptions });

关于javascript - 具有多个参数的 Knockout JS 中的扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23931637/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com