2014-09-26 130 views
4
var ready; 

    ready = function() { 
    var imageChooser, productChooser; 
    productChooser = function() { 
     var self; 
     self = this; 
     self.quantityId = ko.observable(1); 
     self.quantityText = ko.observable('QTY'); 
     self.genderId = ko.observable(1); 
     self.sizeId = ko.observable(1); 
     self.colorId = ko.observable(1); 
     self.fullSize = ko.observable('SIZE'); 
     this.setGenderAndSize = function(stringtoparse, thestring) { 
     var values; 
     values = stringtoparse.split(":"); 
     self.fullSize("SIZE: " + thestring); 
     self.genderId(values[0]); 
     self.sizeId(values[1]); 
     }; 
     this.setQuantity = function(quantity) { 
     self.quantityId(quantity); 
     self.quantityText("QTY: " + quantity); 
     }; 
    }; 
    imageChooser = function() { 
     this.clicked = ko.observable(); 
     this.setBigImage = (function(message) { 
     alert(message); 
     }).bind(this); 
    }; 
    ko.applyBindings(new productChooser(), $("#genderAndSizeChooser")[0]); 
    ko.cleanNode($('#genderAndSizeChooser')[0]); 
    return ko.applyBindings(new imageChooser(), $('#imageChooser')[0]); 
    }; 

    $(document).on('ready page:load', ready); 

我目前拥有此代码。不知何故,ko.cleanNode代码:Uncaught TypeError:无法读取ko.cleanNode上未定义的属性'nodeType'

ko.cleanNode($('#genderAndSizeChooser')[0]); 

产生一个错误:

Uncaught TypeError: Cannot read property 'nodeType' of undefined 

人有一个想法错误的原因是什么?

+1

你确定实际上有一个'genderAndSizeChooser'的'id'元素吗? 'ko.applyBindings'调用会收到未定义的,并像正常一样将绑定应用于主体,这样就不会出错,但在undefined上调用'ko.cleanNode'会导致错误。 – 2014-09-26 05:25:00

+0

omg。请做出答案。 applyBindings的方式实际上不会发送任何错误。我的Rails应用程序已覆盖文档的“id” – 2014-09-26 06:01:58

回答

5

看起来它实际上没有找到一个ID为genderAndSizeChooser的元素。

ko.applyBindings通话将收到undefined和应用绑定到身体像正常的,这样,才不会出错,但调用ko.cleanNodeundefined会导致错误。

相关问题