{"id":120,"date":"2012-02-07T21:19:55","date_gmt":"2012-02-08T04:19:55","guid":{"rendered":"http:\/\/www.petesworkshop.com\/blog_wp\/?p=120"},"modified":"2012-05-08T11:42:50","modified_gmt":"2012-05-08T17:42:50","slug":"mobile-application-development-and-ibm-i-part-four-mobilize-me","status":"publish","type":"post","link":"https:\/\/www.petesworkshop.com\/blog_wp\/?p=120","title":{"rendered":"Mobile application development and IBM i (Part four) &#8211; Mobilize me!"},"content":{"rendered":"<p>So we have this great little 5250 application which is now just an ugly web application.\u00a0 Truth is, this thing is so lightweight that as a web application, it is pretty snappy.\u00a0 But, not ALL applications are going to be this light weight and unless your standards are as low as mine when it comes to GUI, you&#8217;ll want to make this a *little* prettier.\u00a0 Here is where we are, so far:\u00a0 http:\/\/opensourceoni.com\/mobilerem\/cgi_punch.pgm<\/p>\n<p>The great news is that with CSS3 and HTML5 the road from ugly to beautiful is short and sweet.\u00a0 We just need to add a bit of css and perhaps a javascript library to improve things.\u00a0 But there is some redesign needed as well, especially since we are now going to add some more heft to the size of the application.\u00a0 Our original design had two separate CGI programs which basically presented the two screens of information.\u00a0 In each case a page is constructed at the server end and sent back to the browser to be rendered.\u00a0 Not a great design since a large portion of the page really doesn&#8217;t change.\u00a0 And imagine if we had headers, footers and graphics on the page as well.\u00a0 All that back and forth can eat bandwidth and breaks our &#8220;good citizen&#8221; rule.\u00a0 So we need a new approach.<\/p>\n<p>First, we are going to change from multiple pages to a single page.\u00a0 In jQuery mobile, the &#8220;trick&#8221; that is used is to define a div and treat it as if it were a page.\u00a0 With jQuery, jQuery mobile and the css added to the file, we can start to redesign the page like so:<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;div data-role=&#8221;page&#8221; id=&#8221;main&#8221; data-theme=&#8221;b&#8221; &gt;<\/p>\n<p>&lt;div&gt;<\/p>\n<p>&lt;div data-role=&#8221;page&#8221; id=&#8221;punches&#8221; data-theme=&#8221;b&#8221; &gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>That creates two &#8220;pages&#8221; for jQuery mobile to work with.\u00a0 The library takes care of the heavy lifting of showing and hiding the pages as we &#8220;page&#8221; back and forth.\u00a0 Make sense?<\/p>\n<p>For the &#8220;main&#8221; page, the first page we see that has the last punch and current time and date information as well as the buttons, there really isn&#8217;t all that much difference between the &#8220;classic&#8221; CGI page and our mobile page.\u00a0 We have a table with our information formatted into it.\u00a0 The &#8220;punches&#8221; page, where we list the information on the punches returned from the server, hasn&#8217;t changed all that much either except on both pages the \/% %\/ variable delimiters are gone because we are not making the round trip to the server so our CGI program no longer has to dump the information into the variables.\u00a0 So how, you ask, are we going to get data into the HTML forms?\u00a0 Ah! For that we use the magic of AJAX, JSON and the jQuery libraries.<\/p>\n<p><strong>AJAX<\/strong><\/p>\n<p>AJAX is the easiest part to understand.\u00a0 All AJAX does is send a small bit of data to the server (think of it as a miniscule web page) which the server reads and executes.\u00a0 That execution may return data, or it may not (usually does).\u00a0 The server packages up the data and sends it back to the AJAX requestor. At that point, the function that called the AJAX deals with the response.<\/p>\n<p><strong>JSON<\/strong><\/p>\n<p>The second piece, JSON, is a little harder to understand.\u00a0 Basically, JSON is just a string of data in a particular format.\u00a0 Basically the format is this:\u00a0 { &#8220;name&#8221;:value}.\u00a0 Deceptively simple but very powerful because if you know anything about JavaScript you know that looks very similar to an object definition in JavaScript.\u00a0 So, as an example, if we wanted to create an object called Person and add some attributes to it you would do it something like this:<\/p>\n<p>personObj=new Object();<br \/>\npersonObj.firstname=&#8221;John&#8221;;<br \/>\npersonObj.lastname=&#8221;Doe&#8221;;<br \/>\npersonObj.age=50;<br \/>\npersonObj.eyecolor=&#8221;blue&#8221;;<\/p>\n<p>But we could also do it like this:<br \/>\npersonObj={firstname:&#8221;John&#8221;,lastname:&#8221;Doe&#8221;,age:50,eyecolor:&#8221;blue&#8221;};<\/p>\n<p>And when we needed to get values from the object we would do it like so:<\/p>\n<p>var first = personObj.firstname;<\/p>\n<p>It shouldn&#8217;t take much of your brain&#8217;s CPU cycles to think: Hey, wait a minute!\u00a0 If I had a string formatted like so on the server: &#8216;{firstname:&#8221;John&#8221;,lastname:&#8221;Doe&#8221;,age:50,eyecolor:&#8221;blue&#8221;}&#8217; and I returned that string to my web page with an AJAX call,\u00a0 I can create objects of data on the client without much effort!\u00a0 Pseudocode would be something like:<br \/>\nvar personObj = getJsonFromAjax();\u00a0 Nice!\u00a0 That is EXACTLY what we will do with a little help from jQuery.<\/p>\n<p><strong>jQuery<\/strong><\/p>\n<p>If we want to start hooking some of this stuff up, let&#8217;s begin by assuming that we have returned a JSON formatted string to our client application.\u00a0 jQuery pretty much handles all of the heavy lifting:<\/p>\n<p>jsonURL =\u00a0 &#8216;cgi_mobile.pgm?action=getList&#8217;;\u00a0 \/\/ URL that calls our CGI program on the server<\/p>\n<p>$.ajax({<br \/>\nurl: jsonURL,<br \/>\nasync: false,<br \/>\ncache: false,<br \/>\nsuccess: function(data){<\/p>\n<p>try<br \/>\n{<br \/>\njsonContent = $.parseJSON(data);<br \/>\n}<br \/>\ncatch(err)<br \/>\n{<br \/>\nalert(&#8216;(JSON exception)&#8217;);<br \/>\nreturn false;<br \/>\n}<br \/>\n}<\/p>\n<p>This code snippet would call the cgi_mobile.pgm and pass the action &#8216;getList&#8217; and then the program would return that list in a JSON formatted string which is parsed back to an object using the $.parseJSON function in jQuery.\u00a0 You can take a look at the program source but basically the CGI program simply formats the data using a &#8216;json&#8217; template and sends back the result which is a JSON string.\u00a0 It does it EXACTLY the way it would take an HTML template and send back HTML.\u00a0 You can take a look at the jsonlist.json template but there isn&#8217;t anything new except it formats the data as a JSON string.<\/p>\n<p>That string is actually an array of JSON objects (because that is the way I formatted it!) so now I have an array of objects and those objects are &#8220;punches&#8221;.\u00a0 It looks like this:<\/p>\n<p>[ { &#8220;lastPunch&#8221;: &#8220;IN&#8221;, &#8220;punchDate&#8221;: &#8220;12\/30\/11&#8221;, &#8220;punchTime&#8221;:&#8221;07:40:38&#8243; } ,{ &#8220;lastPunch&#8221;: &#8220;IN&#8221;, &#8220;punchDate&#8221;: &#8220;12\/30\/11&#8221;, &#8220;punchTime&#8221;:&#8221;08:07:43&#8243;}]\u00a0 In fact, you can see the &#8220;raw&#8221; data just by calling that action in your browser:<br \/>\nhttp:\/\/opensourceoni.com\/mobilerem\/cgi_mobile.pgm?action=getList<\/p>\n<p>Nothing special.\u00a0 AND since we now have an object we can iterate through and format our table on the client side with a little bit of javascript:<\/p>\n<p>for(var i =0;i&lt;jsonContent.length;i++)<br \/>\n{<br \/>\nvar rec = jsonContent[i];<br \/>\nhtml = html + &#8216;&lt;tr&gt;&lt;td&gt;&lt;input type=text name=lastPunch id=lastPunch value=&#8217;+rec.lastPunch+&#8217; readonly&gt;&lt;\/td&gt;&#8217;<br \/>\nhtml = html + &#8216;&lt;td&gt;&lt;input type=text name=punchDate id=punchDate value=&#8217;+rec.punchDate+&#8217; readonly&gt;&lt;\/td&gt;&#8217;<br \/>\nhtml = html + &#8216;&lt;td&gt;&lt;input type=text name=punchTime id=punchTime id=punchTime value=&#8217;+rec.punchTime+&#8217; readonly&gt;&lt;\/td&gt;&lt;\/tr&gt;&#8217;<\/p>\n<p>}<\/p>\n<p>Once we have that HTML constructed, jQuery just let&#8217;s us stuff the html into the empty body of the table in our &#8220;punches&#8221; &#8220;page&#8221;:<\/p>\n<p>$(&#8220;#punchList tbody&#8221;).html(html);<\/p>\n<p>And then we change the &#8220;page&#8221; (basically hiding the div we are on and showing the div named &#8220;punches&#8221;)<br \/>\n$.mobile.changePage(&#8220;#punches&#8221;,&#8217;slide&#8217;,false,true);<\/p>\n<p>Not a whole lot of magic here.\u00a0 Just taking advantage of the capabilities of AJAX, JSON, and jQuery.<\/p>\n<p>Believe it or not, the hard part is done.\u00a0 We can easily take this mobile web application and turn it into a mobile &#8220;native&#8221; application using PhoneGap.\u00a0 But, that will have to wait until next week (or so).\u00a0 Remember that you can get this code right <a title=\"Mobile Application Development Code (V6R1)\" href=\"http:\/\/www.petesworkshop.com\/downloads\/mobilerem.zip\">here<\/a>.<\/p>\n<p>OH!\u00a0 I missed giving you a link to the actual web application.\u00a0 You will find that here: http:\/\/opensourceoni.com\/mobilerem\/cgi_mobile.pgm<\/p>\n<p>Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So we have this great little 5250 application which is now just an ugly web application.\u00a0 Truth is, this thing is so lightweight that as a web application, it is pretty snappy.\u00a0 But, not ALL applications are going to be &hellip; <a href=\"https:\/\/www.petesworkshop.com\/blog_wp\/?p=120\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[36,37,5,34,38,31,35],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-programming","tag-android","tag-html5","tag-ibm-i","tag-mobile-development","tag-phonegap","tag-php","tag-rpg-ile"],"_links":{"self":[{"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":8,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.petesworkshop.com\/blog_wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}