Merge pull request #13893 from colemanw/dev/core#821
[civicrm-core.git] / js / model / crm.schema-mapped.js
CommitLineData
6a488035
TO
1/**
2 * Dynamically-generated alternative to civi.core.js
3 */
4b513f23 4(function($, _) {
6a488035
TO
5 if (!CRM.Schema) CRM.Schema = {};
6
7 /**
8 * Data models used by the Civi form designer require more attributes than basic Backbone models:
9 * - sections: array of field-groupings
10 * - schema: array of fields, keyed by field name, per backbone-forms; extra attributes:
11 * + section: string, index to the 'sections' array
12 * + civiFieldType: string
13 *
14 * @see https://github.com/powmedia/backbone-forms
15 */
16
17 CRM.Schema.BaseModel = CRM.Backbone.Model.extend({
18 initialize: function() {
19 }
20 });
21
22 CRM.Schema.loadModels = function(civiSchema) {
23 _.each(civiSchema, function(value, key, list) {
24 CRM.Schema[key] = CRM.Schema.BaseModel.extend(value);
25 });
26 };
27
28 CRM.Schema.reloadModels = function(options) {
29 return $
30 .ajax({
31 url: CRM.url("civicrm/profile-editor/schema"),
32 data: {
33 'entityTypes': _.keys(CRM.civiSchema).join(',')
34 },
35 type: 'POST',
36 dataType: 'json',
37 success: function(data) {
38 if (data) {
39 CRM.civiSchema = data;
40 CRM.Schema.loadModels(CRM.civiSchema);
41 }
42 }
43 });
44 };
45
46 CRM.Schema.loadModels(CRM.civiSchema);
4b513f23 47})(CRM.$, CRM._);