diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/model/model_field_command.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/mail/static/src/model/model_field_command.js')
| -rw-r--r-- | addons/mail/static/src/model/model_field_command.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/addons/mail/static/src/model/model_field_command.js b/addons/mail/static/src/model/model_field_command.js new file mode 100644 index 00000000..f4e59a95 --- /dev/null +++ b/addons/mail/static/src/model/model_field_command.js @@ -0,0 +1,73 @@ +odoo.define('mail/static/src/model/model_field_command.js', function (require) { +'use strict'; + +/** + * Allows field update to detect if the value it received is a command to + * execute (in which was it will be an instance of this class) or an actual + * value to set (in all other cases). + */ +class FieldCommand { + /** + * @constructor + * @param {function} func function to call when executing this command. + * The function should ALWAYS return a boolean value + * to indicate whether the value changed. + */ + constructor(func) { + this.func = func; + } + + /** + * @param {ModelField} field + * @param {mail.model} record + * @param {options} [options] + * @returns {boolean} whether the value changed for the current field + */ + execute(field, record, options) { + return this.func(field, record, options); + } +} + +/** + * Returns a clear command to give to the model manager at create/update. + */ +function clear() { + return new FieldCommand((field, record, options) => + field.clear(record, options) + ); +} + +/** + * Returns a decrement command to give to the model manager at create/update. + * + * @param {number} [amount=1] + */ +function decrement(amount = 1) { + return new FieldCommand((field, record, options) => { + const oldValue = field.get(record); + return field.set(record, oldValue - amount, options); + }); +} + +/** + * Returns a increment command to give to the model manager at create/update. + * + * @param {number} [amount=1] + */ +function increment(amount = 1) { + return new FieldCommand((field, record, options) => { + const oldValue = field.get(record); + return field.set(record, oldValue + amount, options); + }); +} + +return { + // class + FieldCommand, + // shortcuts + clear, + decrement, + increment, +}; + +}); |
