{"id":1036,"date":"2018-04-16T16:55:10","date_gmt":"2018-04-16T16:55:10","guid":{"rendered":"http:\/\/bitcows.com\/?p=1036"},"modified":"2018-04-16T16:55:10","modified_gmt":"2018-04-16T16:55:10","slug":"using-es6-in-appcelerator","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=1036","title":{"rendered":"Using ES6 in Appcelerator"},"content":{"rendered":"<p>Starting with Titanium SDK 6.1.0, Appcelerator started supporting ES6. But you may be asking yourself, how do I use ES6 with Appcelerator? So here is a simple example on how to use some of what ES6 has to offer in your Appcelerator App. <\/p>\n<p><strong>ES6 Elements Used:<\/strong><\/p>\n<ul>\n<li>Fat arrow functions<\/li>\n<li>Classes<\/li>\n<li>Inheritance<\/li>\n<li>Template Literals<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/bitcows.com\/wp-content\/uploads\/2018\/03\/screenshot-577x1024.png\" alt=\"\" width=\"300\"\/><\/p>\n<p>Lets start with our Application class. This will get our <code>Window<\/code> element from <code>Alloy<\/code> and expose a couple of functions to add to the <code>Window<\/code> object and open it on load. <\/p>\n<pre class=\"prettyprint\">\nclass Application {\n   constructor(win) {\n     this.window = $.index; \/\/window defined in index.xml\n   }\n   open() {\n     this.window.open();\n   }\n   add(view){\n     this.window.add(view);\n   }\n}\n<\/pre>\n<p>Next lets create an AnimalView. This is really only to show that you are able to extend classes in ES6 and inherit elements from the parent class with ease. So in a class you always need a constructor so here we will add a few params to our constructor to keep things simple.<\/p>\n<p>Since this is a view we need to address how we get that view onto our application. So we will instantiate a new view by calling <code>Titanium.UI.createView<\/code> and assigning it to <code>this.view<\/code>. Our View also needs an image so we can do the same thing for our image. <code>Ti.UI.createImageView<\/code> and assign it to <code>this.imageView<\/code>. Next we want to add a sound function to hear what our animal sounds like. So lets create a fat arrow function and print out what the sound of or animal is. Lastly we will add our imageView to our View and move on to the next class.<\/p>\n<pre class=\"prettyprint\">\nclass AnimalView {\n   constructor(name, sound, type, imgSrc) {\n     this.name = name;\n     this.type = type;\n     this.sound = sound;\n\n     var playSound = () => {\n       console.log(this.sound);\n     }\n\n     this.view = Titanium.UI.createView({\n       layout : \"vertical\",\n       height : \"50%\"\n     });\n\n     var imageView = Ti.UI.createImageView({\n       image: imgSrc,\n       height : \"80%\"\n     });\n\n     imageView.addEventListener(\"click\", () => {playSound()});\n     this.view.add(imageView);\n   }\n}\n<\/pre>\n<p>This Class will be our concrete AnimalView Class in this case a BirdView. Again we start by adding a constructor and passing our Class some parameters. Since the BirdView extends AnimalView that means it inherits everything from the AnimalView we need to call super and pass the AnimalView some arguments, then give our BirdView some characteristics that belong only to a BirdView, such as color and hobbies. Now the BirdView has a special label explaining the attributes of the BirdView so lets create that label. <code>Ti.UI.createLabel<\/code> and assign it to <code>this.label<\/code>. Next we need to add that label to the view. Remember this.view is inherited from the AnimalView so that when we call <code>this.view.add(this.label)<\/code> its actually adding it to the parent class AnimalView&#8217;s View. Note inside our createLabel we are using Template Literals. This allows us to put in values with out having to concatenate strings. <\/p>\n<pre class=\"prettyprint\">\nclass BirdView extends AnimalView {\n   constructor(name, color, type, hobby){\n     super(name, \"tweet\", type, \"\/images\/bird.jpg\");\n       this.color = color;\n       this.hobby = hobby;\n\n       this.label = Ti.UI.createLabel({\n         text : `${this.name} the ${this.type} is ${this.color} \\nenjoys ${this.hobby} and goes: ${this.sound}`\n       });\n\n       this.view.add(this.label);\n   }\n}\n<\/pre>\n<p>Lastly, we need to add all our views together and add them to our application. Here we create a new instance of Application and add our BirdView to it. Then we call open on our Application which opens the window. <\/p>\n<pre class=\"prettyprint\">\n\/\/open view\nlet app = new Application();\napp.add(new BirdView(\"Steve\", \"yellow\", \"bird\", \"hiking\"));\napp.open();\n<\/pre>\n<p>This is what we would have in our Alloy file <code>index.xml<\/code> NOTE: this is not needed but I wanted to show that you could use Alloy in conjunction with Classes. If you didn&#8217;t want to use this you could just as easily do this<br \/>\n<code>this.window = Ti.UI.createWindow();<\/code> to create the instance of the window. <\/p>\n<pre class=\"prettyprint\">\n&lt;Alloy&gt;\n   &lt;Window class=\"container\" layout=\"vertical\" top=\"50\"\/&gt;\n&lt;\/Alloy&gt;\n<\/pre>\n<p><strong><a href=\"https:\/\/gist.github.com\/djmason9\/5f23ba82a6117ac41beba3bc087b7480\" target=_new\">GitList Link<\/a><\/strong><\/p>\n<p><a href=\"https:\/\/bitbucket.org\/tst-innovation-lab\/es6project\/\">Download Full Project<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting with Titanium SDK 6.1.0, Appcelerator started supporting ES6. But you may be asking yourself, how do I use ES6 with Appcelerator? So here is a simple example on how to use some of what ES6 has to offer in your Appcelerator App. ES6 Elements Used: Fat arrow functions Classes Inheritance Template Literals Lets start&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=1036\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1161,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,15,18,20],"tags":[30,32,49,50,52],"class_list":["post-1036","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-appcelerator","category-ios","category-iphone","category-javascript","tag-android","tag-appc","tag-ios-2","tag-javascript","tag-mobile"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/1036","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=1036"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/1036\/revisions"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}