Book Image

Knockout.JS Essentials

Book Image

Knockout.JS Essentials

Overview of this book

Table of Contents (16 chapters)
KnockoutJS Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Generating mock data with MockJSON


Once you have mocked the HTTP call, you need to send some data in the response. You have different possibilities:

  • You can hand-write the data in the responseText attribute of the $.mockjax call:

    $.mockjax({
      url: '/products',
      type: 'GET',
      dataType: 'json',
      responseTime: 750,
      responseText: ['Here I can fake the response']
    });
  • You can use a function to generate the mock data:

    $.mockjax({
      url: '/products',
      type: 'GET',
      dataType: 'json',
      responseTime: 750,
      response: function(settings) {
        var fake = 'We fake the url:'+settings.url;
        this.responseText = fake;
      }
    });
  • You can use a library that generates complex and random data in the response.

    This third option can be performed with a library called mockJSON. You can download it from the GitHub repository at https://github.com/mennovanslooten/mockJSON.

    This library allows you to generate data templates to create random data. This helps you to keep your fake data more realistic. You can see on...