3

Is is possible to create a "ui:button" and set "press" attribute to a function that's defined as a variable in the same scope of $A.createComponent, or inline which is the same, and NOT a function of the controller.

example:

.cmp content:

<aura:component implements="force:lightningQuickAction">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    {!v.body}
</aura:component>

controller.js content:

({

    doInit: function(component, event, helper) {
        console.log('doInit', component.getName(), component, event, helper);

        // already tried these 2, and errors on click
        // var inlineCallback = $A.getCallback(function() {
        //     console.log('pressed', arguments);
        // });
        // var inlineCallback = function() {
        //     console.log('pressed', arguments);
        // };

        var controls = [];
        for (var i = 0; i < 4; i++) {
            var num = i + 1;
            controls.push([
                'ui:button',
                {
                    'aura:id': 'btn' + num,
                    'label': 'Button ' + num,

                    // how to use "inlineCallback"?
                    'press': component.getReference('c.buttonsPressed')
                }
            ]);
        }

        // create all
        $A.createComponents(
            controls,
            function (components, status, errorMessage) {
                console.log('created', arguments);
                component.set('v.body', components);
            }
        );

    },

    // all buttons
    buttonsPressed: function(component, event, helper) {
        var btn = event.getSource();
        var btnId = btn.getLocalId();
        console.log('button id `' + btnId + '` was clicked', btn);
    }

})
  • No that's unusual, whats the problem with defining the method in controer. – Raul Sep 12 '18 at 18:51
  • I'm currently studying components very deeply, and trying to figure out every possible way to create components and configure them... – Mac A. Sep 12 '18 at 19:00
3

When you try to use an inline function, you get an error like this:

The provided key (function() { }) is not a string and cannot be used to look up values for the current component.

This error suggests that you always need to have a reference, because the function itself can't identify the correct component context to execute in. In other words, this is a security restriction to prevent code injection that might otherwise be possible.

  • I'd appreciate it if you could find the docs for this? – Mac A. Sep 12 '18 at 20:08
  • @MacA. Salesforce rarely documents things that you "can't" do. I'd be really surprised if any such documentation exists. – sfdcfox Sep 12 '18 at 20:23
  • Just realized who you are, I greatly appreciate the time and effort you put into helping sfdc developers. – Mac A. Sep 12 '18 at 21:28
  • @MacA. Thanks! I just love helping people, especially on slightly off-the-wall questions like this that challenge me to learn something new or question why things work the way they do. – sfdcfox Sep 12 '18 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.