[ADD] basic implementation of each and map on collections
bzr revid: xmo@openerp.com-20110818122106-lt23fl3smbxg4f82
This commit is contained in:
parent
10117faf2c
commit
92e06c997b
|
@ -376,6 +376,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
|
|||
* re-renders the content of the list view
|
||||
*/
|
||||
reload_content: function () {
|
||||
this.records.reset();
|
||||
this.$element.find('.oe-listview-content').append(
|
||||
this.groups.apoptosis().render(
|
||||
$.proxy(this, 'compute_aggregates')));
|
||||
|
@ -537,7 +538,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
|
|||
count += record.count || 1;
|
||||
_(columns).each(function (column) {
|
||||
var field = column.id,
|
||||
value = record.values.get(field);
|
||||
value = record.values[field];
|
||||
switch (column['function']) {
|
||||
case 'sum':
|
||||
sums[field] += value;
|
||||
|
@ -685,7 +686,7 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
|
|||
.closest('tr').each(function () {
|
||||
var record = records.at($(this).data('index'));
|
||||
result.ids.push(record.get('id'));
|
||||
result.records.push(record);
|
||||
result.records.push(record.attributes);
|
||||
});
|
||||
return result;
|
||||
},
|
||||
|
@ -718,14 +719,9 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
|
|||
this.$_element.remove();
|
||||
},
|
||||
get_records: function () {
|
||||
var records = [];
|
||||
for(var i=0, length=this.records.length; i<length; ++i) {
|
||||
records.push({
|
||||
count: 1,
|
||||
values: this.records.at(i)
|
||||
})
|
||||
}
|
||||
return records;
|
||||
return this.records.map(function (record) {
|
||||
return {count: 1, values: record.attributes};
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Reloads the record at index ``row_index`` in the list's rows.
|
||||
|
@ -1340,6 +1336,9 @@ var Collection = openerp.base.Class.extend(/** @lends Collection# */{
|
|||
* @returns this
|
||||
*/
|
||||
reset: function (records) {
|
||||
_(this._proxies).each(function (proxy) {
|
||||
proxy.reset();
|
||||
});
|
||||
this.length = 0;
|
||||
this.records = [];
|
||||
this._byId = {};
|
||||
|
@ -1368,6 +1367,25 @@ var Collection = openerp.base.Class.extend(/** @lends Collection# */{
|
|||
|
||||
_onRecordEvent: function (event, record, options) {
|
||||
this.trigger.apply(this, arguments);
|
||||
},
|
||||
|
||||
// underscore-type methods
|
||||
each: function (callback) {
|
||||
for(var section in this._proxies) {
|
||||
if (this._proxies.hasOwnProperty(section)) {
|
||||
this._proxies[section].each(callback);
|
||||
}
|
||||
}
|
||||
for(var i=0; i<this.length; ++i) {
|
||||
callback(this.records[i]);
|
||||
}
|
||||
},
|
||||
map: function (callback) {
|
||||
var results = [];
|
||||
this.each(function (record) {
|
||||
results.push(callback(record));
|
||||
});
|
||||
return results;
|
||||
}
|
||||
});
|
||||
Collection.include(Events);
|
||||
|
|
|
@ -200,4 +200,76 @@ $(document).ready(function () {
|
|||
c.at(1).set('wealth', 5);
|
||||
strictEqual(total, 47);
|
||||
});
|
||||
|
||||
module('list-hofs', {
|
||||
setup: function () {
|
||||
openerp = window.openerp.init();
|
||||
window.openerp.base.list(openerp);
|
||||
}
|
||||
});
|
||||
test('each, degenerate', function () {
|
||||
var c = new openerp.base.list.Collection([
|
||||
{id: 1, value: 5},
|
||||
{id: 2, value: 10},
|
||||
{id: 3, value: 20}
|
||||
]), ids = [];
|
||||
c.each(function (record) {
|
||||
ids.push(record.get('id'));
|
||||
});
|
||||
deepEqual(
|
||||
ids, [1, 2, 3],
|
||||
'degenerate collections should be iterated in record order');
|
||||
});
|
||||
test('each, deep', function () {
|
||||
var root = new openerp.base.list.Collection(),
|
||||
ids = [];
|
||||
root.proxy('foo').add([
|
||||
{id: 1, value: 5},
|
||||
{id: 2, value: 10},
|
||||
{id: 3, value: 20}]);
|
||||
root.proxy('bar').add([
|
||||
{id: 10, value: 5},
|
||||
{id: 20, value: 10},
|
||||
{id: 30, value: 20}]);
|
||||
root.each(function (record) {
|
||||
ids.push(record.get('id'));
|
||||
});
|
||||
// No contract on sub-collection iteration order (for now anyway)
|
||||
ids.sort(function (a, b) { return a - b; });
|
||||
deepEqual(
|
||||
ids, [1, 2, 3, 10, 20, 30],
|
||||
'tree collections should be deeply iterated');
|
||||
});
|
||||
test('map, degenerate', function () {
|
||||
var c = new openerp.base.list.Collection([
|
||||
{id: 1, value: 5},
|
||||
{id: 2, value: 10},
|
||||
{id: 3, value: 20}
|
||||
]);
|
||||
var ids = c.map(function (record) {
|
||||
return record.get('id');
|
||||
});
|
||||
deepEqual(
|
||||
ids, [1, 2, 3],
|
||||
'degenerate collections should be iterated in record order');
|
||||
});
|
||||
test('map, deep', function () {
|
||||
var root = new openerp.base.list.Collection();
|
||||
root.proxy('foo').add([
|
||||
{id: 1, value: 5},
|
||||
{id: 2, value: 10},
|
||||
{id: 3, value: 20}]);
|
||||
root.proxy('bar').add([
|
||||
{id: 10, value: 5},
|
||||
{id: 20, value: 10},
|
||||
{id: 30, value: 20}]);
|
||||
var ids = root.map(function (record) {
|
||||
return record.get('id');
|
||||
});
|
||||
// No contract on sub-collection iteration order (for now anyway)
|
||||
ids.sort(function (a, b) { return a - b; });
|
||||
deepEqual(
|
||||
ids, [1, 2, 3, 10, 20, 30],
|
||||
'tree collections should be deeply iterated');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue