Dynamic List Values from a Known Source
To provide a dynamic list of valid values from mashables, mashups or other information sources, you add the macro metadata element <type> to the corresponding <parameter> and set the datatype to enum. You invoke the mashables, mashup or other source of information to obtain the dynamic list using <uiconfig>:
<macros xmlns="http://www.open-mashup.org/schemas/v1.0/EMML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openmashup.org/schemas/v1.0/EMML/..
/schemas/EMMLPrestoSpec.xsd"
domain="myBlocks">
<macro name="helloWorld"
xmlns:presto="http://www.jackbe.com/v1.0/EMMLPrestoExtensions"
xmlns:macro="http://www.openmashup.org/schemas/v1.0/EMMLMacro">
<presto:macro-meta>
<block usage="Wires">
...
</block>
<parameters>
...
<parameter name="aList">
<label>Choose a country</label>
<type datatype="enum"/>
<uiconfig><[CDATA[ ... ]]></uiconfig>
</parameter>
</parameters>
</presto:macro-meta>
....
</macro>
...
</macros>
The contents of <uiconfig> is a JSON object with configuration for this property. It is a best practice to wrap the <uiconfig> content in a CDATA section, as shown in this example.
To get a dynamic list of values, the configuration object must include a property named acceptedValues. This property must contain an array with the list of values to display.
To obtain this list dynamically, you define a self-invoking, anonymous function as the value of acceptedValues. The function is invoked immediately after it is defined, during rendering of the custom block properties form, and returns the array of values to display.
This example uses MashZone NextGen Connect for JavaScript (PC4JS) to invoke a MashZone NextGen mashable named CountryCodes and build the array of valid values for a block property from the results:
<uiconfig><![CDATA[{acceptedValues: (function(){
var countries = [];
var connection=new Presto.Connection(’/presto’);
connection.request( {
url: "/mashzone/edge/api/rest/CountryCodes/getData?x-presto-resultFormat=json",
type: "get",
contentType: "application/x-www-form-urlencoded",
data: "" },
{ onSuccess: function(response, responseHeaders) {
var result=response;
list = result["e:DataTable"]["e:Entry"];
for (var i=0; i < list.length; i +=1) {
var country = list[i].Name;
countries.push(country);
} },
onFailure: function(e){
console.log("service call failed");
} } );
return countries;
})()
} ]]></uiconfig>
Some points to keep in mind:
You can find the URL and any other technical information needed to invoke a mashable or mashup in Technical Specs on the artifact page for that mashable or mashup. See
Use Mashable/Mashup Technical Specs for more information.
When users add the custom block to a mashup in
Wires, the list of values for the block property can only be successfully populated if that user has permission to run the mashable or mashup that is being invoked. Make sure that the run permissions for this mashable or mashup are set appropriately.
Because the results from the mashable used in this example have a namespace, the code to loop through the results to populate the array uses a
namespace-prefix:node-name syntax. The specific prefix to use for the namespace is visible in the Tree View for results for the mashable or mashup.
You can also use jQuery functions to send AJAX requests to retrieve the dynamic list values.