简体   繁体   English

KnockoutJS中的后期可观察数组绑定

[英]Late observable-array binding in KnockoutJS

My MVC razor view renders this markup: 我的MVC剃刀视图呈现此标记:

function existingNamingsViewModel() {
            var self = this;
            var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model));
            self.ExistingNamings = ko.observableArray(initialData);
        }
        ko.applyBindings(new existingNamingsViewModel(), document.getElementById("namings-control"));

Into a correct JS-serialized code, with initialData variable being initialized as: 转换为正确的JS序列化代码,并将initialData变量初始化为:

var initialData = [{"TypeName":"Orders","NameBlocks":["{intInc_G}","/","{intInc_D}","/02/-","{yy}"],"ParamBlocks":["2296","","1","",""]}];

The resulting html, after the ko bindings are applied, takes the form of an editable grid: 应用ko绑定后,生成的html采用可编辑网格的形式: 在此处输入图片说明

I need knockoutJS to automatically be able to update the view model whenever user changes data in those input fields. 每当用户在这些输入字段中更改数据时,我都需要用基因敲除JS自动更新视图模型。 But since the view model was initialize from simple JSON object, NameBlocks and ParamBlocks are not ko.observableArray . 但是由于视图模型是从简单的JSON对象初始化的,因此NameBlocksParamBlocks并不是ko.observableArray I need them to be. 我需要他们。 How do I achieve this? 我该如何实现?

PS One idea is to do a more complex initialization where razor serializes only Name / ParamBlocks arrays, and ExistingNamings array is populated via javascript code manually, creating a custom Naming class object that has Name / ParamBlocks wrapped into ko.observableArray . PS一个想法是做一个更复杂的初始化,其中剃刀仅序列化Name / ParamBlocks数组,而ExistingNamings数组通过javascript代码手动填充,从而创建一个自定义的Naming类对象,该对象将Name / ParamBlocks包装到ko.observableArray But is this the only way? 但这是唯一的方法吗?

There's a mapping plug-in available that should do the trick for you, though you may need to change the way the data is delivered. 有一个可用的映射插件可以为您解决问题,尽管您可能需要更改数据传递的方式。

The mapping plug-in provides a function that maps a JSON object to a view model: 映射插件提供了将JSON对象映射到视图模型的功能:

var viewModel = ko.mapping.fromJS(data);

Note though, in the documentation (linked to above), all properties within JSON objects are named. 但是请注意,在文档中(链接到上文),JSON对象内的所有属性均已命名。

The initialData you provided in your example contains arrays that are simply collections of values, rather than collections of named values. 您在示例中提供的initialData包含的数组只是值的集合,而不是命名值的集合。

Also, initialData is an array containing a single object. 另外, initialData是一个包含单个对象的数组。 If this scenario is always going to expect a single object, then this solution is easier. 如果这种情况总是希望有一个对象,那么此解决方案会更容易。

Let's say that you're able to deliver initialData in the following format: 假设您可以采用以下格式传递initialData

var initialData = {
    "TypeName":"Orders",
    "NameBlocks": [
        {"block":"{intInc_G}"},
        {"block":"/"},
        {"block":"{intInc_D}"},
        {"block":"/02/-"},
        {"block":"{yy}"}
    ],
    "ParamBlocks":[
        {"block":"2296"},
        {"block":""},
        {"block":"1"},
        {"block":""},
        {"block":""}
    ]
};

Then creating the view model would be as simple as the following: 然后,创建视图模型将如下所示:

var viewModel = ko.mapping.fromJS(initialData);

$(function() {
   ko.applyBindings(viewModel); 
});

Here's a fiddle I set up so you can see this in action: http://jsfiddle.net/jimmym715/qUjLQ/ 这是我设置的小提琴,因此您可以在实际中看到它: http : //jsfiddle.net/jimmym715/qUjLQ/

Hopefully, your solution will be as simple as a few data format changes and the use of the mapping plug-in. 希望您的解决方案将像更改一些数据格式和使用映射插件一样简单。

Regardless, the mapping plug-in should send you in the right direction. 无论如何,映射插件都应该向正确的方向发送信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM