1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
odoo.define('mail/static/src/model/model_core.js', function (require) {
'use strict';
/**
* Module that contains registry for adding new models or patching models.
* Useful for model manager in order to generate model classes.
*
* This code is not in model manager because other JS modules should populate
* a registry, and it's difficult to ensure availability of the model manager
* when these JS modules are deployed.
*/
const registry = {};
//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
/**
* @private
* @param {string} modelName
* @returns {Object}
*/
function _getEntryFromModelName(modelName) {
if (!registry[modelName]) {
registry[modelName] = {
dependencies: [],
factory: undefined,
name: modelName,
patches: [],
};
}
return registry[modelName];
}
/**
* @private
* @param {string} modelName
* @param {string} patchName
* @param {Object} patch
* @param {Object} [param3={}]
* @param {string} [param3.type='instance'] 'instance', 'class' or 'field'
*/
function _registerPatchModel(modelName, patchName, patch, { type = 'instance' } = {}) {
const entry = _getEntryFromModelName(modelName);
Object.assign(entry, {
patches: (entry.patches || []).concat([{
name: patchName,
patch,
type,
}]),
});
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
/**
* Register a patch for static methods in model.
*
* @param {string} modelName
* @param {string} patchName
* @param {Object} patch
*/
function registerClassPatchModel(modelName, patchName, patch) {
_registerPatchModel(modelName, patchName, patch, { type: 'class' });
}
/**
* Register a patch for fields in model.
*
* @param {string} modelName
* @param {string} patchName
* @param {Object} patch
*/
function registerFieldPatchModel(modelName, patchName, patch) {
_registerPatchModel(modelName, patchName, patch, { type: 'field' });
}
/**
* Register a patch for instance methods in model.
*
* @param {string} modelName
* @param {string} patchName
* @param {Object} patch
*/
function registerInstancePatchModel(modelName, patchName, patch) {
_registerPatchModel(modelName, patchName, patch, { type: 'instance' });
}
/**
* @param {string} name
* @param {function} factory
* @param {string[]} [dependencies=[]]
*/
function registerNewModel(name, factory, dependencies = []) {
const entry = _getEntryFromModelName(name);
let entryDependencies = [...dependencies];
if (name !== 'mail.model') {
entryDependencies = [...new Set(entryDependencies.concat(['mail.model']))];
}
if (entry.factory) {
throw new Error(`Model "${name}" has already been registered!`);
}
Object.assign(entry, {
dependencies: entryDependencies,
factory,
name,
});
}
//------------------------------------------------------------------------------
// Export
//------------------------------------------------------------------------------
return {
registerClassPatchModel,
registerFieldPatchModel,
registerInstancePatchModel,
registerNewModel,
registry,
};
});
|