我有这段代码:
self.EditItemPopup = function (something) {
var temp = ko.mapping.toJS(something);
//self.ItemToEdit = ko.mapping.fromJS(temp, EditItem, self.ItemToEdit);
ko.mapping.fromJS(temp, EditItem,self.ItemToEdit);
self.FindMatchingCategory(something.CategoryID());
$("#editItemPopup").dialog("open");
};
self.FindMatchingCategory = function (categoryID) {
ko.utils.arrayForEach(self.ViewModel().Categories(), function (categoryToFind) {
if (categoryToFind.CategoryID() == categoryID) {
self.ItemEditCategory(categoryToFind);
}
});
};
当一切正常工作时调用self.EditItemPopup,但是当第二次调用它时,我收到此错误: 未捕获RangeError:超出最大调用堆栈大小
有人能告诉我问题在哪里吗?
谢谢!
编辑:
EditItem映射:
var EditItem = {
ItemName: ko.validatedObservable().extend({
required: {
message: "Please enter an item name.",
insertMessages: false
}
}),
ItemCost: ko.validatedObservable().extend({
required: {
message: "Please enter a valid price.",
insertMessages: false
}
}),
CategoryID: ko.observable()
};
答案 0 :(得分:0)
正如Tomas所提到的,你的映射看起来是错误的。这是一个潜在的解决方案。注意:我在这里做了一些假设,因为我看不到你的所有模型。
定义编辑模型:
function EditItemModel(data){
var self = this;
ko.mapping.fromJS(data, {}, self);
self.ItemName.extend({
required: {
message: "Please enter an item name.",
insertMessages: false
}
});
self.ItemCost.extend({
required: {
message: "Please enter a valid price.",
insertMessages: false
}
});
}
像这样修改EditItemPopup函数:
self.EditItemPopup = function (something) {
var temp = ko.mapping.toJS(something);
self.ItemToEdit(new EditItemModel(temp));
self.FindMatchingCategory(something.CategoryID());
$("#editItemPopup").dialog("open");
};
答案 1 :(得分:-1)
ko.mapping.fromJS有签名:ko.mapping.fromJS(data, mappingOptions, viewModel);
但是,在您的情况下,第二个参数看起来不像映射选项。它看起来像视图模型。