Carson Taylor
Carson Taylor

Reputation: 45

Use Widget Value in Server Script

I may be overthinking this drastically, but what is the easiest way to access a widget's value in a server script?

In my particular case, I am trying to use a dropdown widget's value as the filter for a calculated model query.

function getMonthlyTotalsByResource_() {

  var allRecordsQuery = app.models.Allocations.newQuery();
  allRecordsQuery.filters.Approved._equals = true;
  allRecordsQuery.filters.Resource.Manager.ManagerName._equals = /* How do I make the widget's value available here? */

  var allRecords = allRecordsQuery.run();
...
...

Upvotes: 0

Views: 354

Answers (3)

jc_mc
jc_mc

Reputation: 177

// get widget value
function getWidgetValue() {
    var val = app.PAGES.YOUR_PAGE.descendants.WIDGET_NAME.value;
    return val;
}

// execture query 
function getMonthlyTotalsByResource_(widgetValue) {
    var allRecordsQuery = app.models.Allocations.newQuery();
    allRecordsQuery.filters.Approved._equals = true;
    allRecordsQuery.filters.Resource.Manager.ManagerName._equals = widgetValue;
    var allRecords = allRecordsQuery.run();
}
// run 
getMonthlyTotalsByResource_(getWidgetValue);

Upvotes: 0

Doug Patterson
Doug Patterson

Reputation: 31

The easiest way to do this is to add a parameter to the datasource of your calculated model. Then bind something to it from the client side (e.g. bind to datasource.mycalculatedds.parameters.myparam ).

Then pass the default 'query' object from your calculated datasource. For example in your calculated DS, where you call your function, call getMonthlyTotalsByResource_(query). Then you can set something like

var thismanager = query.parameters.myparam

Upvotes: 0

Markus Malessa
Markus Malessa

Reputation: 1831

In your calculated model datasource in the server script have the following:

return getMonthlyTotalsByResource_(query);

Still in your model datasource add a parameter ('String?') and call it ManagerName.

On your page with the dropdown bind the value of the widget to @datasource.properties.ManagerName

In your server script function change to the following:

function getMonthlyTotalsByResource_(query) {

  var allRecordsQuery = app.models.Allocations.newQuery();
  allRecordsQuery.filters.Approved._equals = true;
  allRecordsQuery.filters.Resource.Manager.ManagerName._equals = 
  query.parameters.ManagerName;

  var allRecords = allRecordsQuery.run();

Upvotes: 1

Related Questions