diff options
| author | Aurélien Warnon <awa@odoo.com> | 2018-08-31 15:06:38 +0200 |
|---|---|---|
| committer | Aurélien Warnon <awa@odoo.com> | 2018-09-25 18:39:32 +0200 |
| commit | d3fbd102694bedaedac7016022a7f08d95a10553 (patch) | |
| tree | e228be08de8360779c80745208a0410d65c919ed /addons/web/static/src/js | |
| parent | 57ffbe89ef62760c9a2a4224d4389b320fb7dfab (diff) | |
[IMP] framework js: improve X2Many 'CREATE' operation to handle multiple records
Purpose
=======
The 'CREATE' operation lacked the possibility to handle multiple records.
The 'context' parameter is now an array of dict that contains the context for each new record.
This is usefull when you want to create multiple records into a list in a single call.
Please note that the 'default_get' is still called once for each record in an async way.
Diffstat (limited to 'addons/web/static/src/js')
3 files changed, 64 insertions, 28 deletions
diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 3f64c0249cb..6c6919cce4d 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -1453,6 +1453,15 @@ var FieldOne2Many = FieldX2Many.extend({ * @param {OdooEvent|MouseEvent} ev this event comes either from the 'Add * record' link in the list editable renderer, or from the 'Create' button * in the kanban view + * @param {Array} ev.data.context additional context for the added records, + * if several contexts are provided, multiple records will be added + * (form dialog will only use the context at index 0 if provided) + * @param {boolean} ev.data.forceEditable this is used to bypass the dialog opening + * in case you want to add record(s) to a list + * @param {function} ev.data.onSuccess called when the records are correctly created + * (not supported by form dialog) + * @param {boolean} ev.data.allowWarning defines if the records can be added + * to the list even if warnings are triggered (e.g: stock warning for product availability) */ _onAddRecord: function (ev) { var self = this; @@ -1485,7 +1494,7 @@ var FieldOne2Many = FieldX2Many.extend({ } } else { this._openFormDialog({ - context: data.context, + context: data.context && data.context[0], on_saved: function (record) { self._setValue({ operation: 'ADD', id: record.id }); }, diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 84e900021e8..695e8147c7f 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -1328,17 +1328,17 @@ var BasicModel = AbstractModel.extend({ * @param {Object} [options] * @param {string} [options.position=top] if the new record should be added * on top or on bottom of the list - * @param {Object} [options.context] additional context to be merged before - * calling the default_get (eg. to set default values) - * @returns {Deferred<string>} resolves to the new record id + * @param {Object} [options.[context]] additional context to be merged before + * calling the default_get (eg. to set default values). + * If several contexts are found, multiple records are added + * @param {boolean} [params.allowWarning=false] if true, the default record + * operation can complete, even if a warning is raised + * @returns {Deferred<[string]>} resolves to the new records ids */ _addX2ManyDefaultRecord: function (list, options) { var self = this; var position = options && options.position || 'top'; - var additionalContext = options && options.context; - var context = this._getContext(list, {additionalContext: additionalContext}); var params = { - context: context, fields: list.fields, fieldsInfo: list.fieldsInfo, parentID: list.id, @@ -1346,17 +1346,36 @@ var BasicModel = AbstractModel.extend({ viewType: list.viewType, allowWarning: options && options.allowWarning }; - return this._makeDefaultRecord(list.model, params).then(function (id) { - list._changes.push({operation: 'ADD', id: id, position: position, isNew: true}); - var record = self.localData[id]; - list._cache[record.res_id] = id; - if (list.orderedResIDs) { - var index = list.offset + (position !== 'top' ? list.limit : 0); - list.orderedResIDs.splice(index, 0, record.res_id); - // list could be a copy of the original one - self.localData[list.id].orderedResIDs = list.orderedResIDs; - } - return id; + + var additionalContexts = options && options.context; + var makeDefaultRecords = []; + if(additionalContexts){ + _.each(additionalContexts, function (context) { + params.context = self._getContext(list, {additionalContext: context}); + makeDefaultRecords.push(self._makeDefaultRecord(list.model, params)); + }); + } else { + params.context = self._getContext(list); + makeDefaultRecords.push(self._makeDefaultRecord(list.model, params)); + } + + return $.when.apply($, makeDefaultRecords).then(function(){ + var ids = []; + _.each(arguments, function(id){ + ids.push(id); + + list._changes.push({operation: 'ADD', id: id, position: position, isNew: true}); + var record = self.localData[id]; + list._cache[record.res_id] = id; + if (list.orderedResIDs) { + var index = list.offset + (position !== 'top' ? list.limit : 0); + list.orderedResIDs.splice(index, 0, record.res_id); + // list could be a copy of the original one + self.localData[list.id].orderedResIDs = list.orderedResIDs; + } + }); + + return ids; }); }, /** @@ -1375,6 +1394,9 @@ var BasicModel = AbstractModel.extend({ * it was changed. * @param {string} [options.viewType] current viewType. If not set, we will assume * main viewType from the record + * @param {boolean} [params.allowWarning=false] if true, change + * operation can complete, even if a warning is raised + * (only supported by X2ManyChange) * @returns {Deferred} list of changed fields */ _applyChange: function (recordID, changes, options) { @@ -1723,6 +1745,9 @@ var BasicModel = AbstractModel.extend({ * key. For example, it looks like {operation: ADD, id: 'partner_1'} * @param {string} [viewType] current viewType. If not set, we will assume * main viewType from the record + * @param {boolean} [params.allowWarning=false] if true, change + * operation can complete, even if a warning is raised + * (only supported by the 'CREATE' command.operation) * @returns {Deferred} */ _applyX2ManyChange: function (record, fieldName, command, viewType, allowWarning) { @@ -1822,15 +1847,17 @@ var BasicModel = AbstractModel.extend({ position: command.position, allowWarning: allowWarning }; - def = this._addX2ManyDefaultRecord(list, options).then(function (id) { - if (command.position === 'bottom' && list.orderedResIDs && list.orderedResIDs.length >= list.limit) { - list.tempLimitIncrement = (list.tempLimitIncrement || 0) + 1; - list.limit += 1; - } - // FIXME: hack for lunch widget, which does useless default_get and onchange - if (command.data) { - return self._applyChange(id, command.data); - } + def = this._addX2ManyDefaultRecord(list, options).then(function (ids) { + _.each(ids, function(id){ + if (command.position === 'bottom' && list.orderedResIDs && list.orderedResIDs.length >= list.limit) { + list.tempLimitIncrement = (list.tempLimitIncrement || 0) + 1; + list.limit += 1; + } + // FIXME: hack for lunch widget, which does useless default_get and onchange + if (command.data) { + return self._applyChange(id, command.data); + } + }); }); defs.push(def); break; diff --git a/addons/web/static/src/js/views/list/list_editable_renderer.js b/addons/web/static/src/js/views/list/list_editable_renderer.js index 841a8e7d4d8..ab5e5036dfa 100644 --- a/addons/web/static/src/js/views/list/list_editable_renderer.js +++ b/addons/web/static/src/js/views/list/list_editable_renderer.js @@ -778,7 +778,7 @@ ListRenderer.include({ // but we do want to unselect current row var self = this; this.unselectRow().then(function () { - self.trigger_up('add_record', {context: ev.currentTarget.dataset.context}); // TODO write a test, the deferred was not considered + self.trigger_up('add_record', {context: ev.currentTarget.dataset.context && [ev.currentTarget.dataset.context]}); // TODO write a test, the deferred was not considered }); }, /** |
