fabrication的拦截器Interceptors简介
簡介:
Interceptors(攔截器),主要目的是為了改變PureMVC的消息通知在到達Commands和Mediators的正常執(zhí)行順序。 在攔截器里可以:
·廢棄notification不再向外廣播
·修改notificationg再向外廣播
·使用新的notification替換原有的notification
·無限制發(fā)送這一次notification
·Interceptors與commands類似,可以使用PureMVC實例訪問和修改應用程序
?
依賴:
Fabrication V0.6+ 可使用此功能
?
Interceptors的注冊
Interceptors使用registerInterceptor方法來進行的注冊,該方法可在FabricationFacade或是SimpleFabricationCommand的子類中使用。
語法,registerInterceptor(noteName:String, clazz:Class, parameters:Object=null)
noteName:注冊notification它的名稱
clazz:攔截器的處理類
parameters:可選參數(shù),供攔截器處理類使用
// registers the interceptor for the save notification name.
registerInterceptor("save", MyInterceptor);
registerInterceptor("save", MyInterceptor, {foo:"bar"});?Interceptors的實現(xiàn)攔截類的處理類(前面提及的clazz),必須繼承自AbstractInterceptor類,并實現(xiàn)intercept方法。當注冊的Notifications被發(fā)送時(使用sendNotification或是routerNotification方法進行發(fā)送),攔截器會調(diào)用該處理類的intercept方法,這些方法中可以直接使用的對象有:notification:被攔截的notification對象parameters:攔截器注冊時傳入的可選參數(shù)processor:對外提供的幾種方法,忽略、跳過或中止該notification的廣播,包含三個方法<proceed|abort|skip>?proceed:允許notification繼續(xù)進行廣播,可以使用新的notification替換當前的notification。skip:如果所有對該notification的攔截器處理已經(jīng)完成,就直接調(diào)用完成方法。不然就再調(diào)用該notification的其它攔截器實例abort:直接中止該notification的廣播,并立即調(diào)用完成方法。?在框架源碼中可以看到上述三個方法中的實現(xiàn): 1: public function getNotification():INotification { 2: return notification; 3: } 4:? 5: /** 6: * Adds a interceptor to the list of interceptors for the current notification. 7: * 8: * @param interceptor The interceptor object to register. 9: */ 10: public function addInterceptor(interceptor:IInterceptor):void { 11: interceptor.processor = this; 12: interceptors.push(interceptor); 13: } 14:? 15: /** 16: * Removes the specified interceptor from the list of interceptors. 17: * 18: * @param interceptor The interceptor object to remove. 19: */ 20: public function removeInterceptor(interceptor:IInterceptor):void { 21: var index:int = interceptors.indexOf(interceptor); 22: if (index >= 0) { 23: interceptors.splice(index, 1); 24: interceptor.dispose(); 25: } 26: } 27:? 28: /** 29: * Runs all interceptors registered with this NotificationProcessor. 30: */ 31: public function run():void { 32: var n:int = interceptors.length; 33: var interceptor:IInterceptor; 34: for (var i:int = 0; i < n; i++) { 35: interceptor = interceptors[i]; 36: 37: interceptor.notification = notification; 38: interceptor.intercept(); 39: } 40: } 41:? 42: /** 43: * Sends a proceed event so that the notification can be send to the rest of 44: * the PureMVC actors. Flags this instance as complete to ignore other interceptors. 45: * Also sends a finish event to indicate that the processor can be disposed. 46: * 47: * @param note The notification to proceed with. If null the current notification object is used. 48: */ 49: public function proceed(note:INotification = null):void { 50: if (!finished) { 51: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.PROCEED, note)); 52: finish(); 53: } 54: } 55:? 56: /** 57: * Sends an abort event. Flags this instance as complete to ignore other interceptors. 58: * Also sends a finish event to indicate that the processor can be disposed. 59: */ 60: public function abort():void { 61: if (!finished) { 62: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.ABORT)); 63: finish(); 64: } 65: } 66:? 67: /** 68: * If all interceptors have been skipped then sends a finish event to indicate 69: * that the processor can be disposed. 70: */ 71: public function skip():void { 72: if (!finished && ++skipCount == interceptors.length) { 73: finish(); 74: } 75: } 76:? 77: /** 78: * Flags the notification as finished and sends a finish event. 79: */ 80: public function finish():void { 81: finished = true; 82: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.FINISH)); 83: }
interceptor的處理過程是異步的,實例中對”save”這條消息進行了監(jiān)聽響應,也對“save”進行了攔截處理,但只有在點擊“繼續(xù)”按鈕的時候才繼續(xù)廣播該notification。
?
英文原文(能力有限,本文翻譯的可能有誤,歡迎批評和斧正):http://code.google.com/p/fabrication/wiki/Interceptors#Requirements
示例demo的源碼:http://code.google.com/p/fabrication/source/browse/examples/interceptor_demo/src#src%2Fmain%2Fflex%2Fcontroller%253Fstate%253Dclosed
?
本地查看效果實例:
?
如需要本示例程序的完整代碼,立即下載>>
轉(zhuǎn)載于:https://www.cnblogs.com/meteoric_cry/archive/2012/02/01/2334442.html
總結(jié)
以上是生活随笔為你收集整理的fabrication的拦截器Interceptors简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科技馆(说一说科技馆的简介)
- 下一篇: 香肠嘴女明星(香肠嘴)