{"id":5300,"date":"2016-09-08T17:56:45","date_gmt":"2016-09-08T09:56:45","guid":{"rendered":"https:\/\/sdeno.com\/?p=5300"},"modified":"2016-09-08T17:56:45","modified_gmt":"2016-09-08T09:56:45","slug":"avalon%e7%9b%91%e5%90%ac%e5%9b%9e%e8%b0%83-watch","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5300","title":{"rendered":"avalon\u76d1\u542c\u56de\u8c03 $watch"},"content":{"rendered":"<p>$watch\u662favalon\u91cd\u8981\u53c8\u5b9e\u7528\u7684\u5c5e\u6027\uff0c\u6b64\u65b9\u6cd5\u662f\u7528\u4e8e\u76d1\u542cvm\u4e2d\u7684\u5bf9\u8c61\u7684\u5c5e\u6027\u53d8\u5316\u3002<\/p>\n<p>\u76d1\u542c\u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff0c \u7b2c\u4e00\u4e2a\u662f\u65b0\u503c\uff0c \u7b2c\u4e8c\u4e2a\u662f\u65e7\u503c\uff0c \u7b2c\u4e09\u4e2a\u662f\u53d1\u751f\u53d8\u52a8\u7684\u5c5e\u6027\u7684\u540d\u5b57\u3002<\/p>\n<p>$watch\u65b9\u6cd5\u4f9b\u4e0e\u5176\u4ed6\u64cd\u4f5cDOM\u7684\u5e93\u4e00\u8d77\u4f7f\u7528\u7684,\u5982\u5bcc\u6587\u672c\u7f16\u8f91\u5668\u4ec0\u4e48. \u5728$watch\u56de\u8c03\u91cc\u66f4\u65b0VM\u81ea\u8eab\u7684\u5c5e\u6027\u662f\u975e\u5e38\u5371\u9669\u7684\u4e8b,\u5f88\u5bb9\u6613\u5f15\u53d1\u6b7b\u5faa\u73af<\/p>\n<p>\u80fd\u76d1\u542c:<br \/>\n\u7b2c\u4e00\u5c42\u5c5e\u6027\u503c\u53d8\u5316;<br \/>\n\u6570\u7ec4\u957f\u5ea6\u53d8\u5316;<br \/>\n\u5bf9\u8c61\u5c5e\u6027\u5185\u5bb9\u53d8\u5316;<br \/>\n\u4e0d\u80fd\u76d1\u542c:<br \/>\n\u6570\u7ec4\u5185\u5bb9\u53d8\u5316;<br \/>\n\u5bf9\u8c61\u957f\u5ea6\u53d8\u5316<br \/>\n\u63a8\u8350\u4f7f\u7528\u6570\u636e\u683c\u5f0f\uff1a<\/p>\n<pre>[{a:123},{b:'\u4f60\u597d'}]<\/pre>\n<p>&nbsp;<\/p>\n<p>\u652f\u6301\u901a\u914d\u7b26*<\/p>\n<p>\u4e0b\u9762\u662f$watch\u65b9\u6cd5\u7684\u7684\u4e03\u79cd\u7528\u6cd5:<\/p>\n<pre>var vm = avalon.define({\r\n $id: \"test\",\r\n array: [1, 2, 3],\r\n d: 888,\r\n arr: [{\r\n     a: 1\r\n   }, {\r\n     a: 2\r\n   }, {\r\n     a: 3\r\n   }],\r\n obj: {\r\n   a: 1,\r\n   b: 2\r\n },\r\n a: {\r\n   b: {\r\n     c: {\r\n       d: 33\r\n     }\r\n    }\r\n }\r\n})\r\n\r\nvar expect = function (a) {\r\n  return {\r\n   to: {\r\n     be: function (b) {\r\n       console.log(a == b)\r\n     }\r\n   }\r\n  }\r\n}\r\n\r\nvm.$watch(\"array.length\", function (a, b, name) {\r\n  console.log('\u7b2c\u4e00\u7ec4 \u6570\u7ec4\u957f\u5ea6', name)\r\n})\r\nvm.$watch(\"arr.*.a\", function (a, b, name) {\r\n expect(a).to.be(99)\r\n expect(b).to.be(1)\r\n console.log('\u7b2c\u4e8c\u7ec4 \u6570\u7ec4\u5143\u7d20\u5c5e\u6027(\u6a21\u7cca\u5339\u914d, \u4e0d\u77e5\u9053\u54ea\u4e2a\u5143\u7d20\u53d8\u5316)', name)\r\n})\r\nvm.$watch(\"obj.a\", function (a, b, name) {\r\n expect(a).to.be(111)\r\n expect(b).to.be(1)\r\n console.log('\u7b2c\u4e09\u7ec4 \u5c5e\u6027\u7684\u5c5e\u6027', name)\r\n})\r\n\r\nvm.$watch(\"obj.*\", function (a, b, name) {\r\n expect(a).to.be(111)\r\n expect(b).to.be(1)\r\n console.log('\u7b2c\u56db\u7ec4 \u5c5e\u6027\u7684\u5c5e\u6027(\u6a21\u7cca\u5339\u914d)', name)\r\n})\r\n\r\nvm.$watch(\"a.b.c.d\", function (a, b, name) {\r\n expect(a).to.be(88)\r\n expect(b).to.be(33)\r\n console.log('\u7b2c\u4e94\u7ec4 \u5c5e\u6027\u7684\u5c5e\u6027\u7684\u5c5e\u6027', name)\r\n})\r\nvm.$watch(\"a.*.c.d\", function (a, b, name) {\r\n expect(a).to.be(88)\r\n expect(b).to.be(33)\r\n console.log('\u7b2c\u516d\u7ec4 \u5c5e\u6027\u7684\u5c5e\u6027\u7684\u5c5e\u6027(\u6a21\u7cca\u5339\u914d)', name)\r\n})\r\nvm.$watch(\"*\", function (a, b, name) {\r\n expect(a).to.be(999)\r\n expect(b).to.be(888)\r\n console.log('\u7b2c\u4e03\u7ec4 \u7b2c\u4e00\u5c42\u5bf9\u8c61\u7684\u4efb\u610f\u5c5e\u6027(\u6a21\u7cca\u5339\u914d)', name)\r\n})\r\nsetTimeout(function () {\r\n vm.array.set(1, 6)\r\n vm.array.push(99)\r\n vm.arr[0].a = 99\r\n vm.obj.a = 111\r\n vm.a.b.c.d = 88\r\n vm.d = 999\r\n}, 100)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u89e3\u9664\u76d1\u542c<\/p>\n<p>$watch\u65b9\u6cd5\u5c31\u662f\u8fd4\u56de\u4e86\u4e00\u4e2a\u51fd\u6570<\/p>\n<pre>var unwatch = vm.$watch(\"array.*\", function (a, b) {\r\n expect(a).to.be(6)\r\n expect(b).to.be(2)\r\n})\r\nunwatch() \/\/\u79fb\u9664\u5f53\u524d$watch\u56de\u8c03<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$watch\u662favalon\u91cd\u8981\u53c8\u5b9e\u7528\u7684\u5c5e\u6027\uff0c\u6b64\u65b9\u6cd5\u662f\u7528\u4e8e\u76d1\u542cvm\u4e2d\u7684\u5bf9\u8c61\u7684\u5c5e\u6027\u53d8\u5316\u3002 \u76d1\u542c\u51fd\u6570\u6709\u4e09\u4e2a\u53c2\u6570\uff0c \u7b2c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-5300","post","type-post","status-publish","format-standard","hentry","category-avalon-js"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5300"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5300\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}