[#291,bugfix][s]: memory backend transform function now works even when records do not have id - fixes #291.

* Also simplified (and made more efficient) by removing dependency on (not very useful) recline.Data.Transform.mapDocs function
* As documented in #291 issue was that an id was required in the docs
This commit is contained in:
Rufus Pollock 2012-12-22 19:51:03 +00:00
parent 7f35cffe3c
commit 3610d95e74
2 changed files with 33 additions and 5 deletions

View File

@ -227,12 +227,15 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
};
this.transform = function(editFunc) {
var toUpdate = recline.Data.Transform.mapDocs(this.data, editFunc);
// TODO: very inefficient -- could probably just walk the documents and updates in tandem and update
_.each(toUpdate.updates, function(record, idx) {
self.data[idx] = record;
var dfd = $.Deferred();
// TODO: should we clone before mapping? Do not see the point atm.
self.data = _.map(self.data, editFunc);
// now deal with deletes (i.e. nulls)
self.data = _.filter(self.data, function(record) {
return record != null;
});
return this.save(toUpdate);
dfd.resolve();
return dfd.promise();
};
};

View File

@ -187,6 +187,31 @@ test('update and delete', function () {
equal(data.data[0].x, memoryData[1].x);
});
test('transform', function () {
var data = [
{a: 1, b: " bla"},
{a: 2, b: "foo "},
{a: 3, b: "bar"}
];
var store = new recline.Backend.Memory.Store(data);
store.transform(function(d) {
d.a = d.a * 10;
return d;
})
equal(store.data[0].a, 10);
equal(store.data[1].a, 20);
});
test('transform deletes', function () {
var data = [{a: 1, b: " bla"},{a: 2, b: "foo "},{a: 3, b: "bar"}];
var store = new recline.Backend.Memory.Store(data);
store.transform(function(d) {
if (d.a == '1') return null;
else return d;
})
equal(store.data.length, 2);
});
})(this.jQuery);
// ======================================