简体   繁体   English

绑定处理程序“值不是函数”

[英]Binding handler 'value is not a function' knockoutJS

I am stumped as to what is causing the problem here as this binding handler normally works fine (below) 我对此问题感到困惑,因为此绑定处理程序通常可以正常工作(如下)

ko.bindingHandlers.buttonGroupChecked = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    var value = valueAccessor();
    var newValueAccessor = function() {
        return {
            click: function() {
                value(allBindingsAccessor.get('val'));
            }
        };
    };
    ko.bindingHandlers.event.init(element, newValueAccessor,
            allBindingsAccessor, viewModel, bindingContext);
},
update: function(element, valueAccessor, allBindingsAccessor,
        viewModel, bindingContext) {
    if (allBindingsAccessor.get("val") === ko.unwrap(valueAccessor())) {
        helpers.activeClassSingle(element, "btn-info", "btn-success");
    }


}
};

I keep getting an error at line 7 value(allBindingsAccessor.get('val')); 我在第7行value(allBindingsAccessor.get('val'));不断收到错误value(allBindingsAccessor.get('val')); saying value is not a function? 说值不是函数吗?

The options are defined in my viewmodel as follows:- 这些选项在我的视图模型中定义如下:

self.yesNoOptions = [
    {val: 1, text: "Yes"},
    {val: 0, text: "No"}
];

And the corresponding HTML and binding is:- 相应的HTML和绑定是:-

<div class="btn-group btn-group-justified" data-bind="foreach: $root.yesNoOptions">
               <div class="btn btn-lg btn-info" data-bind="buttonGroupChecked: $root.currentVariation().variationAgreed, val: val, text: text"></div>
            </div>

Where $root.currentVariation().variationAgreed is an item that is currently selected and is an observable as part of the following object. 其中$root.currentVariation().variationAgreed是当前选定的一项,作为以下对象的一部分可观察到。

var observableWorkItemVariation = function(data){
var self = this;
data = data || {};
self.id = ko.observable(data.id || "");
self.orderWorkItemID = ko.observable(data.orderWorkItemID || "");
self.variationAgreed = ko.observable(data.variationAgreed || 0);
self.changeWorkBillable = ko.observable(data.changeWorkBillable || 1);
self.declareBillable = ko.observable(data.declareBillable || 0);

self.changeWorkBillable.subscribe(function(val){
   if(self.changeWorkBillable() == 0){
       self.declareBillable(0);
   } 
});

self.changeWorkPayable = ko.observable(data.changeWorkPayable || 1);
self.variationCode = ko.observable(data.variationCode || "");
}

It is correctly highlighting the selected item (No as it is defaulted to 0) but when I try to change it throws the error. 它正确地突出显示了所选项(默认为0,否),但是当我尝试更改它时会引发错误。

It looks like value is out of scope when the click even is fired. click甚至触发时, value似乎超出范围。

As it stands now you're defining value in the local scope of one function and you're then trying to access it later in the local scope of a different function. 目前,您正在一个函数的本地范围内定义value ,然后稍后尝试在另一个函数的本地范围内访问它。

I'd recommend using click binding directly on each div. 我建议直接在每个div上使用click绑定。 Knockout will automatically pass you an instance of the ViewModel that was clicked, so you can use that inside of the click handler on the $root view model to figure out which button was clicked to make it active. 淘汰赛将自动为您传递一个被单击的ViewModel实例,因此您可以在$ root视图模型的单击处理程序内部使用该实例来确定单击了哪个按钮以使其处于活动状态。

Thank you for the suggestions, roy gave a useful comment. 谢谢您的建议,罗伊(Roy)发表了有用的评论。

The problem was that I was creating an object from another object and I was passing variables in as varName instead of varName() to get the value (or ko.toJS(object)) so it was passing the observable through as the value of the observable. 问题是我正在从另一个对象创建一个对象,并以varName而不是varName()的形式传递变量以获取值(或ko.toJS(object)),因此它将observable传递为可观察的。

I struggled to spot this as if you do ko.toJS() on the viewModel in <pre> tags it will unwrap it and show the value as correct. 我很难发现这一点,就好像您在<pre>标记中的viewModel上执行ko.toJS()一样,它会展开并显示正确的值。

Hope that is clear for anyone experiencing the problem as well. 希望对遇到此问题的任何人也很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM