diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 24a4dc3dd2a..c24d37ba0c1 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -448,7 +448,8 @@ instance.web.DataSet = instance.web.Class.extend(instance.web.PropertiesMixin, * Read records. * * @param {Array} ids identifiers of the records to read - * @param {Array} fields fields to read and return, by default all fields are returned + * @param {Array} [fields] fields to read and return, by default all fields are returned + * @param {Object} [options] * @returns {$.Deferred} */ read_ids: function (ids, fields, options) { @@ -456,10 +457,20 @@ instance.web.DataSet = instance.web.Class.extend(instance.web.PropertiesMixin, return $.Deferred().resolve([]); options = options || {}; - // TODO: reorder results to match ids list return this._model.call('read', - [ids, fields || false], - {context: this.get_context(options.context)}); + [ids, fields || false], + {context: this.get_context(options.context)}) + .then(function (records) { + if (records.length <= 1) { return records; } + var indexes = {}; + for (var i = 0; i < ids.length; i++) { + indexes[ids[i]] = i; + } + records.sort(function (a, b) { + return indexes[a.id] - indexes[b.id]; + }); + return records; + }); }, /** * Read a slice of the records represented by this DataSet, based on its diff --git a/addons/web/static/test/data.js b/addons/web/static/test/data.js index d7a957ad920..2228709e8bb 100644 --- a/addons/web/static/test/data.js +++ b/addons/web/static/test/data.js @@ -1,3 +1,32 @@ +openerp.testing.section('data.dataset', { + rpc: 'mock', + dependencies: ['web.data'], +}, function (test) { + test('read_ids', {asserts: 2}, function (instance, _, mock) { + var d = new instance.web.DataSet(null, 'foo'); + mock('foo:read', function (args) { + var ids = args[0]; + deepEqual(ids, [3, 1, 2]); + return [ + {id: 1, a: 'bar'}, + {id: 2, a: 'baz'}, + {id: 3, a: 'foo'} + ]; + }); + + return d.read_ids([3, 1, 2]).then(function (records) { + deepEqual( + records, + [ + {id: 3, a: 'foo'}, + {id: 1, a: 'bar'}, + {id: 2, a: 'baz'} + ] + ) + }); + }) +}); + openerp.testing.section('data.model.group_by', { rpc: 'mock', dependencies: ['web.data'],