1.0.5 • Published 3 years ago
@skripio/response-emitter v1.0.5
About
A unified way to emit responses for 1C:Enterprise platform.
Allows to generate both sync and async responses.
Installation
npm install @skripio/response-emitter
Usage
- Import
ResponseEmitter
class and instantiate emitter object. Emitter will creatediv
element with id provided and attach it tobody
by default. That can be changed by passingparentElementSelector
to the constructor.
import ResponseEmitter from '@skripio/response-emitter';
const re = new ResponseEmitter({
responseElementId: 'MyComponentName',
responseElementClass: 'response'
});
- Call object methods to emit response message or to do auxiliary stuff.
Response
Skripio ResponseEmitter
prepares and emits response message object to 1C:Enterprise platform.
Response message is guaranteed to have following structure:
{
"callback": "callback identifier",
"code": "response code",
"payload": "message payload"
}
where:
- callback - contains callback identifier that allows to link function calls to responses when executing asynchronous code in
JS
from1C:Enterprise
. - code - Response code.
- payload - Message payload.
Reference
Table of Contents
ResponseEmitter
The response emitter object.
Parameters
args
object Constructor arguments object.args.responseElementId
string Id that will be assigned to the response DOM element.args.responseElementClass
(string | Array[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)) CSS class(es) that will be assigned to the response DOM element.args.parentElementSelector
string Selector of a DOM element that response DOM element will be attached to. (optional, default'body'
)
emitResponse
Generates serialized response message object. Emits click
event if required.
Parameters
callback
any Any attribute to be placed against id key of the message object.code
any Any attribute to be placed against code key of the message object.payload
any Any attribute to be placed against payload key of the message object.click
boolean If truthy then click event with serialized message object will be emitted. No event emitting otherwise. (optional, defaultfalse
)
Returns string Response message object serialized.
codes
ResponseEmitter.codes
constants.
- DONE - 200
- RESULT - 201
- USER_ERROR - 300
- DEV_ERROR - 400
getUserMessage
ResponseEmitter.getUserMessage
static method generates 1C formatted notification message from phrases in different languages.
Parameters
phrases
object Phrases Object that contains phrases in different languages where key must be language code and value contains phrase in that language.
Examples
{
ru: 'Сообщение на Русском языке.',
en: 'Message in English language.'
}
Returns string Formatted text or an empty string if object provided contains no keys with language codes listed in ISO 639-1.