Applet Resources
Often there are some JavaScripts and CSS styles necessary to be loaded for your Applet. It can be libraries like jQuery or even your whole application code. For these necessities offline API for saving, storing & loading files are ready.
Methods | Description | Supported since |
---|---|---|
addFile() | Add single file to Applet before starting it. | 1.0.0 |
addFiles() | Add files to Applet before starting it. | 1.0.0 |
addFilesSync() | Add files to Applet before starting it in the set order to prevent race condition. | 1.0.0 |
File object
Every method above, accept single object or array of objects depending by type. Every object must have these keys.
Key | Type | Required | Description |
---|---|---|---|
uri | String | Yes | URL of file |
uid | String | Yes | Unique ID in Applet for future manage |
type | String | Yes | Type of file. Example: sos.offline.types.javascript , sos.offline.types.css , sos.offline.types.video |
^^ | |||
flags | Array | Yes | Special flag for file |
headers | Array | No | Authorization headers |
- The file URL must point to a file. If your URI leads to a 303 redirect (e.g. from http to https), the API will not work.
- There are some limitations for Emulator.
addFile()
Method addFile()
will allow you to load single resource into applet. If you want to load more resource, use addFiles()
.
Javascript Example
const file = { // File that will be loaded into an applet
"uri": "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js",
"uid": "jquery-2.2.4.min.js",
"type": sos.offline.types.javascript,
"flags": [sos.offline.flags.append(document.body)]
}
await sos.offline.addFile(file); // And finally load file
addFiles()
Method addFiles()
will allow you to load array of your resources into Applet. Files are load in random order.
Javascript Example
const files = [ // Array of files
{
"uri": "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js",
"uid": "jquery-2.2.4.min.js",
"type": sos.offline.types.javascript,
"flags": [sos.offline.flags.append(document.body)]
},
{
"uri": "https://signageostest.blob.core.windows.net/test/applet-examples/benchmark-styles.css?v=1.0.0",
"uid": "benchmark-styles.css-v1.0.0",
"type": sos.offline.types.css,
"headers": { "Authorization": "Basic Zm9vOmJhcg==" },
"flags": [sos.offline.flags.append(document.head)]
}
];
await sos.offline.addFiles(files);
addFilesSync()
Method addFilesSync()
works same as method above, but all files are loaded in correct order. Otherwise, files are loaded in random order by file size.
Javascript example
const files = [
{
"uri": "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js",
"uid": "jquery-2.2.4.min.js",
"type": sos.offline.types.javascript,
"flags": [sos.offline.flags.append(document.body)]
},
{
"uri": "https://signageostest.blob.core.windows.net/test/applet-examples/benchmark-styles.css?v=1.0.0",
"uid": "benchmark-styles.css-v1.0.0",
"type": sos.offline.types.css,
"headers": { "Authorization": "Basic Zm9vOmJhcg==" },
"flags": [sos.offline.flags.append(document.head)]
}
];
sos.offline.addFilesSync(files).then(() => {
console.log('Now you can use jQuery and CSS file');
jQuery('#do').something();
});
Usage with Typescript
You can also use all these methods with signageOS TypeScript.
interface ISaveFile {
uid: string;
uri: string;
type: string;
headers?: { [key: string]: string };
flags?: IFlag[];
}
addFile(files: ISaveFile[]): Promise<void>;
addFiles(files: ISaveFile[]): Promise<void>;
addFilesSync(files: ISaveFile[]): Promise<void>;
Flags that can be optionally passed to the file object are available in flags property.
interface IFlag {
type: 'append';
element: IElement;
}
Errors
Although we are doing our best, following errors may occur when working with the Applet Resources.
Code | Type | Message |
---|---|---|
40101 | AppletResourcesError | File needs to be object |
40102 | AppletResourcesError | File UID needs to be defined |
40103 | AppletResourcesError | File flags needs to be Array |
40104 | AppletResourcesError | File flag needs to be object |
40105 | AppletResourcesError | Already loading file: FILE_NAME |
^^ | ^^ | Your code has probably multiple occurrences of addFile/s methods. |
40106 | AppletResourcesError | Already existing file: FILE_NAME |
^^ | ^^ | File is already loaded or exists in Applet |
49901 | FileNotFoundError | File FILE_ID was not found |
^^ | ^^ | Applet can not get file. URL is invalid. |