The Json-Pollock package renders live DOM elements out of JSON according to the Structured Messaging Templates specification
In the dist folder you'll find a list of file that allows you to consume json-pollock in a different ways:
As a Bundle
json-pollock.bundle.min.js - this script bundle both package and styles, once you import it into your code it will inject the needed styles into your page header - no additional actions are needed from your side. it is also supports umd - meaning you can consume it using AMD, CommonJS and as simple script
Separate files for code and style
json-pollock.min.js - use this script if you want to handle the import of the styles by youself, if you use it you should also take care to link json-pollock.min.css to your web page. also supports umd.
No UMD
json-pollock.global.min.js - this script is the same as json-pollock.min.js, however is does not support umd - it only puts JsonPollock on the current this (usually the window object). use this in case you inject the package into sites that are not managed by you and you dont know if it uses AMD or not.
No validation
json-pollock.global.no_validation.min.js - a version of json-pollock.global.min.js which the validation phase is excluded. this bundle is lighter as it does not bundles the validation package (Ajv), however it should be used with more care in regard to the json input.
examples
A script tag:
Following examples are relevant only for json-pollock.bundle.min.js and json-pollock.min.js:
Using RequireJS:
Map the JsonPollock path in the RequireJs config, and then:
Using CommonJS:
You can call the init function if you want to configure JsonPollock - it is not mandatory, if you won't call it JsonPollock will be initialized with defaults.
Supported options:
| name | type | description | default |
|---|---|---|---|
maxAllowedElements | Number | max DOM elements that will be renderes, other elements will be ignored. | 50 |
onAfterElementRendered | Function | A callback function that will be invoked after each render on an element, it allows to customize the result DOM element. see the following for exact signature and usage example of the callback function. |
Example callback function for onAfterElementRendered:
The render function renders json into a DOM element.
The registerAction function allow to register a callback to a certain action type, as defined in the spec.
The unregisterAction function allow to unregister a callback to a certain action type, as defined in the spec.
The unregisterAllActions function allow to unregister all callbacks to all action types.
The validate function allow check if a given JSON is a valid for rendering, if validation fails it will thorw an Error (see Error Handling)
Note that this function is not availble on the No validation build
JsonPollock.render() will throw an Error if it fails from any reason, the error object will have a message property that will give the error description.
Perior to the rendering the JSON object is validated against the JSON schema, if it fails to validate the error object will also include an errors property that will hold the validation errors.