The core.js library exposes the core functionality of the AppMachine JavaScript SDK.
Including core.js
To include core.js into your JavaScript block you only need to add the following line of code to your HTML page.
<script type="text/javascript" src="/bridge/core.js"></script>
Note: when adding a new JavaScript block to your app we already did this for you.
Doing so will automatically include the core library into your page and will allow you to access the AppMachine SDK using:
<script type="text/javascript">
window.App
</script>
Methods
Now that you have included the AppMachine SDK Core into your block you can start using it. The core library includes the following methods:
-
getCurrentRecord(callback)
-
Will get the current selected record available in the current scope of the application, e.g. when the block is placed underneath an Excel or Web Service block.
Parameters:
Name Type Description callback
function The callback method that should be invoked once the record has been retrieved. - Throws:
-
If the callback has been specified but isn't a function, or when callback is undefined.
-
- Type
- Error
-
getRecords(callback)
-
Will get the collection of records available in the current scope of the application, e.g. when the block is placed underneath an Excel or Web Service block.
Parameters:
Name Type Description callback
function The callback method that should be invoked once the Array of records is available. Throws:
-
If the callback has been specified but isn't a function, or when callback is undefined.
-
- Type
- Error
-
-
getVariable(key, callback)
-
Allows you to retrieve a variable from the application scope.
Parameters:
Name Type Description key
String The variable name. callback
function The callback method that should be invoked once the variable value has been retrieved. Throws:
-
-
If no valid key has been specified.
-
- Type
- Error
-
-
-
If the callback has been specified but isn't a function, or when callback is undefined.
-
- Type
- Error
-
-
-
goBack()
-
Will close the JavaScript block and navigate back to the parent of the JavaScript block. This is usually only useful when your block is running in Full screen mode.
-
navigateToBlock(blockIdentifier, callback)
-
Allows you to navigate to another block inside your app using the native navigation of the app.
Parameters:
Name Type Description blockIdentifier
String The identifier of the building block, this can be the caption, variable name or id. callback
function The callback method that should be invoked once navigation completed. Throws:
-
When no valid block identifier has been specified, or if the block identifier isn't of type String.
-
- Type
- Error
-
-
setVariable(key, value)
-
Allows you to store a variable in the application scope. This variable won't be removed until the application is shut down and allows you to store variables in one JavaScript block and access them in another JavaScript block.
Parameters:
Name Type Description key
String The variable name. value
Object The variable value. Throws:
-
If no valid key has been specified.
-
- Type
- Error
-
Notifications
Notifications are part of the core features and allow you to interact with the user.
<script type="text/javascript">
window.App.Notification
</script>
-
showAlertMessage(title, message, buttons, buttonClickHandler)
-
Will show an alert dialog with the specified title and message. Optionally you can specify buttons that should be sown in the dialog.
Parameters:
Name Type Description title
String The title that should be shown in the dialog. message
String The message that should be shown in the dialog. buttons
Array An Array of strings containing the buttons. buttonClickHandler
function The callback method that should be invoked once the user hits a button. Throws:
-
If both the title and message are undefined.
-
- Type
- Error
Example
window.App.Notification.showAlertMessage('Hello world', 'Do you want to continue?', ['Yes', 'No'], function(args) {
if(args.index === 0){
//User clicked yes
} else {
//User clicked no
}
});Cache
Caching can be an important aspect of an app because it allows the user to use the app even if they are offline. The cache exposes a couple of features that allow you to cache data or images without affecting the local storage.
<script type="text/javascript">
window.App.Cache
</script>-
downloadImage(url, type, onComplted, onFailed)
-
Will download an image using the native image cache and returns the result as a base64 string. Once the image is available you can access it even if the user is offline.
Parameters:
Name Type Description url
String The url of the image that should be downloaded. type
String The image type (png or jpg). onCompleted
Function The callback method that should be invoked once the image is available. onFailed
Function The callback method that should be invoked when downloading the image failed. Throws:
-
-
If no valid image download url has been specified or if the type of the url isn't a String.
-
- Type
- Error
-
-
-
If the image type is unsupported - supported image types are png and jpg.
-
- Type
- Error
-
-
-
If the onCompleted handler is undefined or isn't a valid function.
-
- Type
- Error
-
Example
window.App.Cache.downloadImage('http://481xy61dp22v2uqbx85ez1twoe.wpengine.netdna-cdn.com/wp-content/themes/appmachinenew/2014/images/header/logo-dark.png', 'png', function(base64Image) {
if(base64Image) {
var imageElement = document.getElementById('myImagePlaceholder');
if (imageElement) {
imageElement.src = base64Image;
}
}
},
function(reason) {
alert('Failed to download the image because: ' + reason);
}); -
-