{"id":177,"date":"2019-10-31T23:02:56","date_gmt":"2019-10-31T15:02:56","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=177"},"modified":"2020-06-20T23:22:20","modified_gmt":"2020-06-20T15:22:20","slug":"ecmascript-mvc%e6%a8%a1%e5%bc%8f%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2019\/10\/31\/ecmascript-mvc%e6%a8%a1%e5%bc%8f%e7%ae%80%e4%bb%8b\/","title":{"rendered":"ECMAScript MVC\u6a21\u5f0f\u7b80\u4ecb"},"content":{"rendered":"<hr \/>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>MVC\u662f\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\u7684\u7f29\u5199\u3002\u89c6\u56fe\u548c\u7528\u6237\u4ea4\u4e92\uff0c\u901a\u8fc7\u4e8b\u4ef6\u5bfc\u81f4\u63a7\u5236\u5668\u6539\u53d8\uff1b \u63a7\u5236\u5668\u6539\u53d8\u5bfc\u81f4\u6a21\u578b\u6539\u53d8\uff0c\u6216\u8005\u63a7\u5236\u5668\u540c\u65f6\u6539\u53d8\u4e24\u8005\uff1b\u6a21\u578b\u6539\u53d8\u5bfc\u81f4\u89c6\u56fe\u6539\u53d8\uff0c\u6216\u8005\u89c6\u56fe\u6539\u53d8\uff0c\u6f5c\u5728\u7684\u4ece\u6a21\u578b\u91cc\u9762\u83b7\u5f97\u53c2\u6570\u6765\u6539\u53d8\u81ea\u5df1\u3002\u5b83\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u6a21\u578b\uff0c\u4e00\u79cd\u7f16\u7a0b\u601d\u7ef4\u7ec4\u7ec7\u65b9\u5f0f\u3002<\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<h4>\u6a21\u578bmodal<\/h4>\n<p>\u8fd9\u91cc\u4f7f\u7528\u591a\u5e74\u524d\u7684\u793a\u4f8b\u505a\u6f14\u793a\u3002<\/p>\n<p><em>\u5176\u4ed6ECMASCRIPT\u6807\u51c6\u6216\u5b9e\u73b0\uff08\u5982Typescript\u3001Coffescript\u7b49\uff09\u540c\u7406\uff0c\u4e0d\u518d\u8d58\u8ff0<\/em><\/p>\n<pre><code class=\"language-javascript line-numbers\">package {\n    \/\/\u5bfc\u5165\u76f8\u5173\u4f9d\u8d56 \u5982\n    import events.Event;\n    import events.EventDispatcher;\n    \/\/import ......\n\n    public class Model extends EventDispatcher {\n        private var hour:String;\n        private var minute:String;\n        private var second:String;\n\n        \/\/\u6784\u9020\u7f6e\u7a7a\n        public function Model() {\n        }\n\n        public function get hour():String {\n            return hour;\n        }\n\n        public function set hour(value:String):void {\n            dispatchEvent(new Event(\"setHour\"));\n            hour = value;\n        }\n\n        public function get minute():String {\n            return minute;\n        }\n\n        public function set minute(value:String):void {\n            dispatchEvent(new Event(\"setMinute\"));\n            minute = value;\n        }\n\n        public function get second():String {\n            return second;\n        }\n\n        public function set second(value:String):void {\n            dispatchEvent(new Event(\"setSecond\"));\n            second = value;\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u6784\u9020\u7f6e\u7a7a\u7684Model\uff0c\u901a\u8fc7getter\u548csetter\u83b7\u53d6Model\u7684\u79c1\u6709\u5c5e\u6027\u3002\u7136\u540e\u662fView\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">package {\n\n    public class View extends Sprite {\n        private var txt:TextField;\n        private var model:Model;\n        private var control:Control;\n\n        public function View(model:Model, control:Control) {\n            model = model;\n            control = control;\n\n            txt = new TextField()\n            addChild(txt);\n            model.addEventListener(\"setHour\", setHandler);\n            model.addEventListener(\"setMinute\", setHandler);\n            model.addEventListener(\"setSecond\", setHandler);\n            control.starUp();\n        }\n\n        private function setHandler(e:Event):void {\n            txt.text = `${model.hour}:${model.minute}:${model.second}`;\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>\u4e4b\u540e\uff0c\u7528control\u63a7\u5236View\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">package {\n\n    public class Control {\n        private var model:Model;\n        private var hour:String;\n        private var minute:String;\n        private var second:String;\n        private var timer:Timer = new Timer(1000);\n\n        public function Control(model:Model) {\n            model = model;\n        }\n\n        public function starUp():void {\n            timer.start();\n            timer.addEventListener(TimerEvent.TIMER, onTimer);\n        }\n\n        private function onTimer(e:TimerEvent):void {\n            var date:Date = new Date();\n            model.hour = date.getHours() &lt; 9 ? \"0\" + date.getHours() : String(date.getHours());\n            model.minute = date.getMinutes() &lt; 9 ? \"0\" + date.getMinutes() : String(date.getMinutes());\n            model.second = date.getSeconds() &lt; 9 ? \"0\" + date.getSeconds() : String(date.getSeconds());\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>\u7531\u4e8e\u5b83\u662f\u6570\u636e\u9a71\u52a8\u7684\uff0c\u660e\u663e\u7684\u5c42\u6b21\uff0c\u903b\u8f91\u662f\u903b\u8f91\uff0c\u8868\u73b0\u662f\u8868\u73b0\uff0c\u968f\u7740\u9879\u76ee\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u8fd9\u79cd\u6a21\u5f0f\u5bf9\u9879\u76ee\u7684\u53ef\u63a7\u6027\u3001\u7ef4\u62a4\u6027\u548c\u6548\u7387\u90fd\u662f\u5f88\u597d\u7684\u652f\u6301\u3002\u4e00\u822c\u662f\u4e2d\u5927\u578b\u9879\u76ee\u5fc5\u5907\u9009\u62e9\u3002<\/p>\n<p>code enjoy! \ud83d\ude04\ud83d\ude04\ud83d\ude01<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.cc\/\">https:\/\/indeex.cc<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd MVC\u662f\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\u7684\u7f29\u5199\u3002\u89c6\u56fe\u548c\u7528\u6237\u4ea4\u4e92\uff0c\u901a\u8fc7\u4e8b\u4ef6\u5bfc\u81f4\u63a7\u5236\u5668\u6539\u53d8\uff1b \u63a7\u5236\u5668\u6539\u53d8\u5bfc\u81f4\u6a21\u578b<a href=\"https:\/\/blog.indeex.club\/index.php\/2019\/10\/31\/ecmascript-mvc%e6%a8%a1%e5%bc%8f%e7%ae%80%e4%bb%8b\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/177"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/177\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}