Last active
November 20, 2020 15:19
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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