Permalink
Browse files

updated stock quote example

  • Loading branch information...
1 parent 1d9c7e8 commit 5c537bf5fee91e845e601496065e28fa8265bb5b @seanhess committed May 2, 2009
Binary file not shown.
@@ -3,7 +3,7 @@
xmlns:glue="stocks.glue.*" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" xmlns:view="stocks.view.*" xmlns:control="stocks.control.*">
<fx:Declarations>
- <glue:MainGlue/>
+ <control:Main/>
</fx:Declarations>
<s:layout>
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Controller xmlns="http://glue.seanhess.net/2009" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:view="stocks.view.*" xmlns:control="stocks.control.*" xmlns:mate="http://mate.asfusion.com/" xmlns:service="stocks.service.*">
+ <fx:Declarations>
+
+ <!-- SERVICE -->
+ <service:QuoteHTTPService id="getQuoteFromWeb"/>
+ <service:RandomQuoteService id="getRandomQuote"/>
+ <service:QuoteServiceParser id="quoteParser"/>
+ <service:QuoteService id="quotes" quoteService="{getQuoteFromWeb}" parser="{quoteParser}"/>
+
+ <!-- VIEW -->
+ <Glue>
+ <view:QuotePanel id="quotePanel"/>
+ <Route event="{QuotePanel.GET_QUOTE}" call="quotes.getQuote(quotePanel.quote)"/>
+ </Glue>
+
+ </fx:Declarations>
+</Controller>
+
@@ -1,38 +0,0 @@
-package stocks.control
-{
- import flash.events.Event;
-
- import mx.rpc.events.ResultEvent;
-
- import net.seanhess.glue.Respond;
-
- import stocks.model.Quote;
- import stocks.service.IQuoteService;
- import stocks.service.QuoteServiceParser;
-
- public class Quotes
- {
- public var quoteService:IQuoteService;
- public var parser:QuoteServiceParser; // not an interface, but you could extend it to mock it
-
- public function getQuote(quote:Quote):void
- {
- var respond:Respond = new Respond(quoteService, quote);
- respond.result(onResult);
- respond.fault(onFault);
-
- quoteService.getQuote(quote.symbol);
- }
-
- private function onResult(event:ResultEvent, respond:Respond):void
- {
- var quote:Quote = respond.data as Quote;
- quote.price = parser.getPrice(event.result);
- }
-
- private function onFault(event:Event):void
- {
- trace("There was an error");
- }
- }
-}
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<GlueMap xmlns="http://glue.seanhess.net/2009" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:view="stocks.view.*" xmlns:control="stocks.control.*" xmlns:mate="http://mate.asfusion.com/" xmlns:service="stocks.service.*">
- <fx:Declarations>
-
- <!-- SERVICE -->
- <service:QuoteService id="quoteService"/>
- <service:RandomQuoteService id="randomQuoteService"/>
- <service:QuoteServiceParser id="quoteParser"/>
-
- <!-- CONTROL -->
- <control:Quotes id="quotes" quoteService="{quoteService}" parser="{quoteParser}"/>
-
- <!-- VIEW -->
- <Glue>
- <view:QuotePanel id="quotePanel"/>
- <Route event="{QuotePanel.GET_QUOTE}" call="quotes.getQuote(quotePanel.quote)"/>
- </Glue>
-
- </fx:Declarations>
-</GlueMap>
-
@@ -2,7 +2,7 @@ package stocks.service
{
import flash.events.IEventDispatcher;
- public interface IQuoteService extends IEventDispatcher
+ public interface IQuoteHTTPService extends IEventDispatcher
{
function getQuote(symbol:String):void;
}
@@ -0,0 +1,15 @@
+package stocks.service
+{
+ import mx.rpc.http.HTTPService;
+
+ public class QuoteHTTPService extends HTTPService implements IQuoteHTTPService
+ {
+ private const URL:String = "http://judstephenson.com/api/Quotes/Realtime/";
+
+ public function getQuote(symbol:String):void
+ {
+ this.url = URL + symbol;
+ send();
+ }
+ }
+}
@@ -1,15 +1,38 @@
package stocks.service
{
- import mx.rpc.http.HTTPService;
+ import flash.events.Event;
+
+ import mx.rpc.events.ResultEvent;
+
+ import net.seanhess.glue.Respond;
+
+ import stocks.model.Quote;
+ import stocks.service.IQuoteHTTPService;
+ import stocks.service.QuoteServiceParser;
- public class QuoteService extends HTTPService implements IQuoteService
+ public class QuoteService
{
- private const URL:String = "http://judstephenson.com/api/Quotes/Realtime/";
+ public var quoteService:IQuoteHTTPService;
+ public var parser:QuoteServiceParser; // not an interface, but you could extend it to mock it
- public function getQuote(symbol:String):void
+ public function getQuote(quote:Quote):void
{
- this.url = URL + symbol;
- send();
+ var respond:Respond = new Respond(quoteService, quote);
+ respond.result(onResult);
+ respond.fault(onFault);
+
+ quoteService.getQuote(quote.symbol);
+ }
+
+ private function onResult(event:ResultEvent, respond:Respond):void
+ {
+ var quote:Quote = respond.data as Quote;
+ quote.price = parser.getPrice(event.result);
+ }
+
+ private function onFault(event:Event):void
+ {
+ trace("There was an error");
}
}
}
@@ -4,7 +4,7 @@ package stocks.service
import mx.rpc.events.ResultEvent;
- public class RandomQuoteService extends EventDispatcher implements IQuoteService
+ public class RandomQuoteService extends EventDispatcher implements IQuoteHTTPService
{
public function getQuote(symbol:String):void
{
@@ -7,13 +7,14 @@ package net.seanhess.glue
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
- public class Respond
+ public class Respond // not getting garbage collected in flex 4
{
public var service:IEventDispatcher;
public var data:*;
public var handlers:Dictionary;
public var autoClean:Boolean = true;
public var uid:String = Math.random().toString();
+ public var cleaned:Boolean = false;
public function Respond(service:IEventDispatcher, data:*=null, autoClean:Boolean = true)
{
@@ -41,6 +42,8 @@ package net.seanhess.glue
protected function callback(event:Event):void
{
+ if (cleaned) return;
+
var handler:Function = handlers[event.type];
try
@@ -74,7 +77,9 @@ package net.seanhess.glue
service = null;
data = null;
- handlers = null;
+ handlers = null;
+
+ cleaned = true;
}
public function remove(type:String):void

0 comments on commit 5c537bf

Please sign in to comment.