Reference Variables from Client Controller in Server Script (widget)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 10:38 AM - edited 12-01-2022 10:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 10:38 AM
log statements show these values are undefined with my current reference, im new to building widgets and probably missing something simple
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2022 07:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2023 08:45 PM
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.