2017-02-28 196 views
0

我在尝试从数据上下文值访问我的observable时遇到此错误。 TypeError: data.itemNo is not a functionTypeError:data.itemNo不是函数

这里是我的淘汰赛:

var TallyModel = function (data) { 
     var self = this; 

     self.itemNo = ko.observable(); 
     self.qty = ko.observable(); 
     self.price = ko.observable(); 
     self.bro = ko.observable(); 
     self.desc = ko.observable(); 
     self.extend = ko.observable(); 
     self.total = ko.observable(); 
     self.seq = ko.observable(); 
    } 

    var TallyViewModel = function (items) { 
     var self = this; 

     if(items != null) { 
      self.items = ko.observableArray(items.map(function(item) { return new TallyModel(item) })); 
     } 
     else { 
      self.items = ko.observableArray([]); 
     } 

     self.addLine = function() { 
      var tModel = new TallyModel(); 
      self.items.push(tModel); 
     }; 

     self.insertLine = function(index) { 
      self.items.splice(index, 0, new TallyModel()); 
     }; 

     self.removeItem = function(index) { 
      console.log(self.items().length); 
      if(self.items().length > 1) { 
       self.items.splice(index, 1); 
      } 
     }; 

     self.checkItemNo = function(data, index) { 
      console.log(data); 
      **var itemNo = $.trim(data.itemNo());** 
      console.log(itemNo); 
      $.each(validItems, function (i, elem) { 
       if (elem.itemNo == itemNo) { 
        data.price(elem.retail); 
        data.bro(elem.brocCode); 
        data.desc(elem.itemDesc); 
        data.extend(elem.extPrice); 
        data.seq(index); 
       } 
       else { 
        console.log("could not find " + itemNo + " - " + elem.itemNo); 
       } 
      }); 
     }; 
    } 

如这里要求的是HTML:

<tbody data-bind="foreach: items"> 
      <tr> 
       <td><input type="text" data-bind="value: itemNo, insertPress: $index, deletePress: $index, event: { blur: $parent.checkItemNo.bind($data, $index) }, attr: { name: 'itemNo[' + $index() + ']', id: 'itemNo[' + $index() + ']' }" class="form-control" /></td> 
       <td><input type="text" data-bind="value: qty, insertPress: $index, tabEnterPress: '#tallyEntry', deletePress: $index, attr: { name: 'itemQty[' + $index() + ']', id: 'itemQty[' + $index() + ']' }" class="form-control" /></td> 
       <td><input type="text" data-bind="value: price, attr: { name: 'itemPrice[' + $index() + ']', id: 'itemPrice[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
       <td><input type="text" data-bind="value: bro, attr: { name: 'itemBro[' + $index() + ']', id: 'itemBro[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
       <td><input type="text" data-bind="value: desc, attr: { name: 'itemDesc[' + $index() + ']', id: 'itemDesc[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
       <td><input type="text" data-bind="value: extend, attr: { name: 'itemExtend[' + $index() + ']', id: 'itemExtend[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
       <td><input type="text" data-bind="value: total, attr: { name: 'itemTotal[' + $index() + ']', id: 'itemTotal[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
       <td><input type="text" data-bind="value: seq, attr: { name: 'itemSeq[' + $index() + ']', id: 'itemSeq[' + $index() + ']' }" class="form-control" readonly="readonly" tabindex="-1" /></td> 
      </tr> 
     </tbody> 

我已经加粗/主演的代码的罪魁祸首线。它正在工作,我必须改变一些事情,不知道为什么它不再有效。让我知道是否需要发布相关的HTML。

+0

当你'console.log(数据)'看到什么? 'itemNo'作为'function'或'Number' – Jag

+0

请提供完整的示例。我在这里看不到'checkItemNo'调用的地方。争论有点问题。 –

回答

2

您使用bind的功能绑定

blur: $parent.checkItemNo.bind($data, $index) 

bind的第一个参数是thisArg。它可能会更容易使用这种语法

blur: function(){ $parent.checkItemNo($data, $index); } 
+0

不完全了解它,但是这固定了它!谢谢!什么是'thisArg'? – dmikester1

+0

@ dmikester1阅读文档。我把它连接起来是有原因的。 – Jamiec

+0

aw,非常感谢! – dmikester1