2014-09-26 46 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會導致錯誤。

相關問題