summaryrefslogtreecommitdiff
path: root/addons/base/static
diff options
context:
space:
mode:
authorXavier Morel <xmo@openerp.com>2011-05-10 10:34:20 +0200
committerXavier Morel <xmo@openerp.com>2011-05-10 10:34:20 +0200
commit0dcd78a54e59f89567cc079bd3399bbf73b5ea16 (patch)
treed244078e3c2b0cdf56bfea0b74b89d497a31c948 /addons/base/static
parent948da1b6c505709cb0f51da40060c770f7f77466 (diff)
[ADD] DataGroup API
bzr revid: xmo@openerp.com-20110510083420-18zr6j8ab5d9c023
Diffstat (limited to 'addons/base/static')
-rw-r--r--addons/base/static/src/js/data.js79
-rw-r--r--addons/base/static/src/js/list.js12
2 files changed, 88 insertions, 3 deletions
diff --git a/addons/base/static/src/js/data.js b/addons/base/static/src/js/data.js
index da0f18001a8..bb190e068c6 100644
--- a/addons/base/static/src/js/data.js
+++ b/addons/base/static/src/js/data.js
@@ -3,15 +3,88 @@ openerp.base.data = function(openerp) {
openerp.base.DataGroup = openerp.base.Controller.extend( /** @lends openerp.base.DataGroup# */{
/**
- * Management interface between views and the collection of selected OpenERP
- * records (represents the view's state?)
+ * Management interface between views and grouped collections of OpenERP
+ * records
*
* @constructs
* @extends openerp.base.Controller
+ *
+ * @param {Array} group_by sequence of fields by which to group
* @param {openerp.base.Session} session Current OpenERP session
+ * @param {openerp.base.DataSet} dataset root dataset, holder of base view state
*/
- init: function(session) {
+ init: function(session, group_by, dataset) {
this._super(session, null);
+ this.model = dataset.model;
+ this.context = dataset.context;
+ this.domain = dataset.domain;
+
+ this.group_by = group_by;
+
+ this.groups = null;
+ },
+ fetch: function () {
+ var d = new $.Deferred();
+ var self = this;
+
+ if (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) {
+ self.groups = response.result;
+ d.resolveWith(self, [response.result]);
+ }, function () {
+ d.rejectWith.apply(d, self, [arguments]);
+ });
+ }
+ return d.promise();
+ },
+ /**
+ * Retrieves the content of the nth-level item in the DataGroup, which
+ * results in either a DataSet or a DataGroup instance.
+ *
+ * Calling :js:func:`~openerp.base.DataGroup.get` without having called
+ * :js:func:`~openerp.base.DataGroup.list` beforehand will likely result
+ * in an error.
+ *
+ * @param {Number} index the index of the group to open in the datagroup's collection
+ * @param {Function} ifDataSet executed if the item results in a DataSet, provided with the dataset as parameter and as context
+ * @param {Function} ifDataGroup executed if the item results in a DataSet, provided with the datagroup as parameter and as context
+ */
+ get: function (index, ifDataSet, ifDataGroup) {
+ var group = this.groups[index];
+ if (!group) {
+ throw new Error("No group at index " + index);
+ }
+
+ var child_context = _.extend({}, this.context, group.__context);
+ if (group.__context.group_by.length) {
+ var datagroup = new openerp.base.DataGroup(
+ this.session, group.__context.group_by, {
+ model: this.model,
+ context: child_context,
+ domain: group.__domain
+ });
+ ifDataGroup.call(datagroup, datagroup);
+ } else {
+ var dataset = new openerp.base.DataSetSearch(this.session, this.model);
+ dataset.domain = group.__domain;
+ dataset.context = child_context;
+ ifDataSet.call(dataset, dataset);
+ }
+ },
+ /**
+ * Provides a list of all top-level items of the data group.
+ *
+ * @returns {$.Deferred}
+ */
+ list: function () {
+ return this.fetch();
}
});
diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js
index 62d29da0cba..f4a2e481418 100644
--- a/addons/base/static/src/js/list.js
+++ b/addons/base/static/src/js/list.js
@@ -290,6 +290,18 @@ openerp.base.ListView = openerp.base.Controller.extend(
// TODO: handle non-empty results.group_by with read_group
self.dataset.context = results.context;
self.dataset.domain = results.domain;
+ if (results.group_by.length) {
+ var group = new openerp.base.DataGroup(
+ self.session, results.group_by, self.dataset);
+ group.list().then(function (lst) {
+ console.log(this, lst);
+ this.get(0, function () {
+ console.log('set', this);
+ }, function () {
+ console.log('group', this);
+ });
+ });
+ }
return self.do_reload();
});
},