forked from soraghallaigh/FH-Training-App-Sencha
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding required files to replace webview with stocks option. Added vi…
…ew, model, controller, new icon and some CSS. Also updated viewport and home view
- Loading branch information
Alan Moran
committed
Apr 4, 2012
1 parent
78f1484
commit 0b24b11
Showing
20 changed files
with
552 additions
and
7 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
app.controllers.stocks = new Ext.Controller({ | ||
|
||
getStocks: function() { | ||
var companyName = Ext.getCmp("companyName").getValue(); | ||
|
||
if (companyName.length == 0) { | ||
Ext.Msg.alert('Error', 'You must enter a company name.', Ext.emptyFn); | ||
return; | ||
} | ||
// Show loading spinner | ||
mask.show(); | ||
|
||
// Call to the cloud | ||
$fh.act({ | ||
act: 'getStockInfo', | ||
req: { | ||
name: companyName, | ||
} | ||
}, function(res) { | ||
//Adding res to store | ||
app.stores.stocks.add({Name: res.stockInfo.Name, Symbol:res.stockInfo.Symbol, Last: res.stockInfo.Last, Change: res.stockInfo.Change, Date: res.stockInfo.Date, Open: res.stockInfo.Open, Time: res.stockInfo.Time}); | ||
|
||
// Hide loading spinner | ||
mask.hide(); | ||
|
||
//Empty the company field | ||
Ext.getCmp("companyName").setValue(); | ||
|
||
},function(err){ | ||
mask.hide(); | ||
Ext.Msg.alert('Error', 'An error has occurred with your request. Please try again.', Ext.emptyFn); | ||
}); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
app.models.Stocks = Ext.regModel('app.models.Stocks', { | ||
fields: ['Name', 'Symbol', 'Last', 'Change', 'Date', 'Time', 'Open'], | ||
}); | ||
|
||
app.stores.stocks = new Ext.data.Store({ | ||
model: 'app.models.Stocks', | ||
data : [] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
app.views.Stocks = Ext.extend(Ext.Panel, { | ||
title: 'Stocks', | ||
iconCls: 'home', | ||
scroll: 'vertical', | ||
height: '100%', | ||
layout: 'fit', | ||
|
||
listeners: { | ||
beforeshow: function() { | ||
}, | ||
}, | ||
|
||
dockedItems: [ | ||
{ | ||
dock: 'top', | ||
xtype: 'toolbar', | ||
title: '<img class="logo logoOffset" src="app/images/logo.png" />', | ||
items: [ | ||
{ | ||
text: 'Back', | ||
ui: 'back', | ||
hidden: app.hideBack || false, | ||
handler: function() { | ||
app.views.viewport.setActiveItem(app.views.home, {type: 'slide', direction: 'right'}); | ||
app.stores.stocks.removeAll(); | ||
} | ||
} | ||
] | ||
} | ||
], | ||
|
||
items: [{ | ||
xtype: 'panel', | ||
layout:{ | ||
type:'vbox', | ||
align: 'strech' | ||
|
||
}, | ||
defaults:{flex:'1'}, | ||
items: [{ | ||
xtype: 'form', | ||
items: [ | ||
{ | ||
xtype: 'fieldset', | ||
title: 'Search Stock Info', | ||
instructions: 'Enter company name above to perform a mash-up.', | ||
defaults: { | ||
labelAlign: 'left', | ||
labelWidth: '50%' | ||
}, | ||
items: [ | ||
{ | ||
xtype: 'textfield', | ||
id: 'companyName', | ||
name: 'Company Name' | ||
}, | ||
] | ||
}, | ||
{ | ||
xtype: 'button', | ||
text: 'Submit', | ||
handler: function() { | ||
Ext.dispatch({ | ||
controller: app.controllers.stocks, | ||
action: 'getStocks' | ||
}); | ||
} | ||
} | ||
] | ||
},{ | ||
id:"stockResults", | ||
xtype: 'list', | ||
width: '100%', | ||
disableSelection: true, | ||
scroll: "vertical", | ||
store: app.stores.stocks, | ||
itemTpl:'<h2>Stock: {Symbol}</h2><ul><li>Change: {Change}</li><li>Date: {Date}</li><li>Last: {Last}</li><li>Name: {Name}</li><li>Symbol: {Symbol}</li><li>Time: {Time}</li></ul>', | ||
flex: 1 | ||
}] | ||
}] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
/* | ||
* Stocks | ||
*/ | ||
function getStockInfo(param) { | ||
return stock.getStockInfo(param.name); | ||
} | ||
|
||
/* | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Mash multiple business apis returned data. | ||
* Stock Symble lookup: Using YAHOO API. JSONP | ||
* Stock Info lookup: Using WebServiceX API . SOAP | ||
* | ||
*/ | ||
|
||
var stock = { | ||
//YAHOO finance api for looking up stock symbol with a company name. It is a JSONP service. | ||
yahooApi : "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={0}&callback=YAHOO.Finance.SymbolSuggest.ssCallback", | ||
//WebServiceX API (Open API). It returns stock details with specific stock symbol. | ||
webServiceXApi : "http://www.webservicex.net/stockquote.asmx", | ||
/** | ||
* The function will look for stock symbol based on "name" param, and return stock info from WebServiceX | ||
* | ||
* Return stock information. | ||
*/ | ||
getStockInfo : function(name) { | ||
//Compose request url using user input. | ||
var yahooApiUrl = this.yahooApi.replace("{0}", name); | ||
/* | ||
* Perform Webcall | ||
* Raw response from YAHOO JSONP api which contains stock symbol as well as other information we do not want. | ||
* | ||
*/ | ||
var symbolRes = $fh.web({ | ||
url : yahooApiUrl, | ||
method : "GET", | ||
charset : "UTF-8", | ||
period : 3600 | ||
}); | ||
|
||
//Clear up YAHOO response and only keep the information "stock symbol" we need. | ||
var stockSymbol = this.processSymbolRes(symbolRes); | ||
|
||
// construct SOAP envelop. We could do this manually or just use a Javascript Library. | ||
var soapEnvolope = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<GetQuote xmlns="http://www.webserviceX.NET/">' + '<symbol>' + stockSymbol + '</symbol>' + '</GetQuote>' + '</soap:Body>' + '</soap:Envelope>'; | ||
|
||
//Retrieve SOAP url | ||
var stockInfoUrl = this.webServiceXApi; | ||
|
||
//Prepare webcall parameters | ||
var opt = { | ||
url : stockInfoUrl, | ||
method : "POST", | ||
charset : "UTF-8", | ||
contentType : "text/xml", | ||
body : soapEnvolope, | ||
period : 3600 | ||
} | ||
|
||
//Perform webcall | ||
var stockInfoSoapRes = $fh.web(opt); | ||
|
||
//getSOAPElement will retrieve specific XML object within SOAP response | ||
var body=stockInfoSoapRes.body.replace(/</g,"<").replace(/>/g,">"); | ||
var xmlData = getSOAPElement("GetQuoteResult", body); | ||
|
||
var xmlObj=xml(xmlData); | ||
|
||
//Create Object | ||
var stockInfo={ | ||
Symbol:xmlObj.find("Symbol").text(), | ||
Last:xmlObj.find("Last").text(), | ||
Open:xmlObj.find("Open").text(), | ||
"Date":xmlObj.find("Date").text(), | ||
Time:xmlObj.find("Time").text(), | ||
Change:xmlObj.find("Change").text(), | ||
Name:xmlObj.find("Name").text() | ||
} | ||
|
||
//mash up the data and return to client. | ||
return { | ||
stockSymbol : stockSymbol, | ||
stockInfo :stockInfo | ||
}; | ||
|
||
}, | ||
/** | ||
* Process Response from YAHOO stock symbol api. | ||
* It will clear up the response and only return stock symbol as string. | ||
*/ | ||
processSymbolRes : function(res) { | ||
var resBody = res.body; | ||
var removedHeadRes = resBody.replace("YAHOO.Finance.SymbolSuggest.ssCallback(", ""); | ||
//remove jsonp callback header | ||
var removedTailRes = removedHeadRes.substr(0, removedHeadRes.length - 1); | ||
//remove jsonp callback bracket | ||
var resObj = $fh.parse(removedTailRes); | ||
//parse result to JSON object | ||
return resObj.ResultSet.Result[0].symbol; | ||
//return the first matched stock symbol | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function log (str){ | ||
$fh.log({message:str}); | ||
|
||
} | ||
|
||
/** | ||
* Retrieve a XML object that is in SOAP response. | ||
*/ | ||
function getSOAPElement(eleTagWithNS, SOAPRes) { | ||
var start_index = SOAPRes.indexOf("<" + eleTagWithNS + ">"); | ||
var end_tag = "</" + eleTagWithNS + ">"; | ||
var end_index = SOAPRes.indexOf(end_tag) + end_tag.length; | ||
var xmlData = new XML(SOAPRes.substring(start_index, end_index)); | ||
return xmlData; | ||
} |
Oops, something went wrong.