Is it possible to access arguments/parameters passed to an emitted event within an inline / in-template handler? Something like:
<component #some-event="someObject.field = $arguments[0]"></component
What I'm trying to do exactly is assign a value to an object in the scope. I know I can create a method to do that and use it as a handler for the event but I was wondering if this could work as an inline statement.
It is not $arguments[0], but just arguments[0] (without the $). I am surprised that it actually works in the inline handler. So the following code is valid and will work:
<component #some-event="someObject.field = arguments[0]"></component>
The docs for Methods in Inline Handlers specifies $event as a special variable that gets the first parameter passed via event. I have always used it till now.
After reading your question, a bit more research led me to this reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
It seems every javascript function has a local variable called arguments, which is used when a function is expected to get variable number of arguments.
Using arguments[] as inline statements is definitely possible, but not documented anywhere in the context of vue.js framework. On the other hand, if you use $event in inline handler for events, it feels safer as it is documented clearly and will not break in a future version of Vue.js
Sample usage for $event:
<component #some-event="someObject.field = $event"></component>
There is something that not many people know.
Inline handler besides the $event has access to arguments.
For example I was using v-dropzone which is passing many arguments (e.g. file, response).
By using
<v-dropzone #some-event="callMethod($event, 'some other arg')" >
will catch only the first argument.
So the solution was:
<v-dropzone #some-event="callMethod(arguments, 'some other arg')" >
Related
I am seeing the Kotlin code:
navController.navigate("sales_order/" + it.toString()) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
which I can describe as "function call" (navController.navigate) "with additional body" ({...}). How such construction is called (if I want to look it up in the docs) and what does it mean?
When I checked the type of navController.navigate(...) args, then there are 2 arguments. The first argument - string - is provided in () and I am trying to guess, that everything inside {...} is the content for the second argument which has type NavOptionsBuilder in this case. So, I can guess that NavOptionsBuilder has 3 arguments: 1 function call popUpTo that returns some object and 2 named arguments (launchSingleTop, restoreState) which are Boolean type.
Am I deciphering this construction right - just another way of passing arguments - or is there something deeper?
Am I deciphering this construction right
Almost. You got the beginning right, but the end is not exactly correct.
Let's start with what you got right, and throw in some vocabulary here for posterity. Indeed, you seem to be using the overload of navigate that takes 2 arguments: a string route and a builder function.
Functions in kotlin can be passed in multiple ways, but the most common (and the one used here) is passing a lambda expression. Because the syntax for lambda expressions is based on braces ({ ... }), it makes it look like blocks of code, so the Kotlin language went one step further and allowed to pass lambda expressions outside of the parentheses of the function call when the lambda is the last argument. The reason for this is exactly to allow this kind of constructions which look like their own configuration language. This is what is usually referred to as DSLs (Domain Specific Languages).
Now about what you got wrong:
So, I can guess that NavOptionsBuilder has 3 arguments
Not really. NavOptionsBuilder is the receiver of the function that is passed as the second argument of navigate. This means that, within the lambda that you pass, a NavOptionsBuilder instance is implicitly available as this.
This, in turn, means that you can access methods and properties of NavOptionsBuilder within that lambda block. This is what popUpTo, launchSingleTop, and restoreState are: methods and properties of NavOptionsBuilder - not "arguments".
You can find more general info about this here.
Example:
data class T(val flag: Boolean) {
constructor(n: Int) : this(run {
// Some computation here...
<Boolean result>
})
}
In this example, the custom constructor needs to run some computation in order to determine which value to pass to the primary constructor, but the compiler does not accept the run, citing Cannot access 'run' before superclass constructor has been called, which, if I understand correctly, means instead of interpreting it as the non-extension run (the variant with no object reference in https://kotlinlang.org/docs/reference/scope-functions.html#function-selection), it construes it as a call to this.run (the variant with an object reference in the above table) - which is invalid as the object has not completely instantiated yet.
What can I do in order to let the compiler know I mean the run function which is not an extension method and doesn't take a scope?
Clarification: I am interested in an answer to the question as asked, not in a workaround.
I can think of several workarounds - ways to rewrite this code in a way that works as intended without calling run: extracting the code to a function; rewriting it as a (possibly highly nested) let expression; removing the run and invoking the lambda (with () after it) instead (funnily enough, IntelliJ IDEA tags that as Redundant lambda creation and suggests to Inline the body, which reinstates the non-compiling run). But the question is not how to rewrite this without using run - it's how to make run work in this context.
A good answer should do one of the following things:
Explain how to instruct the compiler to call a function rather than an extension method when a name is overloaded, in general; or
Explain how to do that specifically for run; or
Explain that (and ideally also why) it is not possible to do (ideally with supporting references); or
Explain what I got wrong, in case I got something wrong and the whole question is irrelevant (e.g. if my analysis is incorrect, and the problem is something other than the compiler construing the call to run as this.run).
If someone has a neat workaround not mentioned above they're welcome to post it in a comment - not as an answer.
In case it matters: I'm using multi-platform Kotlin 1.4.20.
Kotlin favors the receiver overload if it is in scope. The solution is to use the fully qualified name of the non-receiver function:
kotlin.run { //...
The specification is explained here.
Another option when the overloads are not in the same package is to use import renaming, but that won't work in this case since both run functions are in the same package.
Is Inline Data declaration possible in the importing parameter of the function module. Currently it is giving
The inline declaration "DATA(IT_MARA)" is not possible in this position.
No, that is not possible. Typechecking in general is really bad on function modules. Just take this snippet for example:
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lw_string
test = someother
banana_banana = someother
IMPORTING
output = lw_string.
It will trigger a runtime error, but doesn't stop the compilation. Theres only a critical note inside extended program check.
Anyway, inline declarations work on method calls. Most of the old function module code is already translated into OOP.
I have a view in my ColdBox application that is calling a module handler within the view like this:
#runEvent( event="mymodule:home.index" )#
Now I want to pass arguments to the module, so I changed the call to this:
#runEvent( event="mymodule:home.index", eventArguments=moduleArgs )#
Though unfortunately I don't seem to have access to the passed arguments within the module's event handler. I've dumped rc and prc, but they only contain variables I've set in the main event handler and the event argument doesn't seem to provide a method to return the passed arguments. The documentation about module event executions unfortunately doesn't provide any information about this.
Also, I realized calling event.getCurrentModule() within the module returns an empty string. I would have expected the module's name.
So, how can I access the arguments passed to a module? Is runEvent() the right function for this? Did I miss a module config setting?
You can define arguments in your function like this
function index(event, rc, prc, isRender=false) {
writedump(arguments);
abort;
}
See the ColdBox runEvent() documentation.
The ColdBox documentation explains how to pass additional arguments to your function. So e.g. calling
#runEvent( event="mymodule:home.index", eventArguments={foo="bar"} )#
the foo variable can be accessed via the arguments scope:
function index(event, rc, prc) {
writedump(arguments.foo); // Dumps "bar"
}
First off, I am using a version of older version of dojo, so dojo.event.connect is the proper syntax. My question is this: How do I access the event in the function that I call when the event is fired.
Basically, Ii am dynamically creating a button and then connecting an event on "onClick"
var _btn = dojo.widget.createWidget(widget parameters);
dojo.event.connect(_btn,"onClick","myFunction");
In myFunction, I need to be able to access the attributes of _btn. I have tried passing _btn as the context of dojo.event.connect but this doesn't work. It also wont pass _btn as a parameter for myFunction when I try that. Is it possible to either A) somehow pass _btn as a parameter into myFunction or B) Access the event that is fired in myFunction when _btn is clicked.
I don't know if there is a more convenient way to solve your problem, but in the worst case, you could just use a closure as a surefire way to pass the parameter:
change myfunction from
myfunction(arg1, arg2){
into
myfunction(btn, arg1, arg2){
//using btn here
and use dojo.partial (or dojo.hitch) to create a function that always reveives a certain button as a parameter (and then pass it to the connect):
dojo.event.connect(_btn, 'onClick', dojo.partial(myFunction, _btn));