I'm using Alpaca forms and pulling values for a form autocomplete field from a web service. I've worked through how to use this data as values for the autocomplete using jQuery and now need to use this data source within Alpaca forms, which uses typeahead.js and Bloodhound.js. I'm not quite sure how Alpaca interacts with these other JS libs. The code below returns an array of values in the autocomplete field, but of course only the matching value should be displayed and selectable. I'm not sure if I need to create a Bloodhound instance and parse the array matches there or handle this within the "suggestions" template. I've tried both so far without success.
<html>
<head>
<title>Alpaca-Autocomplete Form</title>
<link type="text/css" rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<!-- jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<!-- Bootstrap -->
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<!-- Handlebars -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.js"></script>
<!-- Alpaca -->
<link type="text/css" href="http://code.cloudcms.com/alpaca/1.5.14/bootstrap/alpaca.min.css" rel="stylesheet" />
<script type="text/javascript" src="http://code.cloudcms.com/alpaca/1.5.14/bootstrap/alpaca.min.js"></script>
<!-- jQuery UI Support -->
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css">
<!-- typeahead.js https://github.com/twitter/typeahead.js -->
<script src="bower_components/typeahead.js/dist/bloodhound.min.js" type="text/javascript"></script>
<!-- <script src="bower_components/typeahead.js/dist/typeahead.bundle.min.js" type="text/javascript"></script> -->
<script src="typeahead-0.10.5.bundle.min.js" type="text/javascript"></script>
</head>
<body>
<div id="field7"> </div>
<script>
$("#field7").alpaca({
"schema": {
"type": "string"
},
"options": {
"type": "text",
"label": "Input Parameter DataType",
"helper": "Select the name of the input parameter data type",
"typeahead": {
"config": {
"autoselect": true,
"highlight": true,
"hint": true,
"minLength": 1,
"data": {
"q": "request.term"
}
},
"datasets": {
"type": "remote",
"name": "data",
"displayKey": function(data) {
console.log("** displayKey function called **")
return data.field_values.buckets;
},
"source": "http://smart-api.info/api/suggestion/?field=services.inputParameter.parameterDataType",
"displayKey": function(source) {
var arr = source.buckets;
var results = [];
var dataKeys = source.buckets.map(function (x) {
results.push({"value": x.key}); //expected data format for type: remote for Alpaca
return x.key // key is the name of the key to extract the value for display
});
console.log(dataKeys);
return dataKeys;
},
"templates": function(dataKeys){
var matcher = new RegExp( "\\b" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( dataKeys, function( item ){
return matcher.test( item );
}) );
}
}
}
}
});
</script>
</body>
</html>