knockout.js partial mapping from json

﹥>﹥吖頭↗ 提交于 2020-01-06 08:27:12

问题


On the knockout.js site's documentation they say that when you get data back from the server you can do this:

// Every time data is received from the server:
ko.mapping.fromJS(data, viewModel);

I'd like to partially map the data back into my object model. Is that possible?

I have a viewModel.jobs[i].JobType child object, so I'd like to do something like this:

ko.mapping.fromJS(data.jobType, viewModel.jobs[i].JobType);

... meaning I'd like to just map in the jobType from the result from the server into this specific job's JobType field.

... Also keeping in mind:

// does not work because viewModel.jobs[i].JobType() is not a function.
viewModel.jobs[i].JobType(data.JobType);

回答1:


This worked:

ko.mapping.fromJS(data.job, viewModel.jobs[i]);



来源:https://stackoverflow.com/questions/11370052/knockout-js-partial-mapping-from-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!