diff --git a/Cache.js b/Cache.js index 7bef849..c970df4 100644 --- a/Cache.js +++ b/Cache.js @@ -127,9 +127,11 @@ define([ return when(this.inherited(arguments), function (result) { // now put result in cache (note we don't do add, because add may have // called put() and already added it) - cachingStore.put(object && typeof result === 'object' ? result : object, directives); - // the result from the add should be dictated by the master store and be unaffected by the cachingStore - return result; + var cachedPutResult = + cachingStore.put(object && typeof result === 'object' ? result : object, directives); + // the result from the add should be dictated by the master store and be unaffected by the cachingStore, + // unless the master store doesn't implement add + return result || cachedPutResult; }); }, put: function (object, directives) { @@ -138,9 +140,11 @@ define([ cachingStore.remove((directives && directives.id) || this.getIdentity(object)); return when(this.inherited(arguments), function (result) { // now put result in cache - cachingStore.put(object && typeof result === 'object' ? result : object, directives); - // the result from the put should be dictated by the master store and be unaffected by the cachingStore - return result; + var cachedPutResult = + cachingStore.put(object && typeof result === 'object' ? result : object, directives); + // the result from the put should be dictated by the master store and be unaffected by the cachingStore, + // unless the master store doesn't implement put + return result || cachedPutResult; }); }, remove: function (id, directives) { diff --git a/tests/RequestMemory.js b/tests/RequestMemory.js index 6de1a98..7474ae7 100644 --- a/tests/RequestMemory.js +++ b/tests/RequestMemory.js @@ -2,12 +2,11 @@ define([ 'intern!object', 'intern/chai!assert', 'require', - 'dojo/when', 'dojo/_base/array', 'dojo/_base/declare', '../RequestMemory', '../Trackable' -], function (registerSuite, assert, require, when, arrayUtil, declare, RequestMemory, Trackable) { +], function (registerSuite, assert, require, arrayUtil, declare, RequestMemory, Trackable) { var store; function mapResultIds(results) { @@ -25,7 +24,7 @@ define([ }, '.get': function () { - return when(store.get('node2'), function (item) { + return store.get('node2').then(function (item) { assert.strictEqual( JSON.stringify(item), JSON.stringify({ 'id': 'node2', 'name':'node2', 'someProperty':'somePropertyB' }) @@ -35,11 +34,17 @@ define([ '.put': function () { var updatedItem; - return when(store.get('node5')).then(function (item) { + var updateEventFired; + store.on('update', function () { + updateEventFired = true; + }); + return store.get('node5').then(function (item) { item.changed = true; updatedItem = item; - return store.put(updatedItem); + var putResult = store.put(updatedItem); + assert.isDefined(putResult && putResult.then, 'put should return a promise'); + return putResult; }).then(function () { return store.get('node5'); }).then(function (item) { @@ -49,17 +54,27 @@ define([ '.add': function () { var newItem = { 'id': 'node6', 'name':'node5', 'someProperty':'somePropertyB' }; - return when(store.add(newItem), function () { - return when(store.get('node6'), function (item) { - assert.strictEqual(JSON.stringify(item), JSON.stringify(newItem)); - }); + + var addEventFired; + store.on('add', function () { + addEventFired = true; + }); + var addResult = store.add(newItem); + assert.isDefined(addResult && addResult.then, 'add should return a promise'); + return addResult.then(function () { + return store.get('node6'); + }).then(function (item) { + assert.strictEqual(JSON.stringify(item), JSON.stringify(newItem)); + assert.isTrue(addEventFired); }); }, '.remove': function () { - return when(store.get('node3')).then(function (item) { + return store.get('node3').then(function (item) { assert.ok(item); - return store.remove('node3'); + var removeResult = store.remove('node3'); + assert.isDefined(removeResult && removeResult.then, 'remove should return a promise'); + return removeResult; }).then(function () { return store.get('node3'); }).then(function (item) { @@ -70,7 +85,7 @@ define([ 'filter': function () { var results = store.filter({ someProperty: 'somePropertyB' }).fetch().then(mapResultIds); - return when(results, function (data) { + return results.then(function (data) { assert.deepEqual(data.slice(), [ 'node2', 'node5' ]); }); }, @@ -80,14 +95,14 @@ define([ { property: 'someProperty', descending: true }, { property: 'name', descending: false } ]).fetch().then(mapResultIds); - return when(results, function (data) { + return results.then(function (data) { assert.deepEqual(data.slice(), [ 'node3', 'node2', 'node5', 'node1', 'node4' ]); }); }, '.fetchRange': function () { var results = store.fetchRange({start: 1, end: 4}).then(mapResultIds); - return when(results, function (data) { + return results.then(function (data) { assert.deepEqual(data.slice(), [ 'node2', 'node3', 'node4' ]); }); }, @@ -99,10 +114,9 @@ define([ }) .sort('name', true) .fetchRange({start: 1, end: 3}).then(mapResultIds); - return when(results, function (data) { + return results.then(function (data) { assert.deepEqual(data.slice(), [ 'node3', 'node1' ]); }); } }); }); -