{"id":962,"date":"2017-12-05T22:32:29","date_gmt":"2017-12-05T22:32:29","guid":{"rendered":"http:\/\/bitcows.com\/?p=962"},"modified":"2017-12-05T22:32:29","modified_gmt":"2017-12-05T22:32:29","slug":"learning-appcelerator-part-3","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=962","title":{"rendered":"Learning Appcelerator Part 3 (Model and Navigation Controller)"},"content":{"rendered":"<p>Building on our last two tutorials now we will add more detail to our app. We are going to introduce a Navigation Controller in this and a new Model.<\/p>\n<p><a href=\"https:\/\/github.com\/djmason9\/Appcelerator-BioAuth\/tree\/BaseAppMockWithDetails\">Get the code<\/a><\/p>\n<p>Adding new files<br \/>\n<code>userListNav.xml<\/code><br \/>\n<code>userDetails.xml<\/code><br \/>\n<code>userDetails.js<\/code><br \/>\n<code>userDetails.tss<\/code><br \/>\n<code>details.js<\/code><\/p>\n<h4>CONTROLLER<\/h4>\n<p>We are going to change the doAuth function to support our NavigationWindow on both platforms<\/p>\n<h5>index.js<\/h5>\n<pre class=\"prettyprint\">\nTiAuth.doAuth = function(didPassBio) {\n    if (password == $.passWrdTxt.value && userName == $.userNmTxt.value || didPassBio) {\n\n        var win1 = Alloy.createController(\"userList\").getView(); \/\/android \n        var opt = {};\n\n        if (OS_IOS) {\n            win1 = Alloy.createController(\"userListNav\").getView(); \/\/ios\n            opt = {\n                transition: Ti.UI.iOS.AnimationStyle.FLIP_FROM_LEFT,\n                duration: 500\n            };\n        }\n\n        win1.open(opt);\n\n    } else {\n        alert(\"You have failed to login!\");\n    }\n\n};\n<\/pre>\n<p>Update <code>userList.js<\/code><br \/>\nWe are adding a userDetail function to allows to navigate to the next screen. Here we will call the next screen userDetails and pass some info to it from our json object pulled from the collection.<\/p>\n<h5>userList.js<\/h5>\n<pre class=\"prettyprint\">\n(function main() {\n\t\/\/ Hide nav bar\n\tif (OS_IOS) {\n\t\t$.userList.hideNavBar(true, {\n\t\t\tanimated : false\n\t\t});\n\t}\n\n\tAlloy.Collections.userModel.fetch();\n})();\n\nfunction userDetail(e) {\n        \/\/put our model on the controller so we can grab them in the UI\n\tvar opts = {\n\t\t$model : Alloy.Collections.userModel.at(e.itemIndex)\n\t},\n\tcontrollerNm = \"userDetails\";\n\n\tif (OS_IOS) {\n\t\tAlloy.Globals.pushViewOnController(controllerNm, opts);\n\t} else {\n\t\tvar win1 = Alloy.createController(controllerNm, opts).getView();\n\t\twin1.open();\n\t}\n\t\n}\n<\/pre>\n<h4>VIEW<\/h4>\n<p>Starting with our new file userListNav we will had a new Component called NavigationWindow. This is an iOS Component that implements a specialized view that manages the navigation of hierarchical content. Android handles this differently so we need to use the <code>platform<\/code> attribute to differentiate between the functionality. Then we can &#8220;include&#8221; the userList into our file so we don&#8217;t need to repeat the code in the userList.xml file for each condition. <\/p>\n<h5>userListNav.xml<\/h5>\n<pre class=\"prettyprint\">&lt;Alloy&gt;\n    &lt;NavigationWindow class=\"container\" id=\"userListNav\" platform=\"ios\"&gt;\n      &lt;Require src=\"userList\"\/&gt;\n   &lt;\/NavigationWindow&gt;\n&lt;\/Alloy&gt;<\/pre>\n<p>Adding a click handler to our list.<\/p>\n<h5>userList.xml<\/h5>\n<pre class=\"prettyprint\">&lt;ListView width=\"100%\" onItemclick=\"userDetail\" defaultItemTemplate=\"listTemplate\"&gt;<\/pre>\n<p>Here we templatized our data so we pull the values off the model that we have passed from the userList.js file.<\/p>\n<h5>userDetails.xml<\/h5>\n<pre class=\"prettyprint\">\n&lt;Alloy&gt;\n\t&lt;Window class=\"container\" id=\"userDetails\" title=\"User Details\"&gt;\n\t  &lt;View layout=\"vertical\"&gt;\n\t    &lt;Label text=\"NAME: {name}\"\/&gt;\n\t    &lt;Label text=\"STATUS: {status}\"\/&gt;\n\t    &lt;Label text=\"PHONE: {phone}\"\/&gt;\n\t    &lt;Label text=\"LOCATION: {location}\"\/&gt;\n          &lt;\/View&gt;\n\t&lt;\/Window&gt;\n&lt;\/Alloy&gt;\n<\/pre>\n<h4>DATA<\/h4>\n<p>Add a few fields to our mock data <code>Phone<\/code> and <code>Location<\/code><\/p>\n<h5>assets\/mockData\/users.json<\/h5>\n<pre class=\"prettyprint\">{\n \"name\": \"Darren\",\n \"status\": \"online\",\n \"location\" : \"Phoenix\",\n \"phone\" : \"555-1234\"\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Building on our last two tutorials now we will add more detail to our app. We are going to introduce a Navigation Controller in this and a new Model. Get the code Adding new files userListNav.xml userDetails.xml userDetails.js userDetails.tss details.js CONTROLLER We are going to change the doAuth function to support our NavigationWindow on both&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=962\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":649,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,15,16,18],"tags":[],"class_list":["post-962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-appcelerator","category-ios","category-code-examples","category-iphone"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=962"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}