MX8 - How to get this in nanoflow javascript actions?

2
Morning I’ve been playing with the new JS actions in Nanoflows, glad to finally have them. Has anyone found a way to get ‘this’, or get the element calling the nanoflow? For example, I want a button to call alert(this.id); as a foundation, but calling this right now just returns an alert with the message “undefined”. Anyone had any luck with this? Cheers  
asked
3 answers
3

I faced exactly the same problem today and came up with this “hack”. It works for button elements and I haven’t tested it with anything else so your mileage may vary. Basically you need to call a javascript action within the nanoflow and execute something like this:

function disable_after_click() {
	// BEGIN USER CODE

	let dijit = window.dijit.registry.toArray().filter(d => d.focused).pop();
	let el = document.getElementById(dijit.id);

	el.disabled = true;
	el.classList.add('disabled');

	return Promise.resolve(true);

	// END USER CODE
}

The idea came to me once I saw the ‘focused’ attribute on the element in the widget registry.

Like I said, it’s probably considered a hack and maybe not ideal for a production environment!

answered
1

What do you mean with “the element calling the nanoflow”? Do you mean the Button that was clicked? Or the input widget that triggered the event?

answered
1

I haven’t played around much with the javascript actions.

But I reckon you can use the .bind(this) ES5 method to your promise function or you can try using the ES6 arrow notation for your resolve and reject functions => which automatically makes this available I believe. Haven’t tested it nor can I tell you what would be the “this”  element in this scenario.

So if you test this I would be curious to your results.

answered