使用KnockoutJS映射插件执行部分更新 [英] Performing partial updates with KnockoutJS mapping plugin

查看:121
本文介绍了使用KnockoutJS映射插件执行部分更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我将此JSON与KO Mapping插件一起使用,并且工作正常:

Right now, I'm using this JSON with the KO Mapping plugin and it's working fine:

{
  "Controls": [
    {
      "Fields": [
        {
          "Name": "emailField", 
          "Text": "email", 
          "Visible": true
        }, 
        {
          "Name": "hiddenField", 
          "Text": "text", 
          "Visible": true
        }
      ], 
      "Name": "form2", 
      "Type": "Form"
    }, 
    {
      "Data": [
        [
          "Federico Aloi", 
          20
        ], 
        [
          "Andres Lopez", 
          31
        ], 
        [
          "Pablo Perez", 
          32
        ]
      ], 
      "Fields": [
        {
          "Name": "nameField", 
          "Text": "Nombre", 
          "Visible": true
        }, 
        {
          "Name": "ageField", 
          "Text": "Edad", 
          "Visible": true
        }
      ], 
      "Name": "datagrid1", 
      "Type": "Datagrid"
    }
  ], 
  "Name": "pagina1", 
  "Title": "Probando el KO"
}

现在,我的要求是执行部分更新".我想这样做的一些情况:

Now my requirement is to perform "partial updates". Some scenarios when I'd like to do that:

  • 我需要在第二个控件中更改Data数组.
  • 我只需要更新一个控件,而不是整个页面(这是我正在序列化的类,是此JSON的根).
  • 我需要向页面添加另一个控件.

也许另一个解决方法是使用ko.mapping.toJS(viewModel)重新创建原始对象,对其进行更改,然后再次重新映射...但是我相信您会得到更好的东西.

Perhaps another workaround would be recreating the original object with ko.mapping.toJS(viewModel), change it and then re-map it again... but I believe that you will come out with something better.

我尝试了ko.mapping.fromJS(updatedControl, viewModel.Controls()[0]),但没有用,这是我的代码:

I tried with ko.mapping.fromJS(updatedControl, viewModel.Controls()[0]) but it didn't work, here is my code:

function (control) {
    $.getJSON($.format('api/control/{0}/{1}', viewModel.Name(), control.Name()), function (response) {
        ko.mapping.fromJS(response, viewModel.Controls()[0]);
    });
},

响应:

{
  "Fields": [
    {
      "Name": "emailField", 
      "Text": "email", 
      "Visible": true
    }, 
    {
      "Name": "hiddenField", 
      "Text": "text", 
      "Visible": true
    }
  ], 
  "Name": "form2", 
  "Type": "Form"
}

EDIT2 :在 http://jsfiddle.net/faloi/4FcAy/10上进行检查/

推荐答案

我认为这是映射插件中最常见的问题.该插件目前只能对属性进行部分更新.例如,如果您要将以下数据重新映射到您的对象

I think this is now the single most asked question re the mapping plugin. The plugin can only currently do partial updates of properties. E.g if you were to remap the below data to your object

{ "Name": "pagina1 foo", "Title": "Probando el KO bar" }

它将仅更新那些属性.但是,对集合的处理方式有所不同,因此,如果要使用以下内容重新映射.

It would update just those properties. However the collections are treated differently so if you were to remap with the below.

{ "Name": "pagina1 foo", "Title": "Probando el KO bar", 
  Controls: [ { // snip some updated object } ] }

它将删除控件集合中的不匹配项,而不是更新匹配项.使其生效的唯一方法是遍历集合并映射要更新的每个项目.我相信您的代码应该在正确的轨道上

It would remove the non-matching item in your Controls collection instead of updating the matching item. The only way to get this to work is to loop through the collection and map each individual item you wish to update. Your code is almost on the right track I believe it should be

ko.mapping.fromJS(updatedControl, viewModel.Controls()[0])

编辑

您缺少fromJS调用中的中间映射参数.

You were missing the middle mapping argument on the fromJS call.

http://jsfiddle.net/3CtFq/

映射对象通常是可选的,但我想如果您正在映射子对象,则插件无法告诉此子项父对象最初是没有选项的映射.

The mapping object is usually optional but I guess if you are mapping a sub object as is your case, the plugin can't tell that this sub items parent was originally mapped with no options.

希望这会有所帮助.

这篇关于使用KnockoutJS映射插件执行部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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