summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/standalone_field_manager_mixin.js
blob: 501ecf7c0124ada0dec2f1dbd1c1e767c3a40e91 (plain)
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
odoo.define('web.StandaloneFieldManagerMixin', function (require) {
"use strict";


var FieldManagerMixin = require('web.FieldManagerMixin');

/**
 * The StandaloneFieldManagerMixin is a mixin, designed to be used by a widget
 * that instanciates its own field widgets.
 *
 * @mixin
 * @name StandaloneFieldManagerMixin
 * @mixes FieldManagerMixin
 * @property {Function} _confirmChange
 * @property {Function} _registerWidget
 */
var StandaloneFieldManagerMixin = _.extend({}, FieldManagerMixin, {

    /**
     * @override
     */
    init: function () {
        FieldManagerMixin.init.apply(this, arguments);

        // registeredWidgets is a dict of all field widgets used by the widget
        this.registeredWidgets = {};
    },

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * This method will be called whenever a field value has changed (and has
     * been confirmed by the model).
     *
     * @private
     * @param {string} id basicModel Id for the changed record
     * @param {string[]} fields the fields (names) that have been changed
     * @param {OdooEvent} event the event that triggered the change
     * @returns {Promise}
     */
    _confirmChange: function (id, fields, event) {
        var result = FieldManagerMixin._confirmChange.apply(this, arguments);
        var record = this.model.get(id);
        _.each(this.registeredWidgets[id], function (widget, fieldName) {
            if (_.contains(fields, fieldName)) {
                widget.reset(record, event);
            }
        });
        return result;
    },

    _registerWidget: function (datapointID, fieldName, widget) {
        if (!this.registeredWidgets[datapointID]) {
            this.registeredWidgets[datapointID] = {};
        }
        this.registeredWidgets[datapointID][fieldName] = widget;
    },
});

return StandaloneFieldManagerMixin;

});