对于swiz1.0框架的一点理解

Swiz的核心是个简单的控制反转框架。借助于IoC框架,应用组件(例如视图)无需实例化其依赖(所用的对象),甚至都不用查找。在组件创建时框架会注入这些依赖(因此术语依赖注入也用来表示这种行为)并产生松耦合且可重用性更好的组件。

Swiz IoC Framework所管理的组件叫做bean

 

Swiz 早期版本的注入方式:

[Autowire(bean="contactService")]

public var contactService:RemoteObject;

我采用的注入方式为:

[Inject(source=” contactService”)]

public var contactService:RemoteObject;

可以把bean注入到controllerservicesview或其他组件中。

 

例如我在CalcController中编写:

[Bindable]

public var surebetView:SurebetView;

我在其他的as/mxml,如果想同步获得或者更改该对象的值,可以在as/mxml中编写:

[Bindable]

[Inject(source= "calcController.surebetView",bind="true")]

public var surebetView:SurebetView;

参数:source 代表你注入bean的位置,blind代表是否绑定,至于绑定的意思,大家应该很明白。

 

swiz 中,时间触发后,swiz controller中控制事件的捕获,但是前提是发出的事件的冒泡功能要打开,这样swizcontroller中使用[EventHandler]可以捕获到该事件。

as/mxml中,触发事件的方式为:

1、在继承与UIComponent的组件:

var e:CalcEvent=new CalcEvent(CalcEvent.CALC_BLANCEMODE);

e.total=total;

e.arrayData=arrayData;

dispatchEvent(e);

2、非继承与UIComponent的组件:

 必须先声明:

 [dispatcher]

Public var dispatcher:EventDispatcher;

 

然后调用:

dispatcher.dispatchEvent(e);

 

本人在实践当中发现,对与AIR环境中NativeMenu,没有生效,报错为dispatcher为空。

 

例如:

[EventHandler(event="CalcEvent.CALC_GAME", properties="surebetView")]

public function commonCalcHandler(surebetView:SurebetView):void{}

参数:

Event:事件名称;

properties 为该事件传递过来的对象,该对象必须在CalcEvent中定义,传递过来的名字必须和定义时相同。

Scope:事件作用域:local,global

 

可以用多个[EventHandler]作用于同一个方法:

[EventHandler(event="CalcEvent.CALC_GAME", properties="surebetView")]

[EventHandler(event="CalcEvent.CALC_G", properties="surebetView")]

[EventHandler(event="CalcEvent.CALC_M", properties="surebetView")]

public function commonCalcHandler(surebetView:SurebetView):void{}

这样任何一个时间触发,都会调用该commonCalcHandler方法。

 

 

Swiz 有一个ServiceHelper辅助类,该辅助类在[EventHandler]方法commonCalcHandler()中使用,该类使用如下:serviceHelper.executeServiceCall(arg1,resultHandler,onfault);

使用该类可以代替addEventListener(),来实现异步的侦听和调用,对于该类是否在后台很好的处理了侦听器表示怀疑,通常情况下我们addEventListener()后,在后面的侦听方法执行完毕后要手动removeEventListener,这样便于更好的回收虚拟机内存,由于event是打开了冒泡功能的,所以我建议使用swiz 的这种方式:serviceHelper.executeServiceCall()

这样可以有利于更好的产生service层。

 

 

Chainning API

Chainning 可以使一组事件顺序执行,而不需要我们手动触发控制。

public   doEventChain() :  function void

{

   eventChain : EventChain =   EventChain( dispatcher ); var new

 eventChain.addEventListener( ChainEvent.CHAIN_COMPLETE, handleChainComplete, false, 0, true );

 eventChain.addStep(   EventChainStep(   UserEvent( UserEvent.USER_SCREEN_ACTIVE ) ) ); new new

 eventChain.addStep(   EventChainStep(   UserEvent( UserEvent.USER_PROCESSING_COMPLETE ) ) ); new new

 eventChain.start();

}

[EventHandler( event="UserEvent.USER_SCREEN_ACTIVE" )]

public   logUserScreenActive() :  function void

{

 // perform logic or dispatch events when the user screen is active

}

[EventHandler( event="UserEvent.USER_PROCESSING_COMPLETE" )]

public   logUserProcessingComplete() :  function void

{

 // perform logic or dispatch events when the processing finishes

}

public   handleChainComplete( event :   ) :  function Event void

{

 // perform logic or dispatch events when the chain is complete

}

以上是自己使用swiz 的一点心得,swiz 的短小精悍,个人比较赞赏,不错。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值