{ "id": "118710", "key": "ALOY-794", "fields": { "issuetype": { "id": "1", "description": "A problem which impairs or prevents the functions of the product.", "name": "Bug", "subtask": false }, "project": { "id": "11113", "key": "ALOY", "name": "Alloy", "projectCategory": { "id": "10400", "description": "Tools for developing applications", "name": "Tooling" } }, "fixVersions": [ { "id": "15758", "description": "Alloy 1.4.0", "name": "Alloy 1.4.0", "archived": false, "released": true, "releaseDate": "2014-07-17" } ], "resolution": { "id": "1", "description": "A fix for this issue is checked into the tree and tested.", "name": "Fixed" }, "resolutiondate": "2014-01-21T22:28:19.000+0000", "created": "2013-08-17T12:43:42.000+0000", "priority": { "name": "Medium", "id": "3" }, "labels": [ "alloy", "qe", "qe-closed-3.3.0", "qe-manualtest" ], "versions": [ { "id": "15477", "description": "Alloy 1.1.2", "name": "Alloy 1.1.2", "archived": false, "released": true, "releaseDate": "2013-05-02" } ], "issuelinks": [], "assignee": { "name": "tlukasavage", "key": "tlukasavage", "displayName": "Tony Lukasavage", "active": true, "timeZone": "America/Los_Angeles" }, "updated": "2014-11-20T18:20:27.000+0000", "status": { "description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.", "name": "Closed", "id": "6", "statusCategory": { "id": 3, "key": "done", "colorName": "green", "name": "Done" } }, "components": [ { "id": "12329", "name": "Runtime", "description": "Generic bucket for uncategorized runtime issues" } ], "description": "Q&A: http://developer.appcelerator.com/question/156082/alloy-compiler-conditional-code-not-work\r\n\r\nAdd code below in a controller code:\r\n\r\n{code:javascript}\r\nif(OS_ANDROID) {\r\n\tfunction aaa() {\r\n\t\tTi.API.info('android aaa');\r\n\t}\r\n}\r\nelse {\r\n\tfunction bbb() {\r\n\t\tTi.API.info('not android bbb');\r\n\t}\r\n}\r\n\r\nif(OS_IOS) {\r\n\tfunction aaa() {\r\n\t\tTi.API.info('ios aaa');\r\n\t}\r\n}\r\nelse {\r\n\tfunction bbb() {\r\n\t\tTi.API.info('not ios bbb');\r\n\t}\r\n}\r\n\r\nsetTimeout(aaa, 0);\r\nsetTimeout(bbb, 0);\r\n{code}\r\n\r\nThe compiled code is:\r\n{code:javascript}\r\n function aaa() {\r\n Ti.API.info(\"android aaa\");\r\n }\r\n function bbb() {\r\n Ti.API.info(\"not android bbb\");\r\n }\r\n function aaa() {\r\n Ti.API.info(\"ios aaa\");\r\n }\r\n function bbb() {\r\n Ti.API.info(\"not ios bbb\");\r\n }\r\n setTimeout(aaa, 0);\r\n setTimeout(bbb, 0);\r\n{code}", "attachment": [], "flagged": false, "summary": "Alloy Conditional code not functional", "creator": { "name": "aladdina", "key": "aladdina", "displayName": "Mason Zhang", "active": true, "timeZone": "Asia/Shanghai" }, "subtasks": [], "reporter": { "name": "aladdina", "key": "aladdina", "displayName": "Mason Zhang", "active": true, "timeZone": "Asia/Shanghai" }, "environment": "Mac OS X 10.7\r\nJava 1.6.0.51\r\nAlloy 1.2.0\r\nTi 3.1.2.GA", "comment": { "comments": [ { "id": "267050", "author": { "name": "tlukasavage", "key": "tlukasavage", "displayName": "Tony Lukasavage", "active": true, "timeZone": "America/Los_Angeles" }, "body": "As noted in the Q&A, this is an issue with putting function declarations inside a block, which is a bad practice in Javascript as function declarations have top-level scope. The fix for the issue is to properly structure the blocks using function expressions instead, like this:\n\n{code:javascript}\nvar aaa, bbb;\n \nif(OS_ANDROID) {\n aaa = function() {\n Ti.API.info('android aaa');\n }\n}\nelse {\n bbb = function() {\n Ti.API.info('not android bbb');\n }\n}\n \nif(OS_IOS) {\n aaa = function() {\n Ti.API.info('ios aaa');\n }\n}\nelse {\n bbb = function() {\n Ti.API.info('not ios bbb');\n }\n}\n \n$.index.open();\n \naaa();\nbbb();\n{code}\n\nSeeing as how these are supposed to be \"compiler\" conditionals, though, I will look into the possibility of having this work. I'm hesitant, though, and unlikely to change it as it could inadvertantly promote a bad Javascript practice.", "updateAuthor": { "name": "tlukasavage", "key": "tlukasavage", "displayName": "Tony Lukasavage", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2013-08-17T13:20:16.000+0000", "updated": "2013-08-17T13:20:16.000+0000" }, { "id": "267051", "author": { "name": "aladdina", "key": "aladdina", "displayName": "Mason Zhang", "active": true, "timeZone": "Asia/Shanghai" }, "body": "The discussion on forum: http://developer.appcelerator.com/question/156082/alloy-compiler-conditional-code-not-work", "updateAuthor": { "name": "aladdina", "key": "aladdina", "displayName": "Mason Zhang", "active": true, "timeZone": "Asia/Shanghai" }, "created": "2013-08-17T13:21:09.000+0000", "updated": "2013-08-17T13:21:09.000+0000" }, { "id": "267064", "author": { "name": "aladdina", "key": "aladdina", "displayName": "Mason Zhang", "active": true, "timeZone": "Asia/Shanghai" }, "body": "More to add:\r\n\r\nI took some time to change all the function definition to function expression. But got other issues. Here are sample code:\r\n\r\nstill on Mac OS X 10.7, iOS simulator, TI 3.1.2.GA, Alloy 1.2.0\r\n\r\nview.xml\r\n\r\n\r\n