How to access the event arguments passed to a ColdBox module? - module

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"
}

Related

Kotlin construction: function call with additional body - what such construction means or how it is called (if I want to look it up in the docs)?

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.

Initializing a field member in Kotlin that is a function

New to Kotlin, I have seen this code:
val myModule : Module = module {
viewModel { MyViewModel(get()) }
single { MyRepository() }
}
Looking at the Kotlin docs, it isn't clear to me what the braces mean after "module". Is module a function and the braces are used to initialize the function? If this is true, can you point me to the part in the Kotlin documentation that indicates this? I can't find anything in the docs that shows an example of this. Here is the link:
https://kotlinlang.org/docs/reference/properties.html
Note that your example seems like Koin code.
In a more general sense:
In kotlin when the last parameter of a function is another function ( see Higher order functions) you can put it outside the parenthesis, and if it is the only (non optional) parameter you can omit the parenthesis enterily.
In your example module viewModel and single are functions that take another function as their only parameter, this way you can pass the lambda defining this paramter directly without any parenthesis.
The braces mean that the module function receives a lambda as a parameter. http://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter

Vue 2 arguments in inline (in-template) event handler

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')" >

How do you access the event that is used for dojo.event.connect in the function that is being called?

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));

Calling any dll function based on variable arguments

I have the following items in a structure:
- Dll name (absolute/relative path)
- Function name in the dll
- number of parameters
- Array of parameter types and values
With this information, I need to load the dll and call the function.
To load the dll I would use LoadLibrary.
To get the address of the function I would use GetProcAddress.
Call the function.
To unload the dll, FreeLibrary
Since the number of arguments of the function is dynamic, I am not sure about how the arguments need to be passed to the function.
Can anyone provide some details on how the parameters can be passed to the dll function?
Thanks and Regards,
V Karthick
Is it possible to pass the dictionary object (the one with the parameters) to some wrapper function and have that wrapper function call the actual function? It would probably save you a lot of headache and you could potentially use the preprocessor to generate the wrapper functions for you.