Models
General description
This is an abstract JS-extension, containing only a single class.
This class, paired with Service\Converter is designed to provide "seamless" and transparent data exchange between backend and frontend.
Life cycle looks as follows:
- Data is uploaded from the database.
- Data is converted to json at the backend via Service\Converter.
- Based on the data, descendant
crm.model
is created, providing a comfortable access to data at the frontend. - Data can be modified at the frontend similar to the methods
EntityObject
at the backend. - After the update, data can be saved by calling the method
save
, executing ajax-query to a corresponding to controller. - Controller executes action with an item and returns actual object status again in json format.
Additional option, when frontend has id only and the model can receive actual data from backend via the method load()
.
For all these actions to operate correctly, model must contain the means to get names for four ajax actions:
get
;add
;update
;delete
.
By default, action names have the format 'crm.api.' + this.getModelName() + '.' + action
.
Model can be configured by passing additional get-parameters for each action
For the complete chain to operate correctly as designed, each data type must have the following:
- its implemented converter;
- its implemented model;
- its controller.
For now, all this is available only for SPA settings (crm.type-model).
Methods
Method | Description | Available from version |
---|---|---|
constructor(data: {}, params: {})
|
Constructor. Data argument used as model data. Params argument is used during ajax queries. | |
get actions() |
Returns object with keys get, add, update, delete, indicating names for ajax actions for executing rules for reading, adding, updating, and deleting data. | |
compileActionString(action: string): string |
Collects string with name of executed ajax-actions. | |
getId(): ?number |
Returns an IDs. If not available, data is deemed as new, not yet saved in the database. | |
getEntityTypeId(): ?number |
Returns CRM entity type ID. | |
isSaved(): boolean |
Returns true, when ID > 0. | |
isDeleted(): boolean |
Returns true when object is deleted from database (method delete was successfully called and executed). | |
setData(data) |
Writes model data. | |
getData() |
Returns model data. | |
setGetParameters(action: string, parameters: getParameters) |
Writes additional getParameters for actions code. | |
getGetParameters(action: string): getParameters |
Returns additional get-parameters for actions code. | |
getModelName(): string |
Returns model string representation. Abstract method defined in descendant. | |
setDataFromResponse(response: {data: {}}) |
Writes new model data from the response ajax action. | |
load(): Promise<{data: {}},string[]> |
Executes ajax-query for actions with get code. Returns Promise, permitted after model data is updated. | |
save(): Promise<{data: {}},string[]> |
Executes ajax-query for action with add code (when data wasn't saveв) or update. Returns Promise, permitted after query is executed. | |
delete(): Promise<{data: {}},string[]> |
Executes ajax-query for delete (when data wasn't saved) or update code action. Returns Promise, permitted after query is executed. |
Descendants
TypeModel (crm.type-model)
Class for handling SPA settings.
ajax-actions:
get: crm.type.get
add: crm.type.add
update: crm.type.update
delete: crm.type.delete
Allows for full-scale data handling at the frontend side.
In addition to base class methods, this class has several methods for getting/updating model data.
getTitle / setTitle
getCreatedBy
getIsCategoriesEnabled / setIsCategoriesEnabled
getIsStagesEnabled / setIsStagesEnabled
getIsBeginCloseDatesEnabled / setIsBeginCloseDatesEnabled
getIsClientEnabled / setIsClientEnabled
getIsLinkWithProductsEnabled / setIsLinkWithProductsEnabled
getIsCrmTrackingEnabled / setIsCrmTrackingEnabled
getIsMycompanyEnabled / setIsMycompanyEnabled
getIsDocumentsEnabled / setIsDocumentsEnabled
getIsSourceEnabled / setIsSourceEnabled
getIsUseInUserfieldEnabled / setIsUseInUserfieldEnabled
getIsObserversEnabled / setIsObserversEnabled
getIsRecyclebinEnabled / setIsRecyclebinEnabled
getIsAutomationEnabled / setIsAutomationEnabled
getIsBizProcEnabled / setIsBizProcEnabled
getIsSetOpenPermissions / setIsSetOpenPermissions
getLinkedUserFields / setLinkedUserFields
getCustomSectionId / setCustomSectionId
Extra to these fields, there are methods for managing custom settings data - additional section and binding settings:
getCustomSections / setCustomSections
Extra section settings data have an array format with the following elements:
{ id: number, title: string, isSelected: boolean }
getRelations / setRelations
Binding settings data look as
RelationsMap[]
:declare type RelationsMap = { parent: Relation[], child: Relation[], } declare type Relation = { entityTypeId: number, isChildrenListEnabled?: boolean|null, isPredefined?: boolean, }
StageModel (crm.stage-model)
Stage model data. Doesn't handle ajax-actions and operates only at the frontend.
Has the set of methods for retrieving stage data:
getName / setName
getEntityId
getStatusId
getSort / setSort
getColor / setColor
getSemantics
getCategoryId
isFinal
isSuccess
isFailure
CategoryModel (crm.category-model)
Model data for pipeline of custom entity type. Doesn't operate with ajax-actions, only works at the frontend.
It has a set of methods for getting pipeline data:
getName / setName
getSort / setSort
isDefault / setDefault
Examples
$entityTypeId = 140; $type = \Bitrix\Crm\Model\Dynamic\TypeTable::getByEntityTypeId($entityTypeId)->fetchObject(); \Bitrix\Main\UI\Extension::load(['crm.type-model']);
const typeModel = new BX.Crm.Models.TypeModel(= CUtil::PhpToJSObject($type->jsonSerialize()) ?>); typeModel.setTitle('newTitle); typeModel.save().then(() => { console.log('typeModel updated'); });