data/callbacks-cache
Implements the data interface callbacks to call the cacheConnection DataInterface. These calls keep the cacheConnection contents up to date.
dataCallbacksCache( baseConnection )
Implements the data interface callbacks so that a corresponding DataInterface method is called on the cacheConnection. This updates the cacheConnection contents whenever data is updated on the primary connection.
Parameters
- baseConnection
{Object}
:can-connect
connection object that is having thedata/callbacks-cache
behavior added on to it.
Returns
{Object}
:
a can-connect
connection containing the method implementations provided by data/callbacks-cache
.
Example
Shows synchronization between primary connection and cacheConnection data when using this behavior:
import dataUrl from "can-connect/data/url/";
import dataCallbacks from "can-connect/data/callbacks/";
import cacheCallbacks from "can-connect/data/callbacks-cache/";
import memoryCache from "can-connect/data/memory-cache/";
var cacheConnection = connect([memoryCache], {});
var todoConnection = connect([dataUrl, dataCallback, cacheCallbacks], {
cacheConnection,
url: "/todo"
});
todoConnection.createData({name:'do the dishes', completed: false}).then(function(data) {
todoConnection.cacheConnection.getData({id: data.id}).then(function(cachedData) {
// data returned from connection and data returned from cache have the same contents
data.id === cachedData.id; // true
data.name === cachedData.name; // true
data.completed === cachedData.completed; // true
data === cachedData; // false, since callbacks-cache creates a copy of the data when adding it to the cache
})
});