1

I am new to building websites and all I want to do at this stage is to use local JSON file to retrive data instead of mirage provided in ember tutorial. you have mirage/config.js like this:

export default function() {
  this.namespace = '/api';

  let rentals = [{
        //JSON
      }];


  this.get('/rentals', function(db, request) {
    if(request.queryParams.area !== undefined) {
      let filteredRentals = rentals.filter(function(i) {
        return i.attributes.area.toLowerCase().indexOf(request.queryParams.area.toLowerCase()) !== -1;
      });
      return { data: filteredRentals };
    } else {
      return { data: rentals };
    }
  });

  // Find and return the provided rental from our rental list above
  this.get('/rentals/:id', function (db, request) {
    return { data: rentals.find((rental) => request.params.id === rental.id) };
      });
}

This article shows part of the solution but I don't know where it's supposed to be written. Any help would be much appreciated.

Xeno
  • 41
  • 7

1 Answers1

2

There are a few different options for stubbing some data without using mirage. The clearest and easiest is fetch.

Put your json file in the public folder, let's call it something.json. Then, use fetch to get the data (this is the model hook of a route):

model() {
   return fetch('something.json')
    .then(function(res) {
        return res.json()
    })
}

This answer applies from at least 1.13 onward (and possibly earlier). It was written as of 3.1.

handlebears
  • 2,168
  • 8
  • 18