summaryrefslogtreecommitdiff
path: root/addons/base/static/src/js/data.js
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2011-07-14 12:22:43 +0200
committerXavier Morel <xmo@openerp.com>2011-07-14 12:22:43 +0200
commitd4dc9dec0416306f916588c935cdfab1a532f09f (patch)
treefb535b5af82e4635b7f0e242c0f578f93a32e963 /addons/base/static/src/js/data.js
parentb61b9d853de5300582f20006777ba5b7936a823c (diff)
parent9d814c830b5e3146b939c986f70268cbd0ac169c (diff)
[MERGE] bunch of stuff from trunk
bzr revid: xmo@openerp.com-20110714102243-kiyp0o6hjbhmxecd
Diffstat (limited to 'addons/base/static/src/js/data.js')
-rw-r--r--addons/base/static/src/js/data.js94
1 files changed, 55 insertions, 39 deletions
diff --git a/addons/base/static/src/js/data.js b/addons/base/static/src/js/data.js
index 3df9679e1c5..e8a3415fbb3 100644
--- a/addons/base/static/src/js/data.js
+++ b/addons/base/static/src/js/data.js
@@ -1,6 +1,23 @@
openerp.base.data = function(openerp) {
+/**
+ * Serializes the sort criterion array of a dataset into a form which can be
+ * consumed by OpenERP's RPC APIs.
+ *
+ * @param {Array} criterion array of fields, from first to last criteria, prefixed with '-' for reverse sorting
+ * @returns {String} SQL-like sorting string (``ORDER BY``) clause
+ */
+openerp.base.serialize_sort = function (criterion) {
+ return _.map(criterion,
+ function (criteria) {
+ if (criteria[0] === '-') {
+ return criteria.slice(1) + ' DESC';
+ }
+ return criteria + ' ASC';
+ }).join(', ');
+};
+
openerp.base.DataGroup = openerp.base.Controller.extend( /** @lends openerp.base.DataGroup# */{
/**
* Management interface between views and grouped collections of OpenERP
@@ -120,29 +137,26 @@ openerp.base.ContainerDataGroup = openerp.base.DataGroup.extend(
aggregates: aggregates
};
},
- fetch: function () {
+ fetch: function (fields) {
// internal method
var d = new $.Deferred();
var self = this;
- // disable caching for now, not sure what I should do there
- if (false && this.groups) {
- d.resolveWith(this, [this.groups]);
- } else {
- this.rpc('/base/group/read', {
- model: this.model,
- context: this.context,
- domain: this.domain,
- group_by_fields: this.group_by
- }, function () { }).then(function (response) {
- var data_groups = _(response).map(
- _.bind(self.transform_group, self));
- self.groups = data_groups;
- d.resolveWith(self, [data_groups]);
- }, function () {
- d.rejectWith.apply(d, self, [arguments]);
- });
- }
+ this.rpc('/base/group/read', {
+ model: this.model,
+ context: this.context,
+ domain: this.domain,
+ fields: _.uniq(this.group_by.concat(fields)),
+ group_by_fields: this.group_by,
+ sort: openerp.base.serialize_sort(this.sort)
+ }, function () { }).then(function (response) {
+ var data_groups = _(response).map(
+ _.bind(self.transform_group, self));
+ self.groups = data_groups;
+ d.resolveWith(self, [data_groups]);
+ }, function () {
+ d.rejectWith.apply(d, [self, arguments]);
+ });
return d.promise();
},
/**
@@ -163,10 +177,14 @@ openerp.base.ContainerDataGroup = openerp.base.DataGroup.extend(
* records have for the current ``grouped_on`` field name).
* ``aggregates``
* a mapping of other aggregation fields provided by ``read_group``
+ *
+ * @param {Array} fields the list of fields to aggregate in each group, can be empty
+ * @param {Function} ifGroups function executed if any group is found (DataGroup.group_by is non-null and non-empty), called with a (potentially empty) list of groups as parameters.
+ * @param {Function} ifRecords function executed if there is no grouping left to perform, called with a DataSet instance as parameter
*/
- list: function (ifGroups, ifRecords) {
+ list: function (fields, ifGroups, ifRecords) {
var self = this;
- this.fetch().then(function (group_records) {
+ this.fetch(fields).then(function (group_records) {
ifGroups(_(group_records).map(function (group) {
var child_context = _.extend({}, self.context, group.__context);
return _.extend(
@@ -174,7 +192,7 @@ openerp.base.ContainerDataGroup = openerp.base.DataGroup.extend(
self.session, self.model, group.__domain,
child_context, child_context.group_by,
self.level + 1),
- group);
+ group, {sort: self.sort});
}));
});
}
@@ -195,10 +213,10 @@ openerp.base.GrouplessDataGroup = openerp.base.DataGroup.extend(
init: function (session, model, domain, context, level) {
this._super(session, model, domain, context, null, level);
},
- list: function (ifGroups, ifRecords) {
+ list: function (fields, ifGroups, ifRecords) {
ifRecords(_.extend(
- new openerp.base.DataSetSearch(this.session, this.model),
- {domain: this.domain, context: this.context}));
+ new openerp.base.DataSetSearch(this.session, this.model),
+ {domain: this.domain, context: this.context, _sort: this.sort}));
}
});
@@ -215,7 +233,7 @@ openerp.base.StaticDataGroup = openerp.base.GrouplessDataGroup.extend(
init: function (dataset) {
this.dataset = dataset;
},
- list: function (ifGroups, ifRecords) {
+ list: function (fields, ifGroups, ifRecords) {
ifRecords(this.dataset);
}
});
@@ -378,7 +396,7 @@ openerp.base.DataSetStatic = openerp.base.DataSet.extend({
var self = this;
offset = offset || 0;
var end_pos = limit && limit !== -1 ? offset + limit : undefined;
- this.read_ids(this.ids.slice(offset, end_pos), fields, callback);
+ return this.read_ids(this.ids.slice(offset, end_pos), fields, callback);
},
set_ids: function (ids) {
this.ids = ids;
@@ -423,7 +441,7 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
// return read_ids(ids.slice(start,start+limit),fields,callback)
}
}
- this.rpc('/base/dataset/search_read', {
+ return this.rpc('/base/dataset/search_read', {
model: this.model,
fields: fields,
domain: this.domain,
@@ -434,7 +452,9 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
}, function (result) {
self.ids = result.ids;
self.offset = offset;
- callback(result.records);
+ if (callback) {
+ callback(result.records);
+ }
});
},
/**
@@ -452,16 +472,12 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({
*/
sort: function (field, force_reverse) {
if (!field) {
- return _.map(this._sort, function (criteria) {
- if (criteria[0] === '-') {
- return criteria.slice(1) + ' DESC';
- }
- return criteria + ' ASC';
- }).join(', ');
+ return openerp.base.serialize_sort(this._sort);
}
-
var reverse = force_reverse || (this._sort[0] === field);
- this._sort = _.without(this._sort, field, '-' + field);
+ this._sort.splice.apply(
+ this._sort, [0, this._sort.length].concat(
+ _.without(this._sort, field, '-' + field)));
this._sort.unshift((reverse ? '-' : '') + field);
return undefined;
@@ -592,12 +608,12 @@ openerp.base.ReadOnlyDataSetSearch = openerp.base.DataSetSearch.extend({
},
on_create: function(data) {},
write: function (id, data, callback) {
- this.on_write(id);
+ this.on_write(id, data);
var to_return = $.Deferred().then(callback);
setTimeout(function () {to_return.resolve({"result": true});}, 0);
return to_return.promise();
},
- on_write: function(id) {},
+ on_write: function(id, data) {},
unlink: function(ids, callback, error_callback) {
this.on_unlink(ids);
var to_return = $.Deferred().then(callback);