[Flex] swiz 点滴

来源:互联网 发布:googleearth高程数据 编辑:程序博客网 时间:2024/05/17 02:28

* 一个完整的swiz 例子

http://www.cnblogs.com/shanlanjie/archive/2012/06/07/2540006.html


* Main issues

> flex access point is a mxml file under src root folder. 这个mxml会add view指向你的(swiz) view package里的其中一个view mxml作为main view. 而swiz config 代码可以写到这个main view xml里,也可以写到access point的app mxml file里。


> view里只应该有pm,不应该有model, controller & service/delegate. view or pm是通过dispatch event来通知controller的。而controller里dispatch的event通常由pm or view接收。


> pm的讲解见后面的部分。view只会和pm进行交互,我们也应该尽量把view的logic写到pm里,实在不行才写到view里。pm应该有一些data变量,它们要么绑定到model里。要么就是也把model定义为变量,然后定义event handler,这样就使得event发生后,就会把model里的data复制给pm里的data变量。


> model, 你可以设置model里的var是bindable的,也可以在var的setter里dispatch event来通知var value change。


> event, 自定义event,通过dispatch这些event来通知trigger。event通常会带一些变量(声明为public)用来存储。例如,一个search功能,我们创建一个search event class,该class有一个变量为"keyword",那么在pm里会创建一个search event实例,并从view ui里获取keyword值复制给event实例的变量,然后dispatch该event。controller里定义了该event的handler method,会从该event中获取keyword来调用delegate/service。event里的变量起到了传递值的作用。


> service/delegate 不应该有任何data变量 (e.g. model, pm), 会inject httpservice,它只是call httpservice的send method, 至于返回的result应该在controller里处理


> controller 通常会inject service/delegate, 也会inject model. controller里dispatch的event通常由pm or view接收


> 有些人喜欢在controller里调用delegate/service,然后赋值该model,在model的setter里dispatch event,有些人则喜欢在controller里调用了delegate之后直接在controller里dispatch event。不过还是要看情况,当然最应该是写在setter里,这样比较符合逻辑。比如call search service之后,会在controller里调用model的setter来赋值,这时setter method应该dispatch 一个 "..._MODEL_INFO_CHANGE"之类的event来通知event handler。而如果你不是在setter里,而是在controller里dispatch event,那么有可能其他controller也会修改该model,那么就要在所有修改model的地方都dispatch相同的event,就duplicate code了。当然也有些情况我觉得在controller里dispatch event比较好,比如login。




* Presentation Model.

classes in pm package are presentation model. 官方文档解释:pm的作用是为了remove any logic from the view and let the PM handle the view logic。由于view里没有logic,所以you only have to unit test your PM

而且pm还是是model class (in model package) 和 view之间的桥梁,象java 的dto,使得view只和present model交互,而不会直接和model交互。因此在view.mxml里只会看到presentation model的变量。在presentation model里基本上都会有一个model的变量或者把model的某些var inject到pm的var上如果不是把model的某些var inject到pm的var上,则可以添加一些event handler method,当trigger这些handler时,就会把model变量里的data传递给presentation model的变量(因为trigger时model已经被赋值了data)

Note: it is recommended  to declare pm  as a Prototype so the PM will only be created (instantiated) when the corresponding view is added to the stage. 例如

      <swiz:Prototype type="{MyPresentationModel}" />

我觉得swiz:Prototype还表示该bean不是singleton的。即每次把该pm bean inject时,都会create a new instance。如果你希望bean为singleton,而且又只是在view添加到stage之后才创建instance,那么可以使用

      <swiz:Prototype type="{MyPresentationModel}" singleton="true"/>

有可能是因为pm既inject to view,又inject to controller,那么需要设置singleton=true


ref link

http://swizframework.jira.com/wiki/display/SWIZ/Presentation+Model


* vo和model的关系

vo package里定义的class相当于java的pojo,和model不同的是它不会有dispatcher变量vo和model的关系的一个典型的例子是:model里有一个变量是array list,而array list的每一个item就是一个vo


Swiz guide 

official guide

http://swizframework.jira.com/wiki/display/SWIZ/Home

Recommend download swiz examples from https://github.com/swiz/swiz-examples, it include the Quick-start swiz example.


Swiz quick start example guide: http://swizframework.jira.com/wiki/display/SWIZ/Quick+Start


* swiz framework, it can
* Inversion of Control / Dependency Injection
* Event handling
* A simple life cycle for asynchronous remote method invocations
* A framework that is decoupled from your application code Compatibility with both Flex and non-Flex AS3 projects


很像java的spring framework.


* Q: How to add swiz to your flex project?
A: just copy swiz swc file to "libs" folder of project



* 添加<swiz:Swiz>...</swiz:Swiz>。 其中
<swiz:beanProviders>
<config:Beans />
</swiz:beanProviders>


<swiz:beanProvider>是用来设置哪个文件用来定义那些Non-visual component的bean。类似于spring设置bean xml file, all defined bean are processed by Swiz for dependency injection and the creation of event handlers。上面的代码设置了<config:Beans/>。假设xmln:config设置的是"demo.config.*",那么<config:Beans/>表示的是demo.config.Beans.mxml。
注意:该mxml file必须存在,否则会报错


然后再来看demo.config.Beans.mxml:
<swiz:BeanProvider 
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:swiz="http://swiz.swizframework.org"
xmlns:service="demo.service.*" 
xmlns:controller="org.swizframework.quickswiz.controller.*">

<service:UserService id="userService"/>
<controller:UserController id="userController"/>
</swiz:BeanProvider>


看到设置了xmlns:service指向"demo.service.*" ,xmlns:controller指向"demo.controller.*"
因此
<service:UserService id="userService"/>
表示把demo.service.UserService.as定义做id为userService的bean


<controller:UserController id="userController"/>
表示把demo.controller.UserController.as定义做id为userController的bean

之后进行DI时,就会通过这些id来注入


* 待续

原创粉丝点击