子集合映射未触发 [英] Child collection mapping not firing

查看:76
本文介绍了子集合映射未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了搜索引擎,找到了很多答案,但是我陷入了一个问题.问题在于子集合映射似乎不触发.在下面的代码中,警报已创建组",但显示了已创建根"警报.

I have used the search engine, and found alot of answers, but I am stuck on a problem. The problem is that the child collection mapping seems to not fire. In the below code the alert "Groups created", but the "Root created"-alert is shown.

var mapping = {
    'Groups': {
        create: function (options) {
            var lang = ko.observable(ko.mapping.fromJS(options.data));
            alert('Groups created');
            return lang;
        }
    },
    create: function (options) {
        var innerModel = ko.mapping.fromJS(options.data);
        alert('Root created');
        innerModel.ajaxSave = submitFormWithAjax;
        return innerModel;
    }
}

var viewModel = null;
function loadViewModel(model, offlineCache, key) {
    viewModel = ko.mapping.fromJS(model, mapping);

    if (typeof extendViewModel == 'function') {
        extendViewModel();
    }

    ko.applyBindings(viewModel);
}

我的json数据看起来像这样

and my json data looks like this

{
  "Groups": [
    {
      "Texts": [
        {
          "Language": {
            "Id": 1,
            "Name": "English",
            "Lcid": 2057
          },
          "Id": 1,
          "Value": "Display name"
        },
        {
          "Language": {
            "Id": 2,
            "Name": "Swedish",
            "Lcid": 1053
          },
          "Id": 2,
          "Value": "Visningsnamn"
        }
      ],
      "Id": 1,
      "InternalName": "3dc075e3-ff96-4044-b9bb-4a0404912866",
      "DisplayName": "Display name"
    }
  ],
  "AvailableLanguages": [
    {
      "Id": 1,
      "Name": "English",
      "Lcid": 2057
    },
    {
      "Id": 2,
      "Name": "Swedish",
      "Lcid": 1053
    }
  ]
}

为什么会触发我的网上论坛创建的提醒?

Why is my Groups-created alert fired?

推荐答案

由于某种原因,由于某种原因,您不能在多个级别上创建函数,可以做到这一点

You cant have create functions on more than one level for some reason with the mapping plugin, you can do this

RootViewModel = function(data) {
   this.stuffAddedToViewModelBeforeMapping = "Hej på dig";

   var mapping = {
       Groups: {
           create: function(options) {
               return new GroupViewModel(options.data);
           }
       },
       AvailableLanguages: {
           create: function(options) {
               return new LanguageViewModel(options.data);
           }
       }
   };
   ko.mapping.fromJS(data, mapping, this);
};

LanguageViewModel = function(data) {
    ko.mapping.fromJS(data, {}, this);
}

GroupViewModel = function(data) {
    ko.mapping.fromJS(data, {}, this);
};

http://jsfiddle.net/42ZwC/

Lycka直到!

这篇关于子集合映射未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆