{ "id": "166342", "key": "TIMOB-24435", "fields": { "issuetype": { "id": "4", "description": "An improvement or enhancement to an existing feature or task.", "name": "Improvement", "subtask": false }, "project": { "id": "10153", "key": "TIMOB", "name": "Titanium SDK/CLI", "projectCategory": { "id": "10100", "description": "Titanium and related SDKs used in application development", "name": "Client" } }, "fixVersions": [], "resolution": { "id": "7", "description": "", "name": "Invalid" }, "resolutiondate": "2017-03-22T01:18:11.000+0000", "created": "2017-02-27T22:55:22.000+0000", "priority": { "name": "None", "id": "6" }, "labels": [], "versions": [ { "id": "18154", "name": "Release 6.0.1", "archived": false, "released": true, "releaseDate": "2016-12-21" } ], "issuelinks": [], "assignee": { "name": "kiguchi", "key": "kota", "displayName": "Kota Iguchi", "active": false, "timeZone": "America/Los_Angeles" }, "updated": "2017-03-22T01:18:11.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": "12642", "name": "Windows", "description": "Windows authoring support" } ], "description": "h5. Issue Description\r\n\r\nCustomer need a way to receive a function (callback) from Javascript in the C++ code by looking at the code in TitaniumKit.\r\n\r\nFormerly, in Kroll, this would be done by doing something like:\r\n\r\n(Assuming this is an android module)\r\nHashMap args = args;\r\nKrollFunction someFunction = (KrollFunction) args.get(\"someFunction\");\r\n[...]\r\nsomeFunction.call(KrollObject, KrollDict);\r\n\r\nSo Would it be possible you provide an example on this if is possible? \r\n\r\n", "attachment": [], "flagged": false, "summary": "Windows Module: Receive C++ callback from JS ", "creator": { "name": "rramirez", "key": "rramirez", "displayName": " Ricardo Ramirez", "active": true, "timeZone": "America/Los_Angeles" }, "subtasks": [], "reporter": { "name": "rramirez", "key": "rramirez", "displayName": " Ricardo Ramirez", "active": true, "timeZone": "America/Los_Angeles" }, "environment": "Windows 10 \r\n\r\nNode.js Version = 4.6.0\r\nnpm Version = 2.15.9\r\n\r\nAppcelerator CLI = 6.1.0", "comment": { "comments": [ { "id": "408097", "author": { "name": "kiguchi", "key": "kota", "displayName": "Kota Iguchi", "active": false, "timeZone": "America/Los_Angeles" }, "body": "Yes it's possible, you can find examples in TitaniumKit because callback pattern has been used a lot in Titanium API.\r\n\r\nI think easiest way to fire callback is leveraging {{addEventListener}}. For instance [Ti.Gesture|https://github.com/appcelerator/titanium_mobile_windows/blob/master/Source/Sensors/src/Gesture.cpp#L28].\r\n\r\nSo let say you are listening {{somethingfired}} event. In this case you can add listener in JavaScript\r\n\r\n{code:javascript}\r\nvar YOUR_MODULE = require('YOUR_MODULE'), \r\n your_module = new YOUR_MODULE();\r\nyour_module.addEventListener('somethingfired', function(e) {\r\n Ti.API.info(e.type + ' my_number:' + e.my_number);\r\n});\r\n{code}\r\n\r\nYou can fire this callback from the module by executing {{fireEvent}} like below.\r\n\r\n{code:c++}\r\nTITANIUM_FUNCTION(YourModule, doTheMyNumberEvent)\r\n{\r\n const auto ctx = get_context();\r\n auto e = ctx.CreateObject();\r\n e.SetProperty(\"my_number\", ctx.CreateNumber(12345));\r\n fireEvent(\"somethingfired\", e);\r\n}\r\n{code}\r\n\r\nThere is another way to do the callback, by calling {{JSObject}} as a function directly. This is basic functionality within [HAL|https://github.com/appcelerator/HAL] framework.\r\n\r\n{code:javascript}\r\nvar YOUR_MODULE = require('YOUR_MODULE'), \r\n your_module = new YOUR_MODULE();\r\nyour_module.doTheCallbackImmediately(function(str) {\r\n Ti.API.info(str);\r\n});\r\n{code}\r\n\r\n{code}\r\nTITANIUM_FUNCTION(YourModule, doTheCallbackImmediately)\r\n{\r\n\tENSURE_OBJECT_AT_INDEX(my_callback, 0);\r\n\r\n const std::vector args { get_context().CreateString(\"TEST\") };\r\n\tmy_callback(args, get_object());\r\n\treturn get_context().CreateUndefined();\r\n}\r\n{code}\r\n\r\nExample: https://github.com/appcelerator/HAL/blob/master/examples/Widget.cpp#L277", "updateAuthor": { "name": "kiguchi", "key": "kota", "displayName": "Kota Iguchi", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2017-02-28T04:17:12.000+0000", "updated": "2017-02-28T04:17:12.000+0000" }, { "id": "408160", "author": { "name": "rramirez", "key": "rramirez", "displayName": " Ricardo Ramirez", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Thank you Kota ! ", "updateAuthor": { "name": "rramirez", "key": "rramirez", "displayName": " Ricardo Ramirez", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2017-02-28T18:58:36.000+0000", "updated": "2017-02-28T18:58:36.000+0000" }, { "id": "414709", "author": { "name": "kiguchi", "key": "kota", "displayName": "Kota Iguchi", "active": false, "timeZone": "America/Los_Angeles" }, "body": "Closing this for now, this is implemented by HAL framework.", "updateAuthor": { "name": "kiguchi", "key": "kota", "displayName": "Kota Iguchi", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2017-03-22T01:18:11.000+0000", "updated": "2017-03-22T01:18:11.000+0000" } ], "maxResults": 3, "total": 3, "startAt": 0 } } }