title: API mocking in Mithril for front-end development, like jquery-mockjax
body: <p>I am using mithril in an IoT framework. I am trying to isolate the front-end development from backend API implementation tasks. In jQuery, I have used jQuery-mockjax and a similar mechanism in Angular. What I understand is that those libraries intercept ajax function call an appropriately respond with data that are setup using  $.mockjax (or similar functions)</p>
  
  <p>The mechanism recommended here <a href="https://groups.google.com/forum/#!topic/mithriljs/FzpCPMfauf0" rel="nofollow">https://groups.google.com/forum/#!topic/mithriljs/FzpCPMfauf0</a>, does not give the flexibility to implement this. There is no easy way to use mock for a selected few APIs.</p>
  
  <p>After looking through the code, I realized is, to implement this, I need to get access to the ‘ajax’ function (or just the XHR processing section), so that it can be overridden using my own implementation that can mock selected APIs. The problem is that ajax is a local function in the library. If it is exposed as m.ajax, my library can override it and get the functionality I need.</p>
  
  <p>My question is, is there a better way to achieve the same? If you have done similar things please share.</p>
  
  <p>My code structure will be something like this:</p>
  
  <pre><code>// in mithril-mockjax.js, implements mockjax function
  
  m.mockjax = function (options) { … }
  
  // in app-api-mocks.js
  
  m.mockjax({
     method: GET,
     url: /sessions/123,
     response: { …}
  });
  
  m.mockjax({
     method: POST,
     url: /sessions,
     data: {},
     response: { … }
  });
  </code></pre>
  
  <p>The above two files are inserted in the page when I am in the development mode. The rest of the code remains the same.</p>