storeToListeners
Returns a set of listeners transformed from fixture store request handlers. Useful for working with REST-ful resources.
storeToListeners( fixtureStore )
Wraps methods of fixture.store to make them socket event listener.
var listeners = storeToListeners( fixtureStore );
server.on({
"news find": listeners.getListData,
"news get": listeners.getData,
})
Parameters
- fixtureStore:
Use
Fixture store provides REST-ful resource storage. Its designed to work with XHR requests thus its methods expect two arguments request
and response
. To work with socket events we need to transform request handlers into socket event listeners.
Here is how we can do this:
var fixture = require("can-fixture");
var canSet = require("can-set");
var io = require("socket.io-client");
var fixtureSocket = require("can-fixture-socket");
// Create fixture store:
var fixtureStore = fixture.store([
{id: 1, title: 'One'},
{id: 2, title: 'Two'},
{id: 3, title: 'Three'}
], new canSet.Algebra({}));
// Instantiate mocked socket server:
var mockedServer = new fixtureSocket.Server(io);
// Now use fixture store to emulate REST-ful service:
var toListener = fixtureStore.requestHandlerToListener;
mockedServer.on({
"books find": toListener( fixtureStore.getListData ),
"books get": toListener( fixtureStore.getData ),
"books create": toListener( fixtureStore.createData ),
"books update": toListener( fixtureStore.updateData ),
"books delete": toListener( fixtureStore.destroyData )
});