diff options
| author | Xavier Morel <xmo@openerp.com> | 2012-03-01 11:21:17 +0100 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2012-03-01 11:21:17 +0100 |
| commit | 9022aae8cb1122df0c9a108b2905e37949bd73b1 (patch) | |
| tree | 42226990bd1fa2508f9ca14ca1911399f326b9ec /addons/web/static/src/js/data.js | |
| parent | 923e947917c7082b9e583633a45e179af84cb1d2 (diff) | |
[ADD] core Traverser API
There are still questions over how it'll work, and if it can work at all
bzr revid: xmo@openerp.com-20120301102117-zxd89ffcvo2n32nw
Diffstat (limited to 'addons/web/static/src/js/data.js')
| -rw-r--r-- | addons/web/static/src/js/data.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 969f3542908..27d4562aafb 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -261,6 +261,85 @@ openerp.web.Model = openerp.web.Class.extend(/** @lends openerp.web.Model# */{ }, }); +openerp.web.Traverser = openerp.web.Class.extend(/** @lends openerp.web.Traverser# */{ + /** + * @constructs openerp.web.Traverser + * @extends openerp.web.Class + * + * @param {openerp.web.Model} model instance this traverser is bound to + */ + init: function (model) { + this._model = model; + this._index = 0; + }, + + /** + * Gets and sets the current index + * + * @param {Number} [idx] + * @returns {Number} current index + */ + index: function (idx) { + if (idx) { this._index = idx; } + return this._index; + }, + /** + * Returns the model this traverser is currently bound to + * + * @returns {openerp.web.Model} + */ + model: function () { + return this._model; + }, + /** + * Fetches the size of the backing model's match + * + * @returns {Deferred<Number>} deferred count + */ + size: function () { + return this._model.query().count(); + }, + + /** + * Record at the current index for the collection, fails if there is no + * record at the current index. + * + * @returns {Deferred<>} + */ + current: function (fields) { + return this._model.query(fields).first().pipe(function (record) { + if (record == null) { + return $.Deferred() + .reject('No record at index' + this._index) + .promise(); + } + return record; + }); + }, + next: function (fields) { + var self = this; + this._index++; + return this.size().pipe(function (s) { + if (self._index >= s) { + self._index = 0; + } + return self.current(fields); + }); + }, + previous: function (fields) { + var self = this; + this._index--; + if (this._index < 0) { + return this.size().pipe(function (s) { + self._index = s-1; + return self.current(fields); + }); + } + return this.current(fields); + } + +}); + openerp.web.DataGroup = openerp.web.OldWidget.extend( /** @lends openerp.web.DataGroup# */{ /** * Management interface between views and grouped collections of OpenERP |
