原文: http://www.k-zone.cn/zblog/post/flex-mate-framework-using-adapter.html
本篇文章再向大家介绍一个Mate Flex Framework比较典型的流程:双向通讯之模型:使用适配器(Two-way communication via model: Using an adapter)
下面是它的流程图:
okay,让我们来分析一下这个新的流程所带给我们的含义:
1、在视图中,我们使用<Dispatcher> 这个Mate tag,触发了某一个事件。
2、而Event Bus会将这个触发的Event告诉事件地图(Event Map)。
Event Map里面会事先做好一些事件的处理逻辑(即定义好一些EventHandler)
3、在这些定义好的EventHandler里面,会有一组动作来响应这个Event。
在上图中所示的是,在<EventHandler>里面定义了一个<ServiceInvoker>
4、使用Model Manage的方式,设定一些值。
5、通过Bindings的方式,当Model Manager的属性发生改变后,使用Apdapter的方式捕捉到这些改变。
6、通过Bindings的方式,在视图中绑定了Model Manager的属性。
也就是当Model Manager的属性发生了改变,则会同步更新到视图上面。
Kenshin将使用一个现成的例子来说明一下这种流程。
这个例子是的地址在:http://mate.asfusion.com/assets/content/examples/weatherWidget/srcview/
请看下面的片段代码:
|
|
ok,上面的代码说明了,在<EventMap>里面建立了一个类型为WeatherEvent.GET的<EventHandlers>,同时使用<MethodInvoker>调用了一个叫做WeatherManager.setWeather()
同时传入了一个参数:currentEvent.data。
|
|
ok,让我们来看一下WeatherManager,首先由于之前的代码使用了<MethodInvoker>的方式调用了WeatherManager.setWeather()。而function:setWeather里面的内容是:
将传入的内容赋值给_weather,同时触发一个dispatchEvent(new Event("weatherChange"));
而由于event :weatherChange绑定了currentWeather,因此当触发这个event后,自动会执行public function get currentWeather():CurrentConditions里面的内容。
|
|
由于在DetailModel.mxml里面使用了<Injectors>标签,同时在<Injectors>里面加入一些属性标签<PropertyInjector>
即,将WeatherManager里面的currentWeather,赋值给DetailModel里面的weather。
而促成上面的方式是由于之前的一段代码,即触发currentWeather造成的,而触发currentWeather后,则由于<Injectors>的作用而执行了刚刚的一段代码。
|
|
我们在看一下DetailModel里面的weather到底定义了一些什么东西。
代码:public function set weather(w:CurrentConditions)里面已经给我们说明的非常清楚了。
通过set weather,我们获得了humidity、description、pressure、wind、visibility这几个属性。
|
|
上面的代码,则是,当获取了 humidity、description、pressure、wind、visibility这几个属性后,就将其显示在UI上面了。
因此使用了[bindable]的方式,因此当上述几个值发生变化的时候,就会自动显示改变后的结果了。
总结一下,上面的例子其实就是使用了[Bindable ]、<Injectors>这两种方式。 而[Bindable]、<Injectors>这两种方式的叠加也就是之前所说的Model Manager方式。
其实大家,如果详细的看一下http://mate.asfusion.com/assets/content/examples/weatherWidget/srcview/这个例子,就可以完全的理解Mate Flex FrameWork中Apdapter与Model Manager的做法。