1 (function(angular
, $, _
) {
2 // Example usage: <af-form ctrl="afform">
3 angular
.module('af').component('afForm', {
7 controller: function($scope
, $routeParams
, $timeout
, crmApi4
, crmStatus
) {
12 // This component has no template. It makes its controller available within it by adding it to the parent scope.
13 $scope
.$parent
[this.ctrl
] = this;
15 this.$onInit = function() {
16 $timeout(ctrl
.loadData
);
19 this.registerEntity
= function registerEntity(entity
) {
20 schema
[entity
.modelName
] = entity
;
21 data
[entity
.modelName
] = [];
23 this.getEntity
= function getEntity(name
) {
26 // Returns field values for a given entity
27 this.getData
= function getData(name
) {
30 this.getSchema
= function getSchema(name
) {
33 // Returns the 'meta' record ('name', 'description', etc) of the active form.
34 this.getFormMeta
= function getFormMeta() {
35 return $scope
.$parent
.meta
;
37 this.loadData = function() {
39 _
.each(schema
, function(entity
, entityName
) {
40 if ($routeParams
[entityName
] || entity
.autofill
) {
45 crmApi4('Afform', 'prefill', {name
: ctrl
.getFormMeta().name
, args
: $routeParams
})
46 .then(function(result
) {
47 _
.each(result
, function(item
) {
48 data
[item
.name
] = data
[item
.name
] || {};
49 _
.extend(data
[item
.name
], item
.values
, schema
[item
.name
].data
|| {});
55 this.submit
= function submit() {
56 var submission
= crmApi4('Afform', 'submit', {name
: ctrl
.getFormMeta().name
, args
: $routeParams
, values
: data
});
57 return crmStatus({start
: ts('Saving'), success
: ts('Saved')}, submission
);
61 })(angular
, CRM
.$, CRM
._
);