You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2013/05/06 18:50:51 UTC

git commit: [flex-asjs] [refs/heads/develop] - Add stockquote example

Updated Branches:
  refs/heads/develop d69ac2d3b -> 9ea55e39c


Add stockquote example


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/9ea55e39
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/9ea55e39
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/9ea55e39

Branch: refs/heads/develop
Commit: 9ea55e39c7b1908abb447fd1cb6f60ebc3ea6331
Parents: d69ac2d
Author: Alex Harui <ah...@apache.org>
Authored: Mon May 6 09:50:44 2013 -0700
Committer: Alex Harui <ah...@apache.org>
Committed: Mon May 6 09:50:44 2013 -0700

----------------------------------------------------------------------
 examples/StockQuote/src/MyInitialView.mxml         |  116 +++++++++++++
 .../StockQuote/src/StockDataJSONItemConverter.as   |   22 +++
 examples/StockQuote/src/StockQuote.mxml            |   34 ++++
 .../StockQuote/src/controllers/MyController.as     |   81 +++++++++
 examples/StockQuote/src/models/MyModel.as          |  126 +++++++++++++++
 5 files changed, 379 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9ea55e39/examples/StockQuote/src/MyInitialView.mxml
----------------------------------------------------------------------
diff --git a/examples/StockQuote/src/MyInitialView.mxml b/examples/StockQuote/src/MyInitialView.mxml
new file mode 100644
index 0000000..191687b
--- /dev/null
+++ b/examples/StockQuote/src/MyInitialView.mxml
@@ -0,0 +1,116 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+-->
+<basic:ViewBase xmlns:fx="http://ns.adobe.com/mxml/2009"
+				xmlns:basic="library://ns.apache.org/flexjs/basic"
+			    initComplete="initControls()">
+    <fx:Script>
+        <![CDATA[
+			import org.apache.flex.events.CustomEvent;
+			import models.MyModel;
+			
+			private var _symbol:String;
+			
+            public function get symbol():String
+            {
+                return _symbol;
+            }
+			
+			public function get requestedField():String
+			{
+				return radio1.selectedValue as String;
+			}
+			
+			private function radioChanged(e:org.apache.flex.events.Event):void
+			{
+				dispatchEvent(new CustomEvent("radioClicked"));
+				field.text = RadioButton(e.target).text;
+			}
+			
+			private function initControls():void
+			{
+				list.selectedItem = MyModel(applicationModel).stockSymbol;
+				radio1.selectedValue = MyModel(applicationModel).requestedField;
+				if (radio1.selected)
+					field.text = radio1.text;
+				else if (radio2.selected)
+					field.text = radio2.text;
+				else if (radio3.selected)
+					field.text = radio3.text;
+				else if (radio4.selected)
+					field.text = radio4.text;
+				
+			}
+		]]>
+    </fx:Script>
+	<fx:Style>
+		@namespace basic "library://ns.apache.org/flexjs/basic";
+		
+		.output {
+			font-size: 20px;
+		}
+	</fx:Style>
+	<basic:Label x="20" y="8" width="300" text="Enter Stock Symbol or choose from list:" />
+	<basic:TextInput id="symbolTI" x="20" y="25" >
+		<basic:beads>
+			<basic:SimpleBinding eventName="stockSymbolChanged"
+								 sourceID="applicationModel"
+								 sourcePropertyName="stockSymbol"
+								 destinationPropertyName="text" />
+		</basic:beads>
+	</basic:TextInput>
+	<basic:DropDownList id="list" x="180" y="25" width="100" height="17"
+						change="_symbol = list.selectedItem as String; dispatchEvent(new CustomEvent('listChanged'))">
+		<basic:beads>
+			<basic:ConstantBinding
+				sourceID="applicationModel"
+				sourcePropertyName="strings"
+				destinationPropertyName="dataProvider" />
+		</basic:beads>
+	</basic:DropDownList>
+	<basic:TextButton text="Get Quote" x="20" y="50" click="_symbol = symbolTI.text; dispatchEvent(new CustomEvent('buttonClicked'))" />
+	<basic:Label x="20" y="80" id="field" />
+	<basic:Label x="20" y="100" className="output" height="24">
+		<basic:beads>
+			<basic:SimpleBinding eventName="responseTextChanged"
+								 sourceID="applicationModel"
+								 sourcePropertyName="responseText"
+								 destinationPropertyName="text" />
+		</basic:beads>
+	</basic:Label>
+    
+	<basic:RadioButton id="radio1" text="Price" value="Ask" x="180" y="50" groupName="group1" change="radioChanged(event)"/>
+	<basic:RadioButton id="radio2" text="Change" value="Change" x="180" y="70" groupName="group1" change="radioChanged(event)"/>
+	<basic:RadioButton id="radio3" text="Day's High" value="DaysHigh" x="180" y="90" groupName="group1" change="radioChanged(event)"/>
+	<basic:RadioButton id="radio4" text="Day's Low" value="DaysLow" x="180" y="110" groupName="group1" change="radioChanged(event)"/>
+	<basic:CheckBox id="showAllData" text="Show All Data" x="180" y="140" />
+	
+	<basic:TextArea id="ta" x="20" y="160" width="300" height="100">
+		<basic:beads>
+			<basic:SimpleBinding eventName="change"
+								 sourceID="showAllData"
+								 sourcePropertyName="selected"
+								 destinationPropertyName="visible" />
+			<basic:SimpleBinding eventName="responseDataChanged"
+								 sourceID="applicationModel"
+								 sourcePropertyName="allData"
+								 destinationPropertyName="text" />
+		</basic:beads>
+	</basic:TextArea>
+</basic:ViewBase>

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9ea55e39/examples/StockQuote/src/StockDataJSONItemConverter.as
----------------------------------------------------------------------
diff --git a/examples/StockQuote/src/StockDataJSONItemConverter.as b/examples/StockQuote/src/StockDataJSONItemConverter.as
new file mode 100644
index 0000000..b2cb76d
--- /dev/null
+++ b/examples/StockQuote/src/StockDataJSONItemConverter.as
@@ -0,0 +1,22 @@
+package
+{
+    import org.apache.flex.net.JSONItemConverter;
+    
+    public class StockDataJSONItemConverter extends JSONItemConverter
+    {
+        public function StockDataJSONItemConverter()
+        {
+            super();
+        }
+        
+        override public function convertItem(data:String):Object
+        {
+            var obj:Object = super.convertItem(data);
+			if (obj["query"]["count"] == 0)
+				return "No Data";
+			
+			obj = obj["query"]["results"]["quote"];
+			return obj;
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9ea55e39/examples/StockQuote/src/StockQuote.mxml
----------------------------------------------------------------------
diff --git a/examples/StockQuote/src/StockQuote.mxml b/examples/StockQuote/src/StockQuote.mxml
new file mode 100644
index 0000000..de830d4
--- /dev/null
+++ b/examples/StockQuote/src/StockQuote.mxml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<basic:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
+				   xmlns:local="*"
+				   xmlns:basic="library://ns.apache.org/flexjs/basic" 
+				   xmlns:models="models.*" 
+				   xmlns:controllers="controllers.*"
+				   initialize="MyModel(model).stockSymbol='ADBE'"
+				   >
+	<basic:valuesImpl>
+		<basic:SimpleCSSValuesImpl />
+	</basic:valuesImpl>
+	<basic:initialView>
+		<local:MyInitialView />
+	</basic:initialView>
+	<basic:model>
+		<models:MyModel />
+	</basic:model>
+	<basic:controller>
+		<controllers:MyController />
+	</basic:controller>
+	<basic:beads>
+		<basic:HTTPService id="service">
+			<basic:LazyCollection id="collection">
+				<basic:inputParser>
+					<basic:JSONInputParser />
+				</basic:inputParser>
+				<basic:itemConverter>
+					<local:StockDataJSONItemConverter />
+				</basic:itemConverter> 
+			</basic:LazyCollection>
+		</basic:HTTPService>
+		<basic:ViewSourceContextMenuOption />
+	</basic:beads>
+</basic:Application>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9ea55e39/examples/StockQuote/src/controllers/MyController.as
----------------------------------------------------------------------
diff --git a/examples/StockQuote/src/controllers/MyController.as b/examples/StockQuote/src/controllers/MyController.as
new file mode 100644
index 0000000..c7332e3
--- /dev/null
+++ b/examples/StockQuote/src/controllers/MyController.as
@@ -0,0 +1,81 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package controllers
+{
+	import org.apache.flex.events.Event;
+	
+	import org.apache.flex.core.Application;
+	import org.apache.flex.core.IDocument;
+    
+    import models.MyModel;
+    	
+	public class MyController implements IDocument
+	{
+		public function MyController(app:Application = null)
+		{
+			if (app)
+			{
+				this.app = app as StockQuote;
+				app.addEventListener("viewChanged", viewChangeHandler);
+			}
+		}
+		
+        private var queryBegin:String = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22";
+        private var queryEnd:String = "%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json";
+		private var app:StockQuote;
+		
+		private function viewChangeHandler(event:Event):void
+		{
+			app.initialView.addEventListener("buttonClicked", buttonClickHandler);
+			app.initialView.addEventListener("radioClicked", radioClickHandler);
+            app.initialView.addEventListener("listChanged", listChangedHandler);
+		}
+		
+        private function buttonClickHandler(event:Event):void
+        {
+            var sym:String = MyInitialView(app.initialView).symbol;
+            app.service.url = queryBegin + sym + queryEnd;
+            app.service.send();
+            app.service.addEventListener("complete", completeHandler);
+        }
+        
+		private function radioClickHandler(event:Event):void
+		{
+			var field:String = MyInitialView(app.initialView).requestedField;
+			MyModel(app.model).requestedField = field;
+		}
+		
+        private function completeHandler(event:Event):void
+        {
+			MyModel(app.model).responseData = app.collection.getItemAt(0);
+        }
+        
+        private function listChangedHandler(event:Event):void
+        {
+            MyModel(app.model).stockSymbol = MyInitialView(app.initialView).symbol;
+        }
+        
+		public function setDocument(document:Object, id:String = null):void
+		{
+			this.app = document as StockQuote;
+			app.addEventListener("viewChanged", viewChangeHandler);
+		}
+
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9ea55e39/examples/StockQuote/src/models/MyModel.as
----------------------------------------------------------------------
diff --git a/examples/StockQuote/src/models/MyModel.as b/examples/StockQuote/src/models/MyModel.as
new file mode 100644
index 0000000..c724f4b
--- /dev/null
+++ b/examples/StockQuote/src/models/MyModel.as
@@ -0,0 +1,126 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package models
+{
+	import org.apache.flex.events.Event;
+	import org.apache.flex.events.EventDispatcher;
+	
+	public class MyModel extends EventDispatcher
+	{
+		public function MyModel()
+		{
+		}
+		
+		private var _requestedField:String = "Ask";
+		
+		[Bindable("requestedFieldChanged")]
+		public function get requestedField():String
+		{
+			return _requestedField;
+		}
+		
+		public function set requestedField(value:String):void
+		{
+			if (value != _requestedField)
+			{
+				_requestedField = value;
+				dispatchEvent(new Event("requestedFieldChanged"));
+				if (_responseData)
+					dispatchEvent(new Event("responseTextChanged"));
+			}
+		}
+		
+		private var _responseText:String;
+		
+		[Bindable("responseTextChanged")]
+		public function get responseText():String
+		{
+			if (_responseData == null)
+				return "";
+			if (_responseData == "No Data")
+				return _responseData as String;
+			var s:String = _responseData[_requestedField];
+			if (s == null)
+			{
+				if (_requestedField == "Ask")
+					s = _responseData["Bid"];
+			}
+			return s;
+		}
+		
+		private var _responseData:Object;
+		
+		[Bindable("responseDataChanged")]
+		public function get responseData():Object
+		{
+			return _responseData;
+		}
+		
+		public function set responseData(value:Object):void
+		{
+			if (value != _responseData)
+			{
+				_responseData = value;
+				_allData = "";
+				dispatchEvent(new Event("responseDataChanged"));
+				dispatchEvent(new Event("responseTextChanged"));
+			}
+		}
+		
+		private var _allData:String = "";
+		
+		[Bindable("responseDataChanged")]
+		public function get allData():String
+		{
+			if (_allData == "" && _responseData != null)
+			{
+				for (var p:String in _responseData)
+				{
+					_allData += p + ": " + _responseData[p] + "\n";
+				}
+			}
+			return _allData;
+		}
+		
+		
+		private var _stockSymbol:String;
+		
+		[Bindable("stockSymbolChanged")]
+		public function get stockSymbol():String
+		{
+			return _stockSymbol;
+		}
+		
+		public function set stockSymbol(value:String):void
+		{
+			if (value != _stockSymbol)
+			{
+				_stockSymbol = value;
+				dispatchEvent(new Event("stockSymbolChanged"));
+			}
+		}
+
+        private var _strings:Array = ["AAPL", "ADBE", "GOOG", "MSFT", "YHOO"];
+        public function get strings():Array
+        {
+            return _strings;
+        }
+
+	}
+}
\ No newline at end of file