绑定处理程序的值不是函数'knockoutJS

时间:2015-12-08 10:22:15

标签: javascript knockout.js data-binding bindinghandlers

我对这里导致问题的原因感到难过,因为这个绑定处理程序通常工作正常(下面)

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


}
};

我一直在第7行value(allBindingsAccessor.get('val'));收到错误,说价值不是函数?

选项在我的viewmodel中定义如下: -

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

相应的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>

其中$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 || "");
}

正确突出显示所选项目(否则默认为0)但是当我尝试更改时会抛出错误。

2 个答案:

答案 0 :(得分:1)

It looks like Public Sub previewPDF(XXlotsofstuffXX) If myStream IsNotNothing Then Me.pdf.Load(myStream) Else Me.pdf.Filepath = file End If me.owner = parentForm Me.Show() Me.CenterToParent() Me.BringToFront() '<--- Try this Me.Focus() '<--- Try this End Sub is out of scope when the value even is fired.

As it stands now you're defining click in the local scope of one function and you're then trying to access it later in the local scope of a different function.

I'd recommend using value binding directly on each div. 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.

答案 1 :(得分:0)

感谢您的建议,罗伊给出了有用的评论。

问题在于我是从另一个对象创建一个对象而我是以varName而不是varName()传递变量来获取值(或ko.toJS(object))所以它将observable传递给了可观察的价值。

我很难发现这一点,好像你在<pre>标签的viewModel上做了ko.toJS(),它会打开它并显示正确的值。

希望对于遇到此问题的人来说也很清楚。