English 中文(简体)
部分更新KnockoutJS绘图皮图
原标题:Performing partial updates with KnockoutJS mapping plugin

如今,我用这名JSON,用KO绘图皮gin,并用工作罚款:

{
  "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"
}

现在我的要求是“部分更新”。 在我愿意这样做的时候,一些情景是:

  • I need to change the Data array in the second control.
  • I need to update only one Control and not the whole Page (this is the class I m serializing, the root in this JSON).
  • I need to add another Control to my Page.

也许还有另一个工作要做,将原物体重新编号为ko.mapping.toJS(viewModel),加以修改,然后重新绘制。 但我认为,你会做得更好。


<>光线> 我尝试了ko.mapping. fromJS(updatedControl, viewModel.Controls(0),但这里是我的法典:

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

response:

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

<><><>>>>> http://jsfiddle.net/faloi/4FcAy/10/“http://jsfiddle.net/faloi/4FcAy/10/

最佳回答
问题回答

我认为,这是关键选择的全局。 我认识到,这种特征有点可笑,但我感到惊讶的是,根本不支持这种特征。

不管怎么说——这是(很遗憾)我最后不得不做些什么来部分更新命令清单——即通过和替换物品。

<代码>数据是我从服务器上的主要航空航天卫星/卫星模型,并载有<编码>数据。 编号ko.observable Array([] of OrderViewModel items。 该文件还载有我想初步绘制的所有其他内容:<代码>数据>.产品、<代码>数据foo<>/code>。

我有制定新<代码”的制图规则。 在制图过程中自动使用Model号令。

            var mapping =
            {
                 orders :
                {
                    key: function (item)
                    {
                        return ko.utils.unwrapObservable(item.orderId);
                    },

                    create: function (options)
                    {
                        return new OrderViewModel(options.data);
                    }
                }
            };

当我从服务器中获取数据时,我首先想对我的模型进行完整的制图。 因此,我设立了<条码>初始更新=true。

当我想重写一个单项命令时,我有<条码> 首次更新=false。

     if (initialUpdate) 
     {
         // standard initial mapping 
         ko.mapping.fromJS(data, mapping, this);
     }
     else
     {
         // merge orders
         var newModel = ko.mapping.fromJS(data, mapping);

         // go through all orders in the updated list
         for (var i = 0; i < newModel.orders().length; i++)
         {
             var newOrder = newModel.orders()[i];

             // find order with same key in existing ko model
             var match = ko.utils.arrayFirst(this.orders(), function (item)
             {
                 return newOrder.orderId() === item.orderId();
             });

             if (match == null)
             {
                 // TODO: insert new order                            
             }

             // replace original order in list
             this.orders.replace(match, newOrder);
         }
     }




相关问题
How to go from DOM node to viewModel object?

When the drop function is called back, this is set to the droppable DOM node (the target) and ui.draggable is the DOM node which was dragged. Is there an idiomatic way of getting the model object ...

making fields observable after ajax retrieval in knockout.js

I am wondering how I can make certain fields observables in knockout.js that I get from an ajax call without having to define the whole object in my viewmodel. Is this possible? Here is what I have ...

ASP.NET MVC Validation with jQuery $.ajax

I have a situation where I am sending data to a Controller via jQuery $.ajax, the data is a serialized json string. (MVC 3.0) It binds it fine - my controller receives the results and they are ...

Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for ...

Anyone using Knockoutjs with asp.net-mvc in anger? [closed]

I find it very interesting and have a prototype working based on Steve s mvc sample and another small sample from this thread. Using json.net to deserialize within the post action as I couldn t ...

热门标签