我想在我的JSON文件中加載,然後爲每個項目插入。使用jQuery插入到可觀察數組中的好方法
所以我有這樣的代碼
function ProductType(id, name) {
var self = this;
self.id = id;
self.name = name;
}
function ProductsViewModel() {
var self = this;
var jqxhr = $.getJSON("data/product.json").success(function(data, status, xhr) {
self.products = ko.observableArray([
$.each(data.data.productTypeList, function(i,item){
new ProductType(i, item.longName);
})
]);
})
.error(function() { alert("error"); })
.complete(function() {
console.log("fetch complete + " + this);
});
}
我不知道如何最好的做法是在當前時間我得到這個錯誤
錯誤插入的可觀察陣列從每個功能
:500錯誤get/knockoutJQMProducts /#products無法解析綁定。 消息:ReferenceError:產品未定義; 綁定值:foreach:產品
但如果我console.log(i)在每個語句內返回結果。
感謝
好點。這確實減少了陣列變化時敲除發送的通知的數量。 – ryadavilli