Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25807] Hyperloop - ES6: Cannot find scoped "this" methods anymore

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2018-03-21T10:00:52.000+0000
Affected Version/sHyperloop 3.1.0
Fix Version/sn/a
ComponentsHyperloop
Labelsn/a
ReporterHans Knöchel
AssigneeUnknown
Created2018-02-24T19:05:01.000+0000
Updated2018-08-06T17:37:03.000+0000

Description

When using ES6 in Hyperloop methods, e.g.
    var ButtonDelegate = Hyperloop.defineClass('ButtonDelegate', 'NSObject');

    ButtonDelegate.addMethod({
        selector: 'buttonPressed:',
        instance: true,
        arguments: ['UIButton'],
        callback: (sender) => { 
            if (this.buttonPressed) {
//              ^-- Here is the issue - it cannot find the declared method
                this.buttonPressed(sender);
            }
        }
    });

    var delegate = new ButtonDelegate();

    // Not found when being used in "Hyperloop.addMethod"
    delegate.buttonPressed = function(sender) {
        alert('Button pressed!');
    };
the this.buttonPressed method cannot be found, although declared. We use this pattern many times, to assign methods to instances afterwards. If there is a better approach, we should replace the usages in the hyperloop-examples sample app. *EDIT*: It only seems to be an issue if the delegate is in the same file as its usage. It works fine for other delegates / sub classes like [here](https://github.com/appcelerator/hyperloop-examples/blob/master/app/lib/ios/subclasses/tableviewdatasourcedelegate.js#L9).

Comments

  1. Hans Knöchel 2018-03-11

    [~cwilliams] Not sure if this is an SDK- or Hyperloop- or code-design-related issue.
  2. Hans Knöchel 2018-03-21

    Resolving as Invalid as ES6 arrow functions have a different scope. To solve this, simply use non-Arrow functions and make use of the global / file-wide this scope.
  3. Eric Merriman 2018-08-06

    Closing as invalid. If incorrect, please reopen.

JSON Source