Reference Variables from Client Controller in Server Script (widget)

jakegartenberg
Tera Expert

how do i pass values from my client controller to the server script in a widget? in the following code below i have the values in the client highlighted that i need to use as parameters in my server script.

 

this widget is called by a catalog client script, very bottom image

the values of c.data.store and c.data.probType are being passed there.

 

jakegartenberg_0-1669919798275.png

jakegartenberg_0-1669920389715.png

 

3 REPLIES 3

jakegartenberg
Tera Expert

log statements show these values are undefined with my current reference, im new to building widgets and probably missing something simple

jakegartenberg
Tera Expert

i am calling my widget from a onSubmit catalog client script so im not sure how to reference the values directly from the page, since the widget is not a part of the page. the values im trying to use in the query are passed as part of the onSubmit client script

joshuartx
Tera Contributor

I'm a bit late to this, but to reference data from the client controller in the server script you should first refresh the server object by calling the method below in the client controller:

 

c.server.refresh();

 

 

After the server object is refreshed, we'll ask the server to pull any changes to its data object from the client controller (this is also done in the client controller). The update function is asynchronous, so you should use .then() to chain a callback function to be executed as soon as the server's data object is updated (otherwise it won't pass any changes made in the client controller):

 

// ES5
c.server.update().then( function(response) { 
c.data.exampleVar = "example string"
})

// ES6
c.server.update().then( ()=> { 
c.data.exampleVar = "example string"
})

 

 

From there you can manipulate the data in the server script as desired.