3

In mockjax you can use a function to return a different response according to the request data type, like so:

$.mockjax(function(settings) {
    if ( settings.dataType == 'json' ) {
         return {
             dataType: 'json',
             proxy: 'test.json'
         };
    }
    return false;
});

Is there a way to do the same thing according to the data sent via the request? This is the only way I can find so far:

$.mockjax({
    url: '/',
    data: { variable: 0 },
    proxy: 'test.json'
});

What I want to do is change the proxy to a different file if variable > 0.

So for example is there a way to parse:

data: { variable: <0 }

or similar?

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
sidonaldson
  • 24,431
  • 10
  • 56
  • 61

1 Answers1

0

I know this is old, but you should be able to investigate the settings.data property for that:

$.mockjax(function(settings) {
    var proxyFile = 'test.json'; // a default proxy?

    if ( settings.data.variable > 0 ) {
        proxyFile = 'test-greater-than-zero.json'
    }

    return {
        proxy: proxyFile
    };
});
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • It's not the answer I was looking for but I'll mark yours as the answer so you get some points – sidonaldson Feb 18 '15 at 12:16
  • Did it solve your problem? If not, I'd be happy to try to help... maybe you could submit a Github issue if there is something you think is missing. – Jordan Kasper Feb 18 '15 at 14:54
  • This seems to pick up on all ajax calls after this; is there a way to only pick up on specific URLs? – allicarn Oct 22 '15 at 14:45
  • No, but you can inspect the `settings.url` to match specific URLs. Returning a falsy value (like `undefined`) results in no mock. – Jordan Kasper Oct 22 '15 at 18:35