我一直在研究和學習以下微框架Cube https://github.com/xirsys/cube以用於我的Haxe項目。如何使用Cube框架動態地將視圖動態映射到Haxe中的視圖
我發現的例子非常有幫助,但我遇到過的一些例子沒有我想要弄清楚的是在運行時註冊並將視圖映射到介體。
我想我很近,但它似乎並沒有工作。下面是我的AppContext的樣子。
class AppContext extends Agent<Dynamic, IEvent>
{
public function new(container: Dynamic, autoStart:Bool)
{
super(container, autoStart);
}
override public function initiate()
{
mediatorMap.mapView(Main, MainMediator);
mediatorMap.mapView(Welcome, WelcomeMediator);
injector.mapSingleton(AppModel, container);
dispatch(AgentEvent.STARTUP_COMPLETE, null);
}
}
這裏是何等的主視圖加載後會在我的我的主要觀點現在
class Main extends Sprite
{
public var agent: AppContext;
@Inject
public var dm:AppModel;
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAdded);
haxe.Log.setColor(0xffffff);
this.name = "main view";
}
public static function main(): Void
{
Lib.current.addChild(new Blastroidz());
}
private function onAdded(e:Event):Void
{
agent = new AppContext(this, false);
agent.addEventHandler(AgentEvent.STARTUP_COMPLETE, onContextStart);
agent.initiate();
}
private function onContextStart(evt:IEvent):Void
{
agent.mediatorMap.createMediator(this);
}
}
在我的主要觀點中保我創建了「歡迎」的觀點,然後我想創建它的介體並像這樣使用它。
class MainMediator extends xirsys.cube.mvcs.Mediator
{
@Inject
public var view:Main;
public var welcome:Welcome;
private var viewManager:NMEViewManager;
override public function onRegister()
{
super.onRegister();
eventDispatcher.addEventHandler(AppEvent.SET_NEW_VIEW, changeViewHandler);
initView();
}
private function initView():Void
{
viewManager = new NMEViewManager(view);
welcome = new Welcome();
viewManager.setView(welcome, NMEViewManager.FADE);
///// this is the important line that doesn't seem to work \\\\\\
mediatorMap.createMediator(welcome);
}
private function changeViewHandler(e:String):Void
{
trace("change view");
}
我歡迎中介看起來像這樣和onRegister似乎不火,我完全不出現任何錯誤....注意viewManager處理的addChild的歡迎觀的主要觀點和這似乎工作得很好。
class WelcomeMediator extends xirsys.cube.mvcs.Mediator
{
@Inject
public var view:Welcome;
override public function onRegister()
{
super.onRegister();
view.addEventListener(Event.COMPLETE, onCompleteHandler);
trace("welcome mediator registered");
}
private function onCompleteHandler(e:Event):Void
{
trace("logo complete");
var event:AppEvent = new AppEvent(AppEvent.SET_NEW_VIEW);
event.view_name = AppModel.MAIN;
this.eventDispatcher.dispatch(AppEvent.SET_NEW_VIEW, event);
}
}
}