requestHandlerToListener
Transforms XHR request handler into socket event listener.
requestHandlerToListener( reqHandler )
Transforms request handler that expects two arguments request
and response
into socket event listener.
server.on("news find", requestHandlerToListener( fixtureStore.getListData ));
Parameters
- reqHandler
{function}
:A request handler, e.g. getListData.
Use
Fixture store methods expect two arguments req
and res
and work like this:
- grab query from
req.data
; - on error call
res( 403, err )
; - on success call
res( data )
.
The format of the returned data is:
- for [can-fixture/StoreType.prototype.getDataList]: {count:
, limit: , offset: , data: [{...},{...}, ...]} - for getData: the item object.
We can use the helper to transform fixture store methods into event listeners:
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({}));
var mockedServer = new fixtureSocket.Server(io);
mockedServer.on("books find", fixtureStore.requestHandlerToListener( fixtureStore.getListData ));