So I've been playing around with the JS API and trying to utilise the batchRequest operation to use the response from one function as a parameter to another. Basically I'm trying to create a few assets (from a CSV and Asset Listing Page) and apply metadata to each asset, then repeat for the length of the list (or array which is what the Asset Listing prints out).
Here is the code I'm using:
function createResourcesBatch(){ //loop through array to create each asset for (var i=0; i < 2; i++){ js_api.batchRequest({ "functions":{ "0":{ "function":"createAsset", "args":{ "parent_id":409664, "type_code":"link", "asset_name":data_array[i][0], "link_type":1, }, "blocking":1 }, "1":{ "function":"setAttribute", "args":{ "asset_id":"%results_0_1_id%", "attr_name":"link_url", "attr_val":data_array[i][3] } }, "2":{ "function":"setMetadataAllFields", "args":{ "asset_id":%results_0_1_id%, "field_info":{ "409760":data_array[i][1], "409765":data_array[i][2] } } } }, "dataCallback":printObject }); } }
Note: data_array is the array of data printed out by the asset listing and is defined above this function in the code. Function printObject is taken directly from the Squiz Manuals while testing. Probably won't continue to use a for loop in production but this is fine while I am testing around 5-10 assets at a time.
I've got everything else lined up such as the API key, JS API file reference, JS API Details screen, but the issue to me is around using the response from one section and passing it to the other since it uses keywords (not sure if mine are correct) and since these are evaluated on page load, this will not work on a loaded page. Doing a quick test with the sample code provided from the manuals at http://manuals.matrix.squizsuite.net/web-services/chapters/javascript-api#Batching-Requests under the heading "Request (Enhanced)" doesn't work either - yes, I've updated the default asset numbers in the example.
Edit: Matrix v4.10.2