结合前两篇文章:
加上Fabrication自身支持的元标签,可简化一些代码,但简化后也付出了一定的代码,那就是变量需要声明为public,而之前虽然繁琐,但却可以将其声明为private。
以InjectProxy标签为例,进行说明,先看之前的实现:
在“小试Flex框架Fabrication”的例子中,使用了AddContactFormProxy这个代理类(用于数据处理),在mediator类(继承自FlexMediator)中声明了_proxy这个变量,然后覆写onRegister方法,在该方法内对变量_proxy进行初始化,方法如下:
_proxy = retrieveProxy(AddContactFormProxy.NAME) as AddContactFormProxy;
调用retrieveProxy方法获取到AddContactFormProxy类的引用,其中AddContactFormProxy.NAME为AddContactFormProxy类中公开的静态常量
public static const NAME:String = “AddContactFormProxy”;//值为类的名称
在org.puremvc.as3.multicore.utilities.fabrication.core包下的FabricationModel类中可看到它的实现:
1: /**
2: * Copyright (C) 2008 Darshan Sawardekar.
3: *
4: * Licensed under the Apache License, Version 2.0 (the "License");
5: * you may not use this file except in compliance with the License.
6: * You may obtain a copy of the License at
7: *
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.puremvc.as3.multicore.utilities.fabrication.core {
18: import org.puremvc.as3.multicore.utilities.fabrication.utils.HashMap;
19: import org.puremvc.as3.multicore.interfaces.IProxy;
20: import org.puremvc.as3.multicore.utilities.fabrication.interfaces.IDisposable;
21: import org.puremvc.as3.multicore.core.Model;
22:
23: /**
24: * FabricationModel is the custom model implementation used internally
25: * by fabrication.
26: *
27: * @author Darshan Sawardekar
28: */
29: public class FabricationModel extends Model implements IDisposable {
30:
31: /**
32: * Creates and returns the instance of the FabricationModel for the specified
33: * multiton key.
34: *
35: * @param multitonKey The multitonkey whose FabricationModel is to be created.
36: */
37: static public function getInstance(multitonKey:String):FabricationModel {
38: if (instanceMap[multitonKey] == null) {
39: instanceMap[multitonKey] = new FabricationModel(multitonKey);
40: }
41:
42: return instanceMap[multitonKey] as FabricationModel;
43: }
44:
45: /**
46: * Stores the proxies in this model in a hash map.
47: */
48: protected var proxyHashMap:HashMap;
49:
50: /**
51: * Creates the FabricationModel instance.
52: *
53: * @param multitonKey The multitonkey for this FabricationModel
54: */
55: public function FabricationModel(multitonKey:String) {
56: super(multitonKey);
57:
58: proxyHashMap = new HashMap();
59: }
60:
61: /**
62: * @see org.puremvc.as3.multicore.interfaces.IModel#registerProxy()
63: */
64: override public function registerProxy(proxy:IProxy):void {
65: proxy.initializeNotifier(multitonKey);
66: proxyHashMap.put(proxy.getProxyName(), proxy);
67: proxy.onRegister();
68: }
69:
70: /**
71: * @see org.puremvc.as3.multicore.interfaces.IProxy#retrieveProxy()
72: */
73: override public function retrieveProxy(proxyName:String):IProxy {
74: return proxyHashMap.find(proxyName) as IProxy;
75: }
76:
77: /**
78: * @see org.puremvc.as3.multicore.interfaces.IModel#hasProxy()
79: */
80: override public function hasProxy(proxyName:String):Boolean {
81: return proxyHashMap.exists(proxyName);
82: }
83:
84: /**
85: * @see org.puremvc.as3.multicore.interfaces.IModel#removeProxy()
86: */
87: override public function removeProxy(proxyName:String):IProxy {
88: var proxy:IProxy = proxyHashMap.remove(proxyName) as IProxy;
89: proxy.onRemove();
90:
91: return proxy;
92: }
93:
94: /**
95: * @see org.puremvc.as3.multicore.utilities.fabrication.interfaces.IDisposable#dispose()
96: */
97: public function dispose():void {
98: proxyHashMap.dispose();
99: proxyHashMap = null;
100:
101: removeModel(multitonKey);
102: }
103: }
104: }
而应用时通过覆写onRegister方法,来初始化:
1: override public function onRegister():void {
2: super.onRegister();
3:
4: _proxy = retrieveProxy(AddContactFormProxy.NAME) as AddContactFormProxy;
5: }
这样之后的处理函数中就可以调用AddContactFormProxy类的方法了,如调用它的add方法:
_proxy.add();//根据方法定义传入对应的参数
而现在换成使用InjectProxy标签,将代码会会精简一些(使用了Ioc注入简化了开发中的代码编写):
1、需要设定编译器的参数:
点击项目,右击选择属性,找到第二项“Flex编译器”,在附加的编译器参数中输入:“-locale zh_CN -keep-as3-metadata=InjectProxy,InjectMediator”
其中我使用了locale 中文语言包进行了处理,具体操作步骤可参考“Flex多国语言示例”(地址见文章最前面)
2、在需要使用的类中,使用如下的写法:
1: [InjectProxy(name="AddContactFormProxy")]
2: public var _proxy:AddContactFormProxy;
这样就完成了对变量_proxy的初始化,后续调用可以不再使用retrieveProxy()方法了,也可以不再去覆写onREgister()方法了。
至于Mediator,则与此类似(InjectMediator),当然也可以实现自定义标签<参数InjectMeditor的实现就好>,主要需要覆写如下方法:
1: protected function addCustomMetaDatas():Array{
2: return [];
3: };
4:
5: protected function addCustomProcessors():Array{
6: return [];
7: };
8:
9: protected function addViewPackages():Array{
10: return [];
11: };
12:
13: protected function addCustomFilterPackage():Array{
14: return [];
15: }
具体参见包org.puremvc.as3.multicore.utilities.fabrication.global中的FabricationConfig类的代码,标签的处理逻辑可以参数包org.puremvc.as3.multicore.utilities.fabrication.injection中的ProxyInjectorProcessor类。(自己实现两个方法:processor、elementExist)
1: package org.puremvc.as3.multicore.utilities.fabrication.interfaces
2: {
3: import org.puremvc.as3.multicore.interfaces.IFacade;
4: import org.puremvc.as3.multicore.utilities.fabrication.injection.InjectionField;
5:
6: public interface IInjectProcessor
7: {
8: function processor(field:InjectionField, facade:IFacade ):Object;
9:
10: function elementExist(field:InjectionField,facade:IFacade = null):IFacade
11: }
12: }
本文例子的 完整代码下载