将属性添加到使用Knockout JS映射插件创建的视图模型(Adding properties to the view model created by using the Knockout JS

编程入门 行业动态 更新时间:2024-10-27 21:22:25
将属性添加到使用Knockout JS映射插件创建的视图模型(Adding properties to the view model created by using the Knockout JS mapping plugin)

我正在通过Knockoutjs网站上的地图插件示例工作。

这是示例数据。

Knockout JS Mapping插件

var data = { name: 'Scott', children: [ { id : 1, name : 'Alice' } ] }

该示例显示如何覆盖其中一个子项的映射,但如何更改基础对象的映射。

例如,如果我想为Scott添加一个“FavouriteChild”属性,我将如何去解决它?

我假设我需要在基础映射上使用create函数,但是我找不到任何地方的语法示例。

var myChildModel = function(data) { ko.mapping.fromJS(data, {}, this); this.nameLength = ko.computed(function() { return this.name().length; }, this); } var mapping = { 'children': { create: function(options) { return new myChildModel(options.data); } } } var viewModel = ko.mapping.fromJS(data, mapping);

编辑:从接受的答案下面我发现这个工作

<span data-bind='text: AdditionalProperty'>

淘汰赛代码

var mapping = { create: function (options) { //customize at the root level. var innerModel = ko.mapping.fromJS(options.data); innerModel.AdditionalProperty = 'Hello World'; return innerModel; } } var viewModel = ko.mapping.fromJS(data, mapping); //use this as our model bindings ko.applyBindings(viewModel);

I am working through the mapping plugin example on the Knockoutjs website.

This is the example data.

Knockout JS Mapping Plugin

var data = { name: 'Scott', children: [ { id : 1, name : 'Alice' } ] }

The example shows how to override the mapping for one of the children but how do I alter the mapping for the base object.

If for example I wanted to add a "FavouriteChild" property to Scott how would I go about it?

I assume I need to use the create function on the base mapping but I cannot find an example of the syntax anywhere.

var myChildModel = function(data) { ko.mapping.fromJS(data, {}, this); this.nameLength = ko.computed(function() { return this.name().length; }, this); } var mapping = { 'children': { create: function(options) { return new myChildModel(options.data); } } } var viewModel = ko.mapping.fromJS(data, mapping);

EDIT : From the accepted answer below I found this to work

<span data-bind='text: AdditionalProperty'>

The knockout code

var mapping = { create: function (options) { //customize at the root level. var innerModel = ko.mapping.fromJS(options.data); innerModel.AdditionalProperty = 'Hello World'; return innerModel; } } var viewModel = ko.mapping.fromJS(data, mapping); //use this as our model bindings ko.applyBindings(viewModel);

最满意答案

您需要在映射对象本身上使用create方法,如:

var mapping = { //customize at the root level. create: function(options) { //first map the vm like normal var vm = ko.mapping.fromJS(options.data); //now manipulate the returned vm in any way that you like vm.someProperty = "test"; vm.someComputed = ko.computed(function() { return vm.first() + " " + vm.last(); }); //return our vm that has been mapped and tweaked return vm; } };

You need to use a create method on the mapping object itself like:

var mapping = { //customize at the root level. create: function(options) { //first map the vm like normal var vm = ko.mapping.fromJS(options.data); //now manipulate the returned vm in any way that you like vm.someProperty = "test"; vm.someComputed = ko.computed(function() { return vm.first() + " " + vm.last(); }); //return our vm that has been mapped and tweaked return vm; } };

更多推荐

本文发布于:2023-07-26 10:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   插件   属性   模型   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!