knockout.js mapping causing infinite recursion on IE9

久未见 提交于 2019-12-07 12:07:27

You should divide your mapping object to 2 objects. Use first for mapping view model and second for children.

var mapping = {
            create: function(options) {
                return new MyViewModel(options.data);
            }
        }

var childrenMapping = {
                'ChildItems': {
                    create: function(options) {
                        return new ChildVM(options.data);
                    }
                }
}

Your ajax request remain the same. Update MyViewModel function to use childrenMapping :

 function MyViewModel(data) {
            var self = this;
            this.$type = 'MyViewModel';

            [some observables]         
            ...

            ko.mapping.fromJS(data, childrenMapping, this);
        }

The root cause of the issue is recursive calling of mapping. When you call ko.mapping.fromJS(data, mapping); knockout calls create rule where creates MyViewModel object. In MyViewModel's constructor you call ko.mapping.fromJS due to the using of the same mapping options knockout calls the same create rule that creates MyViewModel object where ko.mapping.fromJS is called with the same options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!