{"id":621,"date":"2017-05-03T20:36:54","date_gmt":"2017-05-03T20:36:54","guid":{"rendered":"http:\/\/bitcows.com\/?p=621"},"modified":"2017-05-03T20:36:54","modified_gmt":"2017-05-03T20:36:54","slug":"appcelerator-extending-ui-elements","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=621","title":{"rendered":"Appcelerator extending UI elements"},"content":{"rendered":"<p>If you have every used <code>&lt;NavigationWindow&gt<\/code> in your Alloy code you know that it will only work with iOS. But what if you don&#8217;t want to have to duplicate all your code for android?<\/p>\n<pre class=\"prettyprint\">\n\/\/ index.xml\n&lt;Alloy&gt;\n    &lt;NavigationWindow id=\"win1\" platform=\"ios\"&gt;\n        &lt;Window class=\"container\"&gt;\n            &lt;View&gt;\n                &lt;Button onClick=\"showDetails\"&gt;Show Details&lt;\/Button&gt;\n            &lt;\/View&gt;\n        &lt;\/Window&gt;\n    &lt;\/NavigationWindow&gt;\n    \/\/ Terrible we are duplicating code!!\n    &lt;Window class=\"container\" platform=\"android\"&gt;\n        &lt;View&gt;\n            &lt;Button onClick=\"showDetails\"&gt;Show Details&lt;\/Button&gt;\n        &lt;\/View&gt;\n    &lt;\/Window&gt;\n&lt;\/Alloy&gt;<\/pre>\n<p>This is NOT GOOD!<\/p>\n<p>So then why not use and <strong>Require<\/strong> tag? Ok thats not so bad, but then we have to create a new subView.xml file, subView.tss, and subView.js controller for it.<\/p>\n<pre class=\"prettyprint\">\n\/\/ index.xml\n&lt;Alloy&gt;\n    &lt;Require platform=\"android\" src=\"subView\"\/&gt;\n    &lt;NavigationWindow id=\"win1\" platform=\"ios\"&gt;\n        &lt;Require src=\"subView\"\/&gt;\n    &lt;\/NavigationWindow&gt;\n&lt;\/Alloy&gt;\n\n\/\/ subView.xml\n&lt;Window class=\"container\"&gt;\n    &lt;View&gt;\n        &lt;Button onClick=\"showDetails\"&gt;Show Details&lt;\/Button&gt;\n    &lt;\/View&gt;\n&lt;\/Window&gt;\n<\/pre>\n<p>This is BETTER but its still&#8230;<\/p>\n<p>What about if we extended the <strong>NavigationWindow<\/strong>? Let see what that might look like.<\/p>\n<pre class=\"prettyprint\">\n\/\/ lib\/ui.js\nexports.createNavigationWindow = function(args) {\n    \/***\n     * This will be used for the Android Window\n     * @param {Object} args\n     *\/\n    function navigationWindow(args) {\n\n        this.open = function(options) {\n            args.window.open();\n        };\n\n        \/\/ Convert an iOS openWindow call to Window.open\n        this.openWindow = function(win) {\n            win.open();\n        };\n\n        \/\/ Convert an iOS closeWindow call to Window.close\n        this.closeWindow = function(win) {\n            win.close();\n        };\n    }\n\n    \/\/ Android returns the above function\n    if (OS_ANDROID) {\n        return new navigationWindow(args);\n    } else {\n        return Ti.UI.iOS.createNavigationWindow(args);\n    }\n}; \n\n\/\/ views\/index.xml\n\/\/ Add the module=\"ui\" to NavigationWindow\n&lt;Alloy&gt;\n    &lt;NavigationWindow module=\"ui\" id=\"win1\"&gt;\n        &lt;Window class=\"container\"&gt;\n            &lt;View&gt;\n                &lt;Button onClick=\"showDetails\"&gt;Show Details&lt;\/Button&gt;\n            &lt;\/View&gt;\n        &lt;\/Window&gt;\n    &lt;\/NavigationWindow&gt;\n&lt;\/Alloy&gt;\n\n<\/pre>\n<p>Ok now we&#8217;re talking, but what is this file doing? For starters we are exporting our own <strong>createNavigationWindow<\/strong> that our <code>&lt;NavigationWindow module=\"ui\" id=\"win1\"&gt;<\/code> tag will call first. <\/p>\n<p>In this way we intercept the call to the built in <code>Ti.UI.iOS.createNavigationWindow<\/code> and determine who&#8217;s calling it and modify the return value. If its <strong>iOS<\/strong> we just return the original <em>createNavigationWindow<\/em> since that is what iOS can use. <\/p>\n<p>If however, if it&#8217;s <strong>Android<\/strong> we will override the <em>createNavigationWindow<\/em> functions and divert them to the basic open or close functions of the Window class. This way when we call it we don&#8217;t need to use any sort of logic to determine which function to call.<\/p>\n<p>So finally when we call the <strong>openWindow<\/strong> it works with both platforms.<\/p>\n<pre class=\"prettyprint\">function showDetails() {\n    var win2 = Alloy.createController(\"details\").getView();\n    $.win1.openWindow(win2);\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have every used &lt;NavigationWindow&#038;gt in your Alloy code you know that it will only work with iOS. But what if you don&#8217;t want to have to duplicate all your code for android? \/\/ index.xml &lt;Alloy&gt; &lt;NavigationWindow id=&#8221;win1&#8243; platform=&#8221;ios&#8221;&gt; &lt;Window class=&#8221;container&#8221;&gt; &lt;View&gt; &lt;Button onClick=&#8221;showDetails&#8221;&gt;Show Details&lt;\/Button&gt; &lt;\/View&gt; &lt;\/Window&gt; &lt;\/NavigationWindow&gt; \/\/ Terrible we are duplicating code!!&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=621\" 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],"tags":[32],"class_list":["post-621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-appcelerator","category-ios","tag-appc"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/621","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=621"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/621\/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=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}