backbone-faux-server icon indicating copy to clipboard operation
backbone-faux-server copied to clipboard

Enable remote loading of json data using addRoutes option

Open narayanpai opened this issue 10 years ago • 3 comments
trafficstars

Couldn't find much documenation for handler method. Any help would be appreciated

narayanpai avatar May 12 '15 14:05 narayanpai

The handler's signature and semantics are documented as part of addRoute.

Let me know if you need help with something more specific.

biril avatar May 12 '15 15:05 biril

I'm trying to do following, since its an async call, the following code doesn't work. Is there a better way to do the following

  fauxServer.addRoutes({
    listContacts: {
      urlExp: 'api/contacts',
      httpMethod: 'GET',
      handler: function(context) {
        $.getJSON('mock\test-data.json', function(data) {
          context.data = data;
        });
      }
    }
  });

narayanpai avatar May 12 '15 16:05 narayanpai

Unfortunately, async handlers are not supported.

If you can't load the test data up front (so that you synchronously return them when the handler is invoked) I would suggest that you don't use BFS for this but rather override the relevant model's sync method to do the job. Something along the lines of

myModel.sync = function (method, model, options) {
  if (method !== 'read') {
    throw 'this will only work for GET';
  }
  return $.getJSON('mock\test-data.json', function (data) {
    options.success(data);
  });
};

biril avatar May 12 '15 17:05 biril