Collections provides a Javascript API and an in-app code editor.
The Javascript API is limited to Sources, you can use Javascript to search and create documents querying external services (E.g. Google Books). To create a Source, open a collection → tap on button → Edit → Sources.
The API is accessible through the app object.
app.query
This object contains informations about the current query. The query type is determined by the input method (text or barcode).
app.query.isText()bool
Returns true for a text query.
app.query.isBarcode()bool
Returns true for a barcode query.
app.query.valueobject
The query value. For text and barcode queries, the value is a string.
app.params
In a Source, this object will store parameters passed from the search script to the document script.
app.log(object)
Write a message to the console. Equivalent to console.log()
Result
app.result()
This function must be called at the end of the script to return a result. This function has a different declaration depending on the context.
app.result(resultsarray of search results)
In a search script, you should call it by passing an array of search results.
app.result(builderdocument builder)
In a document script, you should call it by passing a document builder.
app.fail()
Call this function to terminate the script execution. You can call it at any point in the code.
Networking
app.request(urlstring)request
Create a new HTTPS request with the specified URL. Note: the app supports only secure HTTPS requests, any http URL is converted to https.
request.methodstring
Set or get the method for the request. "GET" or "POST"
request.bodystring
Set or get the body used by the POST request.
request.addHeaderValue(valuestring, keystring)
Add a header value specifying a value and a key.
request.send()response
Sends the request and returns a response object.
response
The response returned by request.send()
json()JSON object
Parse the response as JSON. It returns undefined if the object can't be parsed.
image()image
Parse the response as image. It returns undefined if the image is not valid.
xml()XML Document
Parse the response as XML. It returns undefined if the response can't be parsed. Please visit XML Parser for the XML parser documentation.
statusCodeinteger
Get the response status code. If the request fails, the status code is -1.
successbool
True if the server sends any response. False otherwise (no connection).
app.image.fromURL(urlstring)image
A shorthand to request an image from web, it returns an image object, or undefined if the request fails.
let request = app.request("https://www.google.com");
let response = request.send();
let data = response.json();
Storage
app.storage
You can use app.storage as a key-value persistence storage. app.storage must be a dictionary and can store any serializable object like string, number, date, array. The dictionary is initialized before evaluating a script.
When you create a document, you usually want to set existing list items or documents to it. To solve this problem, the app uses the concept of suggestion.
A suggestion contains an identifier and all the data needed to create the item. The app will find the item matching the specified identifier, and if that item doesn't exist, it will create a new one.
Create a new suggestion for a list item specifying an alias and a name. By default, the alias is used as the identifier.
suggestion.aliasAsIdentifier()
Use the alias as the identifier.
suggestion.nameAsIdentifier()
Use the name as the identifier.
let listItem = app.listItem.suggest("fantasy", "Fantasy");
document.setListItem(listItem, "genre");
Suggest a document
To suggest a document, create a new document builder and call builder.setIdentifier(fieldAlias) to set a field as the identifier. If you are creating a new document, you don't need to call this method.
let author = app.document.builder();
author.setIdentifier("name");
author.setString("J. K. Rowling", "name");
let book = app.document.builder();
book.setString("Harry Potter", "name");
book.setDocument(author, "author");
The example creates an author with the name field as the identifier. The author is then saved in a book, the app will find an author with "J. K. Rowling" as name or it will create a new one if that doesn't exist.