![]() |
|
||||||||||
|
|||||
|
Регистрация: Apr 2014
Сообщений: 97
|
Итак, недавно, дабы освоить шаблон MVC, я делал с ним кнопку, но как мне тут подсказали, кнопка - это чистое вьюв и для него MVC не нужно. По этому кнопку и переделал в один класс.
Теперь я сделал меню сайта с помощью шаблона проектирования MVC. Документ-класс: package net.freedomstate.elements { /** * ... * @author Scorpion */ import flash.display.Sprite; import net.freedomstate.elements.menu.View; public class Menu extends Sprite { private var _view: View; public function Menu(): void { _view = new View; addChild(_view); } } } Классы MVC: package net.freedomstate.elements.menu { /** * ... * @author Scorpion */ import flash.display.Bitmap; import flash.display.Sprite; import flash.events.*; import net.freedomstate.elements.Butt; public class View extends Sprite { private var _arrayButtons: Array = [ ]; private var _model: Model; private var _controller: Controller; private var _currentPage: Sprite; public function View(): void { _model = new Model(); _controller = new Controller(_model); _model.addEventListener(Event.CHANGE, viewPage); viewMenu(); viewPage(); } private function addButton(text: String, page: String): void { if (_arrayButtons.length == 0) _arrayButtons[_arrayButtons.length] = new Butt(175, 200, text) else _arrayButtons[_arrayButtons.length] = new Butt(_arrayButtons[_arrayButtons.length - 1].x + _arrayButtons[_arrayButtons.length - 1].width + 5, 200, text); addChild(_arrayButtons[_arrayButtons.length - 1]); _arrayButtons[_arrayButtons.length - 1].name = page; _arrayButtons[_arrayButtons.length - 1].addEventListener(MouseEvent.CLICK, clickButton); } private function clickButton(e: Event): void { _controller.dispatchEvent(new DataEvent(Event.ADDED_TO_STAGE, false, false, e.target.name)); } private function viewMenu(): void { switch(_model.dostype) { case "guest": guestMenu(); break; case "user": userMenu(); break; } } private function viewPage(e: Event = null): void { if (_currentPage) { removeChild(_currentPage); _currentPage = null } _currentPage = new _model.currentPage; addChild(_currentPage); } private function guestMenu(): void { deleteButtonds() addButton("Правила", "Description"); addButton("Карта", "Map"); addButton("Органи влади", "Authorities"); addButton("Підприємства", "Enterprises"); addButton("Реєстрація", "Registration"); addButton("Вхід", "Input"); } private function userMenu(): void { deleteButtonds() addButton("Правила", "Description"); addButton("Карта", "Map"); addButton("Органи влади", "Authorities"); addButton("Підприємства", "Enterprises"); addButton("Профіль", "Profile"); addButton("Вхід", "Input"); } private function deleteButtonds(): void { for (var i: int = 0; i < _arrayButtons.length; i++ ) { if (_arrayButtons[i]) { removeChild(_arrayButtons[i]); _arrayButtons[i] = null; } } } } } package net.freedomstate.elements.menu { /** * ... * @author Scorpion */ import flash.events.*; import net.freedomstate.pages.*; public class Controller extends EventDispatcher { private var _model: Model; public function Controller(rModel: Model): void { _model = rModel; this.addEventListener(Event.ADDED_TO_STAGE, addPage); } private function addPage(e: DataEvent): void { switch (e.data) { case "Description": _model.currentPage = Description; break; case "Authorities": _model.currentPage = Authorities; break; case "Enterprises": _model.currentPage = Enterprises; break; case "Registration": _model.currentPage = Registration; break; case "Input": _model.currentPage = Input; break; case "Map": _model.currentPage = Map; break; case "Profile": _model.currentPage = Profile; break; } _model.dispatchEvent(new DataEvent(Event.ADDED_TO_STAGE)); } } } package net.freedomstate.elements.menu { /** * ... * @author Scorpion */ import flash.events.*; import net.freedomstate.pages.*; public class Model extends EventDispatcher { private var _dostype: String; private var _currentPage: Class; public function Model(): void { _dostype = "guest"; _currentPage = Description; this.addEventListener(Event.ADDED_TO_STAGE, addPage); } private function addPage(e: DataEvent): void { dispatchEvent(new Event(Event.CHANGE)); } public function get dostype(): String { return _dostype; } public function get currentPage(): Class { return _currentPage; } public function set currentPage(page: Class): void { _currentPage = page; } } } |
|
|||||
|
О какой гибкости вы говорите, имея это?
switch (e.data) { case "Description": _model.currentPage = Description; break; case "Authorities": _model.currentPage = Authorities; break; case "Enterprises": _model.currentPage = Enterprises; break; case "Registration": _model.currentPage = Registration; break; case "Input": _model.currentPage = Input; break; case "Map": _model.currentPage = Map; break; case "Profile": _model.currentPage = Profile; break; } Зачем вы лезете в MVC, ещё не поняв до конца суть ООП? Уже тошнит повторять одно и то же вам.
__________________
There is no thing in this world that is not simple. |
|
|||||
|
Регистрация: Apr 2014
Сообщений: 97
|
Ага, то есть надо написать класс Ивента, который будет не String с названием страницы передавать, а класс самой страницы, дабы не было свитча? Я правильно понял намек?
|
|
|||||
|
Попробуйте представить, что страниц ещё нет. Ни одной. Не написаны.
Сначала вы пишете меню, а потом больше не трогаете его код, а потом из главного класса добавляете все кнопки в нужном вам порядке.
__________________
There is no thing in this world that is not simple. |
|
|||||
|
[+4 06.05.14]
|
2misha - и тут тоже MVC не нужен, опять же стандартный вью. Максимум M+V. Не более. Я щас обстрагируюсь от того, что это меню и просто по коду буду править.
public class Menu extends Sprite { public function Menu(): void { const control:Controller = new Controller(this) } } public function Controller(host:Sprite): void { _host = host; _view.model = _model; _host.addChild(_view); } public function View(): void { addEventListener(Event.ADDED_TO_STAGE , onAdded); } private function onAdded(e:Event) :void { //app started } Теперь о бредовых ошибках : public function Controller(rModel: Model): void { _model = rModel; this.addEventListener(Event.ADDED_TO_STAGE, addPage); } public function Model(): void { _dostype = "guest"; _currentPage = Description; this.addEventListener(Event.ADDED_TO_STAGE, addPage); } Дальше просто не стал читать, пока разбиритесь со структурой
__________________
Марк Tween |
|
|||||
|
[+4 06.05.14]
|
Цитата:
__________________
Марк Tween |
![]() |
![]() |
Часовой пояс GMT +4, время: 12:27. |
|
|
« Предыдущая тема | Следующая тема » |
| Опции темы | |
| Опции просмотра | |
|
|