KnockoutJS超出最大调用堆栈大小

KnockoutJS超出最大调用堆栈大小

本文介绍了KnockoutJS超出最大调用堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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:超出了最大调用堆栈大小

When self.EditItemPopup is called once everything works fine, but when it is called a second time I get this error:Uncaught RangeError: Maximum call stack size exceeded

有人可以告诉我问题出在哪里吗?

Could someone tell me where the problem is?

谢谢!

EditItem映射:

EditItem mapping:

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()
};

推荐答案

正如Tomas所说,您的映射看起来不正确.这是一个潜在的解决方案.注意:由于无法看到您的所有模型,因此我在这里做一些假设.

As Tomas mentioned, your mapping looks wrong. Here is a potential solution. NOTE: I am making a few assumptions here as I cannot see all of your model.

定义要编辑的模型:

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函数:

Modify the EditItemPopup function like this:

self.EditItemPopup = function (something) {
    var temp = ko.mapping.toJS(something);
    self.ItemToEdit(new EditItemModel(temp));
    self.FindMatchingCategory(something.CategoryID());
    $("#editItemPopup").dialog("open");
};

这篇关于KnockoutJS超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:51