diff --git a/src/backend.memory.js b/src/backend.memory.js index f42aae07..ef17451b 100644 --- a/src/backend.memory.js +++ b/src/backend.memory.js @@ -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(); }; }; diff --git a/test/backend.memory.test.js b/test/backend.memory.test.js index 5743d6d3..822c64f2 100644 --- a/test/backend.memory.test.js +++ b/test/backend.memory.test.js @@ -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); // ======================================