{
"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 \r\n\r\ncontroller.js\r\n\r\nvar interrupt = function() {\r\n debugger;\r\n}\r\n\r\nThis function will be executed twice when click the button.\r\nbut if I change the code back to this way:\r\n\r\nfunction interrupt() {\r\n debugger;\r\n}\r\n\r\nThis works fine.\r\n\r\nFinally it should be still the conditional code issue:\r\n\r\nmy view.xml\r\n~~~\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n~~~\r\n\r\nthe generated code:\r\n~~~\r\n __defers[\"$.__views.startButton!click!start\"] && $.__views.startButton.addEventListener(\"click\", start);\r\n __defers[\"$.__views.interruptButton!click!interrupt\"] && $.__views.interruptButton.addEventListener(\"click\", interrupt);\r\n __defers[\"$.__views.startButton!click!start\"] && $.__views.startButton.addEventListener(\"click\", start);\r\n __defers[\"$.__views.interruptButton!click!interrupt\"] && $.__views.interruptButton.addEventListener(\"click\", interrupt);\r\n~~~\r\n\r\nThe defers are duplicated. So if I use function definition, then everything works because defer will not be used. But if I use function expression, then there will be trouble.\r\n\r\nI still think this kind of issue is too bad for developers, the code just silently wrong.\r\n",
"updateAuthor": {
"name": "aladdina",
"key": "aladdina",
"displayName": "Mason Zhang",
"active": true,
"timeZone": "Asia/Shanghai"
},
"created": "2013-08-18T05:18:29.000+0000",
"updated": "2013-08-18T05:26:30.000+0000"
},
{
"id": "267065",
"author": {
"name": "aladdina",
"key": "aladdina",
"displayName": "Mason Zhang",
"active": true,
"timeZone": "Asia/Shanghai"
},
"body": "You'd better increase the priority of this defect. The code will be silently wrong. This really hurt!",
"updateAuthor": {
"name": "aladdina",
"key": "aladdina",
"displayName": "Mason Zhang",
"active": true,
"timeZone": "Asia/Shanghai"
},
"created": "2013-08-18T05:49:24.000+0000",
"updated": "2013-08-18T05:49:36.000+0000"
},
{
"id": "267073",
"author": {
"name": "tlukasavage",
"key": "tlukasavage",
"displayName": "Tony Lukasavage",
"active": true,
"timeZone": "America/Los_Angeles"
},
"body": "[~aladdina] please try to use the appopriate code formatting tags. Your responses are very hard to read. I bumped up the priority a bit, but as this issue is pretty easy to workaround just by slight restructuring of your code, it will likely wait til the next minor release of alloy.",
"updateAuthor": {
"name": "tlukasavage",
"key": "tlukasavage",
"displayName": "Tony Lukasavage",
"active": true,
"timeZone": "America/Los_Angeles"
},
"created": "2013-08-18T16:23:10.000+0000",
"updated": "2013-08-18T16:23:10.000+0000"
},
{
"id": "289139",
"author": {
"name": "tlukasavage",
"key": "tlukasavage",
"displayName": "Tony Lukasavage",
"active": true,
"timeZone": "America/Los_Angeles"
},
"body": "The original issue of nesting function declarations inside conditionals in Javascript is not going to be addressed for the reasons already mentioned. Even further, under the condition of \"use strict\" in Javascript, this code is not even valid.\r\n\r\nConcerning the multiple instances of event handlers showing up in code when you refactored to avoid the aforementioned issue, I have addressed it. It was resolved as a result of ALOY-851.",
"updateAuthor": {
"name": "tlukasavage",
"key": "tlukasavage",
"displayName": "Tony Lukasavage",
"active": true,
"timeZone": "America/Los_Angeles"
},
"created": "2014-01-21T22:28:19.000+0000",
"updated": "2014-01-21T22:28:19.000+0000"
},
{
"id": "304775",
"author": {
"name": "dpandey",
"key": "dpandey",
"displayName": "Deepti Pandey",
"active": true,
"timeZone": "Asia/Kolkata"
},
"body": "Verified as FIXED using :\r\nMac :10.9.2\r\nAppcelerator Studio, build: 3.3.0.201405121247\r\nSDK - 3.3.0.v20140513191712\r\nacs-1.0.14\r\nalloy-1.4.0-dev\r\nnpm-1.3.2\r\ntitanium-3.3.0-dev\r\ntitanium-code-processor-1.1.1\r\nXcode :5.1.1\r\n\r\nAlloy conditional code are functional and working as expected.\r\nHence closing as FIXED.",
"updateAuthor": {
"name": "dpandey",
"key": "dpandey",
"displayName": "Deepti Pandey",
"active": true,
"timeZone": "Asia/Kolkata"
},
"created": "2014-05-14T10:30:34.000+0000",
"updated": "2014-05-14T10:30:34.000+0000"
}
],
"maxResults": 7,
"total": 7,
"startAt": 0
}
}
}