{"id":191171,"date":"2023-07-31T16:26:28","date_gmt":"2023-07-31T08:26:28","guid":{"rendered":"https:\/\/www.idc.net\/help\/191171\/"},"modified":"2023-07-31T16:26:28","modified_gmt":"2023-07-31T08:26:28","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91knockout%e6%95%99%e7%a8%8b%ef%bc%9a%e7%ac%ac%e4%b8%83%e7%ab%a0%ef%bc%9amapping%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/191171\/","title":{"rendered":"\u540e\u6d6a\u4e91Knockout\u6559\u7a0b\uff1a\u7b2c\u4e03\u7ae0\uff1aMapping\u63d2\u4ef6"},"content":{"rendered":"<h2>Mapping\u63d2\u4ef6<\/h2>\n<p>Knockout\u8bbe\u8ba1\u6210\u5141\u8bb8\u4f60\u4f7f\u7528\u4efb\u4f55JavaScript\u5bf9\u8c61\u4f5c\u4e3aview model\u3002\u5fc5\u987bview model\u7684\u4e00\u4e9b\u5c5e\u6027\u662fobservable\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528KO\u7ed1\u5b9a\u4ed6\u4eec\u5230\u4f60\u7684UI\u5143\u7d20\u4e0a\uff0c\u5f53\u8fd9\u4e9bobservable\u503c\u6539\u53d8\u7684\u65f6\u5019\uff0c\u8fd9\u4e9bUI\u5143\u7d20\u5c31\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<p>\u7edd\u5927\u591a\u6570\u7a0b\u5e8f\u90fd\u9700\u8981\u4ece\u670d\u52a1\u5668\u7aef\u83b7\u53d6\u6570\u636e\uff0c\u4f46\u662f\u7531\u4e8e\u670d\u52a1\u5668\u4e0d\u77e5\u9053observable\u7684\u6982\u5ff5\u662f\u4ec0\u4e48\uff0c\u5b83\u53ea\u652f\u6301\u7b80\u5355\u7684JavaScript\u5bf9\u8c61\uff08\u901a\u5e38\u662f\u5e8f\u5217\u5316\u4ee5\u540e\u7684JSON\uff09\uff0cmapping\u63d2\u4ef6\u53ef\u4ee5\u8ba9\u4f60\u5f88\u65b9\u4fbf\u5730\u5c06\u7b80\u5355JavaScript\u5bf9\u8c61mapp\u5230\u5e26\u6709observable\u503c\u7684view model\u3002\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5199JavaScript\u4ee3\u7801\u5c06\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u6570\u636e\u6765\u6784\u5efa view model\uff0cmapping\u63d2\u4ef6\u53ea\u662f\u4e00\u79cd\u5f88\u597d\u7684\u66ff\u4ee3\u800c\u5df2\u3002<\/p>\n<p>&nbsp;<strong>\u4e0b\u8f7d<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Version 2.0&nbsp;\uff08\u6700\u5c0f\u7248\u672c8.6kb\uff09<\/p>\n<p><strong>\u4f8b\u5b50\uff1a\u624b\u5de5mapping<\/strong><\/p>\n<p>\u663e\u793a\u5f53\u524d\u670d\u52a1\u5668\u65f6\u95f4\u548c\u4f60\u7f51\u7ad9\u4e0a\u7684\u5f53\u524d\u7528\u6237\u6570\u3002\u4f60\u5e94\u8be5\u4f7f\u7528\u5982\u4e0b\u7684view model\u6765\u4ee3\u8868\u4f60\u7684\u8fd9\u4e9b\u4fe1\u606f\uff1a<\/p>\n<pre><code>var viewModel = {\n    serverTime: ko.observable(),\n    numUsers: ko.observable()\n}<\/code><\/pre>\n<p>\u7136\u540e\u7ed1\u5b9aview model\u5230HTML\u5143\u7d20\u4e0a\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>The time on the server is: &lt;span data-bind='text: serverTime'&gt;&lt;\/span&gt;\nand &lt;span data-bind='text: numUsers'&gt;&lt;\/span&gt; user(s) are connected.<\/code><\/pre>\n<p>\u7531\u4e8eview model\u5c5e\u6027\u662fobservable\u7684\uff0c\u5728\u4ed6\u4eec\u53d8\u5316\u7684\u65f6\u5019\uff0cKO\u4f1a\u81ea\u52a8\u66f4\u65b0\u7ed1\u5b9a\u7684HTML\u5143\u7d20\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6700\u65b0\u7684\u6570\u636e\u3002\u6216\u8bb8\u6bcf\u96945\u79d2\u4f60\u8981\u8c03\u7528\u4e00\u6b21Ajax\u8bf7\u6c42\uff08\u4f8b\u5982\uff0c\u4f7f\u7528jQuery\u7684$.getJSON\u6216$.ajax\u51fd\u6388\uff09\uff1a<\/p>\n<pre><code>var data = getDataUsingAjax();          \/\/ Gets the data from the server<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u670d\u52a1\u5668\u8fd4\u56de\u548c\u4e0b\u9762\u76f8\u4f3c\u7684JSON\u6570\u636e\uff1a<\/p>\n<pre><code>{\n    serverTime: '2010-01-07',\n    numUsers: 3\n}<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u7528\u8fd9\u4e9b\u6570\u636e\u66f4\u65b0\u4f60\u7684view model\uff08\u4e0d\u4f7f\u7528mapping\u63d2\u4ef6\uff09\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ Every time data is received from the server:\nviewModel.serverTime(data.serverTime);\nviewModel.numUsers(data.numUsers);<\/code><\/pre>\n<p>\u4e3a\u4e86\u4f7f\u6570\u636e\u663e\u793a\u5728\u9875\u9762\u4e0a\uff0c\u6240\u6709\u7684\u5c5e\u6027\u90fd\u8981\u50cf\u8fd9\u6837\u5199\u4ee3\u7801\u3002\u5982\u679c\u4f60\u7684\u6570\u636e\u7ed3\u6784\u5f88\u590d\u6742\u7684\u8bdd\uff08\u4f8b\u5982\uff0c\u5305\u542b\u5b50\u5bf9\u8c61\u6216\u8005\u6570\u7ec4\uff09\uff0c\u90a3\u5c31\u7ef4\u62a4\u8d77\u6765\u5c31\u76f8\u5f53\u75db\u82e6\u3002mapping\u63d2\u4ef6\u5c31\u662f\u6765\u8ba9\u4f60\u8ba9\u4f60\u7684JavaScript\u7b80\u5355\u5bf9\u8c61\uff08\u6216JSON\u7ed3\u6784\uff09\u8f6c\u6362\u6210observable\u7684view model\u7684\u3002<\/p>\n<p><strong>\u4f8b\u5b50\uff1a\u4f7f\u7528ko.mapping<\/strong><\/p>\n<p>\u901a\u8fc7mapping\u63d2\u4ef6\u521b\u5efaview model\uff0c\u76f4\u63a5\u4f7f\u7528ko.mapping.fromJS\u51fd\u6570\u6765\u521b\u5efa\uff1a<\/p>\n<pre><code>var viewModel = ko.mapping.fromJS(data);<\/code><\/pre>\n<p>\u5b83\u4f1a\u81ea\u52a8\u5c06data\u91cc\u6240\u6709\u7684\u5c5e\u6027\u521b\u5efa\u6210observable\u7c7b\u578b\u7684\u5c5e\u6027\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7ko.mapping.fromJS \u51fd\u6570\u5b9a\u671f\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u66f4\u65b0\u4f60\u7684view model\uff1a<\/p>\n<pre><code>\/\/ Every time data is received from the server:ko.mapping.fromJS(data, viewModel);<\/code><\/pre>\n<p><strong>\u5982\u4f55mapping\uff1f<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp; \u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u90fd\u88ab\u8f6c\u6362\u6210observable\u7c7b\u578b\u503c\uff0c\u5982\u679c\u83b7\u53d6\u7684\u5bf9\u8c61\u7684\u503c\u6539\u53d8\u4e86\uff0c\u5c31\u4f1a\u66f4\u65b0\u8fd9\u4e2aobservable\u7c7b\u578b\u7684\u503c.<\/p>\n<p>&nbsp;&nbsp;&nbsp; \u6570\u7ec4\u4e5f\u88ab\u8f6c\u6362\u6210\u4e86observable\u6570\u7ec4\uff0c\u5982\u679c\u670d\u52a1\u5668\u66f4\u65b0\u6539\u53d8\u4e86\u6570\u7ec4\u7684\u4e2a\u6570\uff0cmapping\u63d2\u4ef6\u4e5f\u4f1a\u6dfb\u52a0\u6216\u8005\u5220\u9664\u76f8\u5e94\u7684item\u9879\uff0c\u4e5f\u4f1a\u5c3d\u91cf\u4fdd\u6301\u548c\u539f\u751fJavaScript\u6570\u7ec4\u76f8\u540c\u7684order\u987a\u5e8f\u3002<\/p>\n<p><strong>Unmapping<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8ba9map\u8fc7\u7684\u5bf9\u8c61\u8f6c\u6362\u6210\u539f\u6765\u7684JavaScript\u5bf9\u8c61\uff0c\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<pre><code>var unmapped = ko.mapping.toJS(viewModel);<\/code><\/pre>\n<p>\u4f1a\u521b\u5efa\u4e00\u4e2aunmapped\u5bf9\u8c61\uff0c\u53ea\u5305\u542b\u4f60\u4e4b\u524dmap\u8fc7\u7684\u5bf9\u8c61\u5c5e\u6027\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u5728view model\u4e0a\u624b\u5de5\u6dfb\u52a0\u7684\u5c5e\u6027\u6216\u8005\u51fd\u6570\u90fd\u4f1a\u88ab\u5ffd\u7565\u7684\uff0c\u552f\u4e00\u4f8b\u5916\u7684\u662f_destroy\u5c5e\u6027\u662f\u53ef\u4ee5unmapped\u56de\u6765\u7684\uff0c\u56e0\u4e3a\u4f60\u4eceko.observableArray\u91ccdestroy\u4e00\u4e2aitem\u9879\u7684\u65f6\u5019\u4f1a\u751f\u6210\u8fd9\u4e2a\u5c5e\u6027\u3002 \u8bf7\u53c2\u8003\u201c\u9ad8\u7ea7\u7528\u6237\u201d\u5c0f\u8282\u5982\u4f55\u914d\u7f6e\u4f7f\u7528\u3002<\/p>\n<p><strong>\u4e0eJSON\u5b57\u7b26\u4e32\u4e00\u8d77\u4f7f\u7528<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7684Ajax\u8c03\u7528\u8fd4\u56de\u7684\u662fJSON\u5b57\u7b26\u4e32\uff08\u800c\u4e0d\u662f\u53cd\u5e8f\u5217\u5316\u540e\u7684JavaScript\u5bf9\u8c61\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528ko.mapping.fromJSON\u51fd\u6570\u6765\u521b\u5efa\u6216\u8005\u66f4\u65b0\u4f60\u7684view model\u3002\u7528ko.mapping.toJSON\u5b9e\u73b0unmap\u3002<\/p>\n<p>\u4f7f\u7528.from\/toJSON\u51fd\u6570\u5904\u7406JSON\u5b57\u7b26\u4e32\u548c\u4f7f\u7528.from\/toJS\u51fd\u6570\u5904\u7406JS\u5bf9\u8c61\u662f\u7b49\u4ef7\u7684\u3002<\/p>\n<p><strong>\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/p>\n<p>\u6709\u65f6\u5019\uff0c\u5728\u4f7f\u7528ko.mapping.fromJS\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u6709\u5fc5\u8981\u53bb\u4f7f\u7528mapping\u7684\u9ad8\u7ea7\u7528\u6cd5\u6765\u5b9a\u4e49mapping\u7684\u8be6\u7ec6\u8fc7\u7a0b\uff0c\u4ee5\u540e\u5b9a\u4e49\u4e86\uff0c\u4ee5\u540e\u518d\u8c03\u7528\u7684\u65f6\u5019\u5c31\u4e0d\u5fc5\u518d\u5b9a\u4e49\u4e86\u3002\u8fd9\u91cc\u6709\u4e00\u4e9b\u60c5\u5f62\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u8fd9\u4e9boption\u3002<\/p>\n<p><strong>\u7528\u6cd51\uff1a\u4f7f\u7528keys\u6765\u4f7f\u5bf9\u8c61unique\u5316<\/strong><\/p>\n<p>\u4f60\u6709\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>var data = {\n    name: 'Scot',\n    children: [\n        { id: 1, name: 'Alicw' }\n    ]\n}<\/code><\/pre>\n<p>\u4f7f\u7528map\u63d2\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83map\u5230view model\u4e0a\uff08\u6ca1\u4efb\u4f55\u95ee\u9898\uff09\uff1a<\/p>\n<pre><code>var viewModel = ko.mapping.fromJS(data);<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6570\u636e\u88ab\u66f4\u65b0\u6210\u5982\u4e0b\u8fd9\u6837\uff1a<\/p>\n<pre><code>var data = {\n    name: 'Scott',\n    children: [\n        { id: 1, name: 'Alice' }\n    ]\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u53d1\u751f\u4e86\u4e24\u4ef6\u4e8b\uff1aname\u4eceScot\u53d8\u6210\u4e86Scott\uff0cchildren[0].name\u4eceAlicw\u53d8\u6210\u4e86Alice\u3002\u4f60\u53ef\u4ee5\u7528\u5982\u4e0b\u4ee3\u7801\u66f4\u65b0view model\uff1a<\/p>\n<pre><code>ko.mapping.fromJS(data, viewModel);<\/code><\/pre>\n<p>\u4e8e\u662f\uff0cname\u50cf\u6211\u4eec\u671f\u671b\u7684\u4e00\u6837\u66f4\u65b0\u4e86\uff0c\u4f46\u662f\u5728children\u6570\u7ec4\u91cc\uff0c\u5b50\u9879Alicw\u88ab\u5220\u9664\u800c\u65b0\u9879Alice\u88ab\u6dfb\u52a0\u5230\u6570\u7ec4\u91cc\u3002\u8fd9\u4e0d\u662f\u6211\u4eec\u6240\u671f\u671b\u7684\uff0c\u6211\u4eec\u671f\u671b\u7684\u662f\u53ea\u662f\u628aname\u4eceAlicw\u66f4\u65b0\u6210Alice\uff0c\u4e0d\u662f\u66ff\u6362\u6574\u4e2aitem\u9879\u3002\u53d1\u751f\u7684\u539f\u56e0\u662f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bmapping plugin\u63d2\u4ef6\u53ea\u662f\u7b80\u5355\u5730\u6bd4\u8f83\u6570\u7ec4\u91cc\u7684\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002 \u56e0\u4e3aJavaScript\u91cc{ id : 1, name : 'Alicw' }\u548c{ id : 1, name : 'Alice' }\u662f\u4e0d\u76f8\u7b49\u7684\uff0c\u6240\u4ee5\u5b83\u8ba4\u4e3a\u559c\u6b22\u5c06\u65b0\u9879\u66ff\u6362\u6389\u8001\u9879\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u9700\u8981\u58f0\u660e\u4e00\u4e2akey\u8ba9mapping\u63d2\u4ef6\u4f7f\u7528\uff0c\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u65b0\u5bf9\u8c61\u8fd8\u662f\u65e7\u5bf9\u8c61\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>var mapping = {\n    'children': {\n        key: function (data) {\n            return ko.utils.unwrapObservable(data.id);\n        }\n    }\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6bcf\u6b21map\u7684\u65f6\u5019\uff0cmapping\u63d2\u4ef6\u90fd\u4f1a\u68c0\u67e5\u6570\u7ec4\u9879\u7684id\u5c5e\u6027\u6765\u5224\u65ad\u8fd9\u4e2a\u6570\u7ec4\u9879\u662f\u9700\u8981\u5408\u5e76\u7684\u8fd8\u662f\u5168\u65b0replace\u7684\u3002<\/p>\n<p><strong>\u7528\u6cd52\uff1a\u7528create\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u6784\u9020\u5668<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u81ea\u5df1\u63a7\u5236mapping\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528create\u56de\u8c03\u3002\u4f7f\u7528\u56de\u8c03\u53ef\u4ee5\u8ba9\u4f60\u81ea\u5df1\u63a7\u5236mapping\u3002<\/p>\n<p>\u4e3e\u4f8b\uff0c\u4f60\u6709\u4e00\u4e2a\u50cf\u8fd9\u6837\u7684JavaScript\u5bf9\u8c61\uff1a<\/p>\n<pre><code>var data = {\n    name: 'Graham',\n    children: [\n        { id: 1, name: 'Lisa' }\n    ]\n}<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u81ea\u5df1map children \u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a<\/p>\n<pre><code>var mapping = {\n    'children': {\n        create: function (options) {\n            return new myChildModel(options.data);\n        }\n    }\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);<\/code><\/pre>\n<p>\u652f\u6301create\u56de\u8c03\u7684options\u53c2\u6570\u662f\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5305\u542b\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>data\uff1a JavaScript\u5bf9\u8c61\uff0c\u5305\u542bchild\u7528\u5230\u7684\u6570\u636e<\/li>\n<li>parent\uff1achild\u5bf9\u8c61\u6240\u5c5e\u7684\u7236\u5bf9\u8c61\u6216\u8005\u6570\u7ec4<\/li>\n<\/ul>\n<p>&nbsp;\u5f53\u7136\uff0c\u5728\u5185\u90e8\u7684create\u56de\u8c03\u91cc\uff0c\u4f60\u4e5f\u53ef\u4ee5\u518d\u6b21\u8c03\u7528ko.mapping.fromJS\u3002\u4e00\u4e2a\u4f8b\u5b50\u5c31\u662f\uff1a\u5982\u679c\u4f60\u60f3\u8ba9\u521d\u59cb\u7684JavaScript\u5bf9\u8c61\u5e26\u6709\u989d\u5916\u7684\u4f9d\u8d56\u5c5e\u6027dependent observables\uff1a<\/p>\n<pre><code>var myChildModel = function (data) {\n    ko.mapping.fromJS(data, {}, this); \n\n    this.nameLength = ko.dependentObservable(function () {\n        return this.name().length;\n    }, this);\n}<\/code><\/pre>\n<p><strong>\u7528\u6cd53\uff1a\u7528update\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684updating<\/strong><\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528update \u56de\u8c03\u6765\u81ea\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u5982\u4f55\u66f4\u65b0\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u9700\u8981\u66ff\u4ee3\u7684\u5bf9\u8c61\u4ee5\u53ca\u548ccreate \u56de\u8c03\u4e00\u6837\u7684options\u53c2\u6570\uff0c\u4f60\u5e94\u8be5return\u66f4\u65b0\u540e\u7684\u503c\u3002<\/p>\n<p>update \u56de\u8c03\u4f7f\u7528\u7684options\u53c2\u6570\u662f\u4e00\u4e2aJavaScript\u5bf9\u8c61\uff0c\u5305\u542b\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>data\uff1aJavaScript\u5bf9\u8c61\uff0c\u5305\u542bchild\u7528\u5230\u7684\u6570\u636e<\/li>\n<li>parent\uff1achild\u5bf9\u8c61\u6240\u5c5e\u7684\u7236\u5bf9\u8c61\u6216\u8005\u6570\u7ec4<\/li>\n<li>observable\uff1a\u5982\u679c\u5c5e\u6027\u662fobservable\u7684\uff0c\u8fd9\u5c06\u4f1a\u5199\u5165\u5230\u5b9e\u9645\u7684observable\u91cc<\/li>\n<\/ul>\n<p>&nbsp;\u4f8b\u5b50\uff0c\u5728\u6570\u636e\u663e\u793a\u4e4b\u524d\uff0c\u5728\u65b0\u6570\u636e\u540e\u9762\u9644\u52a0\u989d\u5916\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>var data = {\n    name: 'Graham',\n} \n\nvar mapping = {\n    'name': {\n        update: function(options) {\n            return options.data + 'foo!';\n        }\n    }\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);\nalert(viewModel.name());<\/code><\/pre>\n<p>alert\u7684\u7ed3\u679c\u662f\uff1aGrahamfoo!\u3002<\/p>\n<p><strong>\u7528\u6cd54\uff1a\u4f7f\u7528ignore\u5ffd\u7565\u4e0d\u9700\u8981map\u7684\u5c5e\u6027<\/strong><\/p>\n<p>\u5982\u679c\u5728map\u7684\u65f6\u5019\uff0c\u4f60\u60f3\u5ffd\u7565\u4e00\u4e9b\u5c5e\u6027\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528ignore\u7d2f\u58f0\u660e\u9700\u8981\u5ffd\u7565\u7684\u5c5e\u6027\u540d\u79f0\u96c6\u5408\uff1a<\/p>\n<pre><code>var mapping = {\n    'ignore': [\"propertyToIgnore\", \"alsoIgnoreThis\"]\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);<\/code><\/pre>\n<p>\u4f60\u58f0\u660e\u7684\u5ffd\u7565\u6570\u7ec4\u88ab\u7f16\u8bd1\u5230\u9ed8\u8ba4\u7684ignore\u6570\u7ec4\u91cc\u3002\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u4ee3\u7801\u4e00\u6837\u6765\u7ef4\u62a4\u5b83\uff1a<\/p>\n<pre><code>var oldOptions = ko.mapping.defaultOptions().ignore;\nko.mapping.defaultOptions().ignore = [\"alwaysIgnoreThis\"];<\/code><\/pre>\n<p><strong>\u7528\u6cd55\uff1a\u4f7f\u7528include\u58f0\u660e\u9700\u8981map\u7684\u5c5e\u6027<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53map\u4f60\u7684view model\u56de\u5230JS\u5bf9\u8c61\u662f\u65f6\u5019\uff0c\u53eamap\u539f\u59cbview model\u91cc\u62e5\u6709\u7684\u5c5e\u6027\uff08\u9664\u4e86\u4f8b\u5916\u7684_destroy\u5c5e\u6027\uff09\uff0c\u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528include\u53c2\u6570\u6765\u5b9a\u5236\uff1a<\/p>\n<pre><code>var mapping = {\n    'include': [\"propertyToInclude\", \"alsoIncludeThis\"]\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);<\/code><\/pre>\n<p>\u4f60\u58f0\u660e\u7684include\u6570\u7ec4\u88ab\u7f16\u8bd1\u5230\u9ed8\u8ba4\u7684include\u6570\u7ec4\u91cc\uff0c\u9ed8\u8ba4\u53ea\u6709_destroy\u3002 \u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u6765\u7ef4\u62a4\uff1a<\/p>\n<pre><code>var oldOptions = ko.mapping.defaultOptions().include;\nko.mapping.defaultOptions().include = [\"alwaysIncludeThis\"];<\/code><\/pre>\n<p><strong>\u7528\u6cd56\uff1a\u4f7f\u7528copy\u6765\u590d\u5236\u5c5e\u6027<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cmap\u7684\u65f6\u5019\u662f\u628a\u6240\u6709\u7684\u503c\u90fd\u8f6c\u6362\u6210observable\u7684\uff0c\u5982\u679c\u4f60\u53ea\u662f\u60f3copy\u5c5e\u6027\u503c\u800c\u4e0d\u662f\u66ff\u6362\u6210observable\u7684\uff0c\u4f60\u53ef\u4ee5\u5c06\u5c5e\u6027\u540d\u79f0\u6dfb\u52a0\u5230copy\u6570\u7ec4\uff1a<\/p>\n<pre><code>var mapping = {\n    'copy': [\"propertyToCopy\"]\n}\n\nvar viewModel = ko.mapping.fromJS(data, mapping);<\/code><\/pre>\n<p>\u4f60\u58f0\u660e\u7684copy\u6570\u7ec4\u88ab\u7f16\u8bd1\u5230\u9ed8\u8ba4\u7684copy\u6570\u7ec4\u91cc\uff0c\u9ed8\u8ba4\u503c\u662f\u7a7a\u3002\u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u6765\u7ef4\u62a4\uff1a<\/p>\n<pre><code>var oldOptions = ko.mapping.defaultOptions().copy;\nko.mapping.defaultOptions().copy = [\"alwaysCopyThis\"];<\/code><\/pre>\n<p><strong>\u7528\u6cd57\uff1aSpecifying the update target<\/strong><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u60f3\u518d\u4e00\u4e2aclass\u5185map\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f5c\u4e3a\u64cd\u4f5c\u7684\u76ee\u6807\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>ko.mapping.fromJS(data, {}, someObject); \/\/ overwrites properties on someObject<\/code><\/pre>\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u60f3map\u4e00\u4e2aJavaScript\u5bf9\u8c61\u5230this\u4e0a\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\uff1a<\/p>\n<pre><code>ko.mapping.fromJS(data, {}, this);<\/code><\/pre>\n<p><strong>\u4ece\u591a\u6570\u636e\u6e90map<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u6b21\u4f7f\u7528ko.mapping.fromJS \u6765\u5c06\u591a\u4e2aJS\u5bf9\u8c61\u7684\u6570\u636e\u6e90map\u5230\u4e00\u8d77\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>var viewModel = ko.mapping.fromJS(alice, aliceMappingOptions);ko.mapping.fromJS(bob, bobMappingOptions, viewModel);<\/code><\/pre>\n<p>\u4f60\u58f0\u660e\u7684mapping\u9009\u9879option\u5728\u6bcf\u6b21\u8c03\u7528\u7684\u65f6\u5019\u90fd\u4f1a\u5408\u5e76\u3002<\/p>\n<p><strong>Map<\/strong>\u4ee5\u540e\u7684observable\u6570\u7ec4<\/p>\n<p>map\u63d2\u4ef6map\u4ee5\u540e\u751f\u4ea7\u7684observable\u6570\u7ec4\uff0c\u5e26\u6709\u51e0\u4e2a\u989d\u5916\u7684\u51fd\u6570\u6765\u5904\u7406\u5e26\u6709keys\u7684mapping\uff1a<\/p>\n<ul>\n<li>mappedRemove<\/li>\n<li>mappedRemoveAll<\/li>\n<li>mappedDestroy<\/li>\n<li>mappedDestroyAll<\/li>\n<li>mappedIndexOf<\/li>\n<\/ul>\n<p>&nbsp;\u5b83\u4eec\u662f\u548cko.observableArray\u91cc\u7684\u51fd\u6570\u7b49\u4ef7\u7684\uff0c\u4e0d\u540c\u662f\u4ed6\u4eec\u901a\u8fc7key\u6765\u5904\u7406\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>var obj = [\n    { id: 1 },\n    { id: 2 }\n]\n\nvar result = ko.mapping.fromJS(obj, {\n    key: function (item) {\n        return ko.utils.unwrapObservable(item.id);\n    }\n});\n\nresult.mappedRemove({ id: 2 });<\/code><\/pre>\n<p>map\u8fc7\u7684observable\u6570\u7ec4\uff0c\u9664\u4e86\u4e0a\u9762\u7684\u51fd\u6570\u8fd8\u652f\u6301\u4e00\u4e2amappedCreate\u51fd\u6570\uff1a<\/p>\n<pre><code>var newItem = result.mappedCreate({ id: 3 });<\/code><\/pre>\n<p>\u9996\u5148\u4f1a\u68c0\u67e5key\uff08id=3\uff09\u5728\u6570\u7ec4\u91cc\u662f\u5426\u5b58\u5728\uff08\u5982\u679c\u5b58\u5728\u5219\u629b\u51fa\u5f02\u5e38\uff09\uff0c\u7136\u540e\uff0c\u5982\u679c\u6709create\u548c update\u56de\u8c03\u7684\u8bdd\u4f1a\u8c03\u7528\u4ed6\u4eec\uff0c\u6700\u540e\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u5e76\u5c06\u65b0\u5bf9\u8c61\u6dfb\u52a0\u5230\u6570\u7ec4\u7136\u540e\u8fd4\u56de\u8be5\u65b0\u5bf9\u8c61\u3002<br \/><strong>\u4e0b\u8f7d<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Version 2.0&nbsp;\uff08\u6700\u5c0f\u7248\u672c8.6kb\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mapping\u63d2\u4ef6 Knockout\u8bbe\u8ba1\u6210\u5141\u8bb8\u4f60\u4f7f\u7528\u4efb\u4f55JavaScript\u5bf9\u8c61\u4f5c\u4e3aview model\u3002\u5fc5\u987b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-191171","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=191171"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/191171\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=191171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=191171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=191171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}