<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Brad Vernon &#187; iphone</title> <atom:link href="http://bradvernon.com/tag/iphone/feed/" rel="self" type="application/rss+xml" /><link>http://bradvernon.com</link> <description>Programmer, Political Junkie, and Tulsan</description> <lastBuildDate>Wed, 26 May 2010 19:20:03 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/> <item><title>Titanium Mobile Synchronous Network Request Workaround</title><link>http://bradvernon.com/2010/03/titanium-mobile-synchronous-network-request-workaround/</link> <comments>http://bradvernon.com/2010/03/titanium-mobile-synchronous-network-request-workaround/#comments</comments> <pubDate>Thu, 04 Mar 2010 16:52:52 +0000</pubDate> <dc:creator>Brad Vernon</dc:creator> <category><![CDATA[Mobile Apps]]></category> <category><![CDATA[android]]></category> <category><![CDATA[iphone]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[titanium]]></category> <category><![CDATA[titanium mobile]]></category><guid
isPermaLink="false">http://bradvernon.com/?p=34</guid> <description><![CDATA[While using Appcelerator&#8217;s Titanium Mobile for a Mobile App I am developing in partnership with a local company in Tulsa, OK I found that there is no support for synchronous network requests.  This can be trouble some if you are wanting to follow DRY (Don&#8217;t Repeat Yourself) coding and using a JavaScript based framework/library in [...]No related posts.]]></description> <content:encoded><![CDATA[<p>While using <a
href="http://www.appcelerator.com">Appcelerator</a>&#8217;s <a
href="http://www.appcelerator.com/products/titanium-mobile-application-development/">Titanium Mobile</a> for a Mobile App I am developing in partnership with a local company in Tulsa, OK I found that there is no support for synchronous network requests.  This can be trouble some if you are wanting to follow DRY (Don&#8217;t Repeat Yourself) coding and using a JavaScript based framework/library in your app.  I found a solution that works for the app I am developing and wanted to share with other developers.</p><p>Note: This code is written for Titanium Mobile 0.9.x.</p><h4>What is the issue?</h4><p>Here is an example of a typical XHR request based on the KitchenSink example application:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">Network</span>.<span style="color: #660066;">createHTTPClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
xhr.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'I should be first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// xhr.open(METHOD, URL, ASYNC_Request(BOOL));</span>
xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;http:example.com/getData&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Ti.<span style="color: #660066;">API</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'I should be second'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div><p>The result in Titanium&#8217;s Log is:</p><div
class="wp_syntax"><div
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">'I should be second'</span>
<span style="color: #3366CC;">'I should be first'</span></pre></div></div><p>Notice that the &#8220;I should be second&#8221; is called before the &#8220;I should be first&#8221; is called.  This is due to the asynchronous request not stopping the script from running while a remote request is being processed. This stops the ability to create a general app-wide function that processes your Network request since it will finish and return the function before the XHR request data has been returned.</p><p>This requires a lot of copy and pasting of your XHR code for every request you make which if you have a change to  make you are going to be spending quite a bit of time searching through your code to fix a problem.</p><h4>What are our needs?</h4><p>For our app we have a &#8220;common.js&#8221; file that has all regularly used functions in it.  This allows us to follow the DRY method and limit the lines of code written.  Since all data for our app is coming from a remote API call for every function in the app we will be making a remote call, processing the data and displaying the results to the user.</p><p>When making a XHR request we have the following needs for each request made:</p><ol><li>Set the current API URL</li><li>Set the headers to get JSON data from the API</li><li>Set the headers for Authorization</li><li>If an error occurs either force a login or display error notification to the user</li><li>Process the data &#8211; returned in JSON string so it needs to be converted to JavaScript Object</li><li>Return processed data to the original js function call</li></ol><h4>What is the workaround?</h4><p>Simply put: Callback function.</p><p>First step create a common.js file that contains the wrapper function for XHR requests. Note: we ran into issues including this in the app.js so a separate include file seems to do the trick best.  I included some extra functions we use to show the portability we have been trying to achieve.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> apiServerUrl        <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://example.com/api/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> apiKey              <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&amp;apikey=demoApp&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
*  Create new Object (mainly for name spacing purposes
*/</span>
<span style="color: #003366; font-weight: bold;">var</span> myFunctions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
*  create a full URL with server, path and API key
*/</span>
myFunctions.<span style="color: #660066;">prepUrl</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> apiServerUrl<span style="color: #339933;">+</span>path<span style="color: #339933;">+</span>apiKey<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
* reusable XHR function
* method: GET / POST
* path: local/ads
* data: object
* callBack: function to call when request is complete
* failMsg: array to pass to myFunctions.notice when request fails for Alert
*/</span>
myFunctions.<span style="color: #660066;">remoteRequest</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>method<span style="color: #339933;">,</span>path<span style="color: #339933;">,</span>data<span style="color: #339933;">,</span>callBack<span style="color: #339933;">,</span>failMsg<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    json <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// create new Network Client</span>
    xhr <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">Network</span>.<span style="color: #660066;">createHTTPClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// get the full url to API server</span>
    url <span style="color: #339933;">=</span> myFunctions.<span style="color: #660066;">prepUrl</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        json <span style="color: #339933;">=</span> myFunctions.<span style="color: #660066;">jsonParse</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// </span>
       <span style="color: #006600; font-style: italic;">// this is the function to call in the main javascript file</span>
       <span style="color: #006600; font-style: italic;">//</span>
        callBack<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #000066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        myFunctions.<span style="color: #660066;">networkError</span><span style="color: #009900;">&#40;</span>xhr.<span style="color: #000066;">status</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> failMsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>method<span style="color: #339933;">,</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;contentType&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;application/json; charset=utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// set authentication</span>
        username <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">App</span>.<span style="color: #660066;">Properties</span>.<span style="color: #660066;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        password <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">App</span>.<span style="color: #660066;">Properties</span>.<span style="color: #660066;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Authorization'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'Basic '</span><span style="color: #339933;">+</span>Ti.<span style="color: #660066;">Utils</span>.<span style="color: #660066;">base64encode</span><span style="color: #009900;">&#40;</span>username<span style="color: #339933;">+</span><span style="color: #3366CC;">':'</span><span style="color: #339933;">+</span>password<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        data <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
*  Parse JSON data - could be replaced with JSON2.js library
*/</span>
myFunctions.<span style="color: #660066;">jsonParse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> json <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
*  display alert to user
*/</span>
myFunctions.<span style="color: #660066;">notice</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">,</span>title<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createAlertDialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        title<span style="color: #339933;">:</span> title<span style="color: #339933;">,</span>
        message<span style="color: #339933;">:</span> msg
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>The key to the above is the callBack function parameter that is passed to the networkRequest function and call in line 41.</p><p>Now for the login.js file that Titanium is using to display the current window modal view to login a user.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;">Ti.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;common.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> win <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span><span style="color: #339933;">;</span>
&nbsp;
win.<span style="color: #660066;">setBackgroundColor</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#fff'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
win.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Login&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> actInd <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createActivityIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    bottom<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
    style<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">iPhone</span>.<span style="color: #660066;">ActivityIndicatorStyle</span>.<span style="color: #660066;">BIG</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> u <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTextField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span>
    value<span style="color: #339933;">:</span><span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    color<span style="color: #339933;">:</span><span style="color: #3366CC;">'#336699'</span><span style="color: #339933;">,</span>
    returnKeyType<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">RETURNKEY_NEXT</span><span style="color: #339933;">,</span>
    keyboardType<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">KEYBOARD_ASCII</span><span style="color: #339933;">,</span>
    hintText<span style="color: #339933;">:</span><span style="color: #3366CC;">'Email Address'</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span>
    clearOnEdit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    fontSize<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
    borderStyle<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BORDERSTYLE_ROUNDED</span><span style="color: #339933;">,</span>
    clearButtonMode<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BUTTONMODE_ALWAYS</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span><span style="color: #CC0000;">250</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>u<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
u.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'return'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    p.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createTextField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span>
    value<span style="color: #339933;">:</span><span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>
    color<span style="color: #339933;">:</span><span style="color: #3366CC;">'#336699'</span><span style="color: #339933;">,</span>
    returnKeyType<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">RETURNKEY_GO</span><span style="color: #339933;">,</span>
    keyboardType<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">KEYBOARD_ASCII</span><span style="color: #339933;">,</span>
    hintText<span style="color: #339933;">:</span><span style="color: #3366CC;">'Password'</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span>
    passwordMask<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    clearOnEdit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    fontSize<span style="color: #339933;">:</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
    borderStyle<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BORDERSTYLE_ROUNDED</span><span style="color: #339933;">,</span>
    clearButtonMode<span style="color: #339933;">:</span>Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">INPUT_BUTTONMODE_ALWAYS</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span><span style="color: #CC0000;">250</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> submit_button <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    id<span style="color: #339933;">:</span><span style="color: #3366CC;">'submit_button'</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Login'</span><span style="color: #339933;">,</span>
    color<span style="color: #339933;">:</span><span style="color: #3366CC;">'#336699'</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span><span style="color: #CC0000;">32</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span>
    fontSize<span style="color: #339933;">:</span><span style="color: #CC0000;">12</span><span style="color: #339933;">,</span>
    fontWeight<span style="color: #339933;">:</span><span style="color: #3366CC;">'bold'</span><span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span><span style="color: #CC0000;">150</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>submit_button<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> navActInd <span style="color: #339933;">=</span> Titanium.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createActivityIndicator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
win.<span style="color: #660066;">setRightNavButton</span><span style="color: #009900;">&#40;</span>navActInd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    top<span style="color: #339933;">:</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
    color<span style="color: #339933;">:</span><span style="color: #3366CC;">'#777'</span><span style="color: #339933;">,</span>
    height<span style="color: #339933;">:</span><span style="color: #3366CC;">'auto'</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
    font<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
        fontSize<span style="color: #339933;">:</span><span style="color: #CC0000;">15</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>label<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
* This is where the networkRequest function is going to be used.
*/</span>
submit_button.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    u.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    p.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">||</span> u.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        myFunctions.<span style="color: #660066;">notice</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter both username and password&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Login Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    navActInd.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    failMsg <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Username/Password Failed&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Login Error&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    r <span style="color: #339933;">=</span> myFunctions.<span style="color: #660066;">remoteRequest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;user/verify?&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>callBack_login<span style="color: #339933;">,</span>failMsg<span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        navActInd.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
* Once the XHR request is complete this function will be called.  You can put all your post data display events within this function.
*/</span>
<span style="color: #003366; font-weight: bold;">var</span> callBack_login <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Ti.<span style="color: #660066;">App</span>.<span style="color: #660066;">Properties</span>.<span style="color: #660066;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span> u.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ti.<span style="color: #660066;">App</span>.<span style="color: #660066;">Properties</span>.<span style="color: #660066;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span> p.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     navActInd.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">currentWindow</span>.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ti.<span style="color: #660066;">UI</span>.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>In line 103 we call the myFunctions.remoteRequest() function.  Notice that the <em>callBack_login</em> does not include <em>&#8220;</em>&#8217;s.</p><p>So far this solution has been working great for our development.  We hope to see Titanium Mobile support synchronous requests in the future, but our current modular development allows us make the switch quite easily.</p><p>I am open to any questions or comments that you may have about our solution.</p><p>No related posts.</p>]]></content:encoded> <wfw:commentRss>http://bradvernon.com/2010/03/titanium-mobile-synchronous-network-request-workaround/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>ZendCon 2008: Day 2 Review</title><link>http://bradvernon.com/2008/09/zendcon-2008-day-2-review/</link> <comments>http://bradvernon.com/2008/09/zendcon-2008-day-2-review/#comments</comments> <pubDate>Thu, 18 Sep 2008 18:37:47 +0000</pubDate> <dc:creator>Brad Vernon</dc:creator> <category><![CDATA[Events]]></category> <category><![CDATA[ajaxian]]></category> <category><![CDATA[iphone]]></category> <category><![CDATA[magento]]></category> <category><![CDATA[phpunit]]></category> <category><![CDATA[selenium]]></category> <category><![CDATA[terry chay]]></category> <category><![CDATA[twitter]]></category> <category><![CDATA[zendcon]]></category><guid
isPermaLink="false">http://bradvernon.com/?p=7</guid> <description><![CDATA[Yesterday I finished up my second day at ZendCon and learned a few more things:ZendCon should be renamed to ZendFrameworkCon for next year
Most talks are high-level presentations and very few show the code that matters to me and presumably most programmers attending
Terry Chay is still one of the best presenters I have seen (note: I [...]Related posts:<ol><li><a
href='http://bradvernon.com/2008/09/zendcon-2008-day-1-review/' rel='bookmark' title='Permanent Link: ZendCon 2008: Day 1 Review'>ZendCon 2008: Day 1 Review</a></li><li><a
href='http://bradvernon.com/2009/10/zendcon-2009-day-1-review/' rel='bookmark' title='Permanent Link: ZendCon 2009: Day 1 Review'>ZendCon 2009: Day 1 Review</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Yesterday I finished up my second day at ZendCon and learned a few more things:</p><ol><li>ZendCon should be renamed to ZendFrameworkCon for next year</li><li>Most talks are high-level presentations and very few show the code that matters to me and presumably most programmers attending</li><li><a
href="http://terrychay.com/blog">Terry Chay</a> is still one of the best presenters I have seen (note: I did not count the # of swear words he used, but if you want to check search for <a
href="http://search.twitter.com/search?q=%23tcfc">#tcfc</a> on <a
href="http://twitter.com">Twitter</a></li><li>I clearly need to learn how to use SPL and Iterators more in my code</li><li><a
href="http://selenium.openqa.org">Selenium</a> and <a
href="http://www.phpunit.de/ ">PHPUnit</a> are testing suites I need to work more with and properly implement</li><li>I finally have a reason to move to Zend Studio 6 over Zend Studio 5.5 that I have been using for the last two years</li><li>You can control a presentation using your <a
href="http://www.apple.com/iphone">iPhone</a></li><li>The <a
href="http://www.ajaxian.com">Ajaxian</a> guys not only make a great website and podcast their presentations are beautiful.</li></ol><p>Here are the sessions I attending on Wednesday:</p><ul><li><span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/225">Magento eCommerce and the Next Generation of PHP Applications</a></span></li><li> <abbr
class="dtstart" title="20080917T0945" /> <abbr
class="dtend" title="20080917T1045" /> <span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/51">Pick Your Protocol: Creating Web Services with Zend Framework</a></span></li><li><span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/199">Lesser Known Security Problems in PHP Applications</a></span></li><li><span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/211">State of Ajax</a></span></li><li><span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/210">PHP and AJAX Made Easier with Zend</a></span></li><li> <abbr
class="dtstart" title="20080917T1715" /> <abbr
class="dtend" title="20080917T1815" /> <span
class="summary"><a
class="url uid" href="http://www.zendcon.com/ZendCon08/public/schedule/detail/174">Zend_Tool: Rapid Application Development with Zend Framework</a></span> <span
class="description"> </span></li></ul><p>Related posts:<ol><li><a
href='http://bradvernon.com/2008/09/zendcon-2008-day-1-review/' rel='bookmark' title='Permanent Link: ZendCon 2008: Day 1 Review'>ZendCon 2008: Day 1 Review</a></li><li><a
href='http://bradvernon.com/2009/10/zendcon-2009-day-1-review/' rel='bookmark' title='Permanent Link: ZendCon 2009: Day 1 Review'>ZendCon 2009: Day 1 Review</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://bradvernon.com/2008/09/zendcon-2008-day-2-review/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching 13/32 queries in 0.012 seconds using disk

Served from: bradvernon.com @ 2010-07-30 14:40:15 -->