Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active November 20, 2020 15:19
Show Gist options
  • Save stevewithington/115b5ac681fe35d42505b1cdedad1593 to your computer and use it in GitHub Desktop.
Save stevewithington/115b5ac681fe35d42505b1cdedad1593 to your computer and use it in GitHub Desktop.
Mura: Example of how to populate a Mura Form Builder option list using `Remote Source`
component {
/**
* MURA FORM: `REMOTE SOURCE` Options Example
*
* Notes:
* + On the form, select `Remote Source` and enter the full URL to where this file resides, along with `?method=getData`
* - For example <http://localhost:8080/sites/{SiteID}/remote/muraFormBuilderRemoteSource.cfc?method=getData>
* + If storing this file under the `/sites` directory, be sure to place it under a directory named `remote` to avoid Access Denied issues
* + If running inside of a Docker container, you MUST use the internal network PORT (e.g., `8080` vs. `80`)
* + How to use a CUSTOM OBJECT for options: <https://gist.github.com/stevewithington/7947902>
* + How to create a Mura form: <https://docs.getmura.com/v7-1/content-managers/advanced-content/#section-how-to-createedit-a-form>
*/
public any function getRS() {
// example recordset : this could come from your own db, of course
var recordsArray = [
{
'datarecordid': '1',
'label': 'California',
'value': 'CA'
},
{
'datarecordid': '2',
'label': 'Ohio',
'value': 'OH'
},
{
'datarecordid': '3',
'label': 'Wisconsin',
'value': 'WI'
},
];
return QueryNew(
'datarecordid,label,value',
'varchar,varchar,varchar',
recordsArray
);
}
/**
* Notes:
* + The method MUST be named `getData`
* + The method MUST be set to `remote` (vs. public or private)
* + The method MUST include returnformat='json' OR returnformat='plain' & return a JSON string such as `return SerializeJSON(someVar)`
* + The returned object MUST be wrapped in a `data` attribute
* + The `data` object MUST contain `datarecordorder` and `datarecords` attributes
* - Optionally, the `data` object may include a `defaultid` attribute to select a specific option by default
*/
remote function getData() returnFormat='json' {
var dataRecordOrder = [];
var dataRecords = {};
var rs = getRS();
for (var row in rs) {
var id = row.datarecordid;
ArrayAppend(dataRecordOrder, id);
dataRecords[id] = {
'datarecordid': id,
'label': row.label,
'value': row.value
};
}
return {
'data': {
'defaultid': dataRecords[1]['datarecordid'],
'datarecordorder': dataRecordOrder,
'datarecords': dataRecords
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment