{"id":939,"date":"2017-12-04T15:16:41","date_gmt":"2017-12-04T15:16:41","guid":{"rendered":"http:\/\/bitcows.com\/?p=939"},"modified":"2017-12-04T15:16:41","modified_gmt":"2017-12-04T15:16:41","slug":"learning-appcelerator-part-2","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=939","title":{"rendered":"Learning Appcelerator Part 2 (ListViews and Models)"},"content":{"rendered":"<p>In the first tutorial we learned how to create a simple bio metric login screen. Building on that example we can now do a follow up screen that will transition to a list of Users. <\/p>\n<p><a href=\"https:\/\/github.com\/djmason9\/Appcelerator-BioAuth\/tree\/BaseApplicationMockData\">Get the code for this Demo<\/a><\/p>\n<p>Building on the previous tutorial  we will be adding a new View called <code>userList.xml<\/code> This is pretty boiler plate code that contains a <code>ListView<\/code>. The important thing to note in this file is the <code>Collection<\/code> tag. This is what will instantiate the model and get it ready to pull data out of it. When we do it like this our controller can then call fetch and retrieve the list of users. We also are using a templating syntax in for <code>{Status}<\/code> and <code>{Name}<\/code>. Lastly to tell the list to use our model we will add <code>dataCollection<\/code> to our <code>ListSection<\/code>.<\/p>\n<h4>VIEW<\/h4>\n<h5>userList.xml<\/h5>\n<pre class=\"prettyprint\">\n&lt;Alloy&gt;\n\t&lt;Collection src=\"userModel\"\/&gt;\n\t&lt;Window class=\"container\" id=\"userList\"&gt;\n\t\t&lt;ListView width=\"100%\" onItemclick=\"userDetail\" defaultItemTemplate=\"listTemplate\"&gt;\n\t\t\t &lt;Templates&gt;\n                &lt;ItemTemplate id=\"detailTemplate\" name=\"listTemplate\"&gt;\n                    &lt;ImageView bindId=\"status\" class=\"rowStatus\" text=\"\"\/&gt;\n                    &lt;Label bindId=\"name\" class=\"rowUserName\" text=\"\"\/&gt;\n                &lt;\/ItemTemplate&gt;\n            &lt;\/Templates&gt;\n\t\t\t&lt;HeaderView&gt;\n\t\t\t\t&lt;View backgroundColor=\"#ddd\" height=\"30\"&gt;\n\t\t\t\t\t&lt;Label class=\"status\"&gt;Online&lt;\/Label&gt;\n\t\t\t\t\t&lt;Label class=\"userName\"&gt;User Name&lt;\/Label&gt;\n\t\t\t\t&lt;\/View&gt;\n\t\t\t&lt;\/HeaderView&gt;\n\t\t\t&lt;ListSection dataCollection=\"userModel\"&gt;\n\t\t\t\t&lt;ListItem  status:image=\"\/images\/{status}.png\" name:text=\"{name}\" height=\"40\" \/&gt;\n\t\t\t&lt;\/ListSection&gt;\n\t\t&lt;\/ListView&gt;\n\t&lt;\/Window&gt;\n&lt;\/Alloy&gt;\n<\/pre>\n<p>Like in our previous example we will do some styling for our list.<\/p>\n<h5>userList.tss<\/h5>\n<pre class=\"prettyprint\">\n\".container\" : {\n  top:75\n},\n\".status\" : {\n left : 5\n},\n\".userName\" : {\n left : 75\n},\n\".rowStatus\" : {\n left : 10,\n width : 25,\n height: 25\n},\n\".rowUserName\" : {\n left : 75\n}\n<\/pre>\n<h4>CONTROLLER<\/h4>\n<p>For our userList controller we are going to call <code>fetch()<\/code> Fetch is a built in <a href=\"http:\/\/backbonejs.org\/#Model-fetch\">backbone.js<\/a> function that will pull data from our model using the built in Sync functionality of backbone.<\/p>\n<h5>userList.js<\/h5>\n<pre class=\"prettyprint\">\nAlloy.Collections.userModel.fetch(); \n<\/pre>\n<p>Of course in order to get to our list well need to change the login file <code>index.js<\/code> from an alert to a way to transition to our new screen. Here we grab our userList controller view and we want to call <code>open()<\/code> to transition to it. We are going to make the screen do a flip transition from Left to right at 1\/2 second. Now you will see an condition here because the built in transition on Android won&#8217;t flip so in this case we just want to slide it in for Android.<\/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();\n        if (OS_IOS) {\n            win1.open({\n                transition: Ti.UI.iOS.AnimationStyle.FLIP_FROM_LEFT,\n                duration: 500\n            });\n        } else {\n            win1.open();\n        }\n\n    } else {\n        alert(\"You have failed to login!\" + Alloy.Globals.isIphoneX);\n    }\n\n};\n<\/pre>\n<p>In our Alloy.js file we need to set up our data so we can display it. Since Alloy.js gets called first before all our controllers this is the perfect place to add our mock up data. In this case we have a static file with some json data in it. So we can grab this data by looking through the file system and grabbing it. Then we will parse it and add it to our mocx collection. <a href=\"https:\/\/github.com\/jasonkneen\/mocx\">For more on how this file works<\/a>. It basically overrides the backbone.js Sync function to add data to its collection.<\/p>\n<h5>assets\/mockData\/alloy.js<\/h5>\n<pre class=\"prettyprint\">\nvar mocx = require(\"mocx\");\n\nAlloy.Globals.isIphoneX = (Ti.Platform.displayCaps.platformHeight == 812);\n\n(function constructor(){\n\n\/\/ Create Mock Users from fake data\nvar file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'mockData\/users.json');\n\tpreparse = file.read().text,\n\tresponse = JSON.parse(preparse);\n    \n\tmocx.createCollection(\"userModel\", response.UserStats);\n})();\n<\/pre>\n<h4>MODEL<\/h4>\n<p>The model code is pretty boiler plate too. When you create a new model using Appc Studio it will build this file for you. The only things you will need to make sure are correct are the collection_name and depending on your json structure you might need to add a <code>parentNode<\/code> function. In our case we have a parent node of <code>UserStats<\/code>. If we didn&#8217;t have that we wouldn&#8217;t need to add that parentNode.<\/p>\n<h5>userModel.js<\/h5>\n<pre class=\"prettyprint\">\nexports.definition = {\n\tconfig: {\n\t\tadapter: {\n\t\t\ttype: \"sql\", \/\/ <-- name of custom adapter\n\t\t\tcollection_name: \"userModel\"\n\t\t},\n\t\t\/\/ Gets the parent node of our Array\n        parentNode: function(data) {\n            data = data || [];\n            return data.UserStats || data;\n        }\n\t},\n\textendModel: function(Model) {\n\t\t_.extend(Model.prototype, {\t\t\t\n\t\t});\n\t\treturn Model;\n\t},\n\textendCollection: function(Collection) {\n\t\t_.extend(Collection.prototype, {\n\t\t});\n\t\treturn Collection;\n\t}\n};\n<\/pre>\n<p>Our fake data. <\/p>\n<h4>user.json<\/h4>\n<pre class=\"prettyprint\">{\n\t\"UserStats\": [\n\t\t{\n\t\t\t\"name\": \"Darren\",\n\t\t\t\"status\": \"online\"\n\t\t},{\n\t\t\t\"name\": \"Aaron\",\n\t\t\t\"status\": \"online\"\n\t\t},{\n\t\t\t\"name\": \"Bill\",\n\t\t\t\"status\": \"offline\"\n\t\t},{\n\t\t\t\"name\": \"Steve\",\n\t\t\t\"status\": \"online\"\n\t\t}\n\t]\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the first tutorial we learned how to create a simple bio metric login screen. Building on that example we can now do a follow up screen that will transition to a list of Users. Get the code for this Demo Building on the previous tutorial we will be adding a new View called userList.xml&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=939\" 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],"tags":[],"class_list":["post-939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-appcelerator","category-ios","category-code-examples"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/939","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=939"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}