panx36
panx36

Reputation: 29

Put data in ember store and mirage

I am pushing my data in a controller to the ember store like this:

    this.get('store').pushPayload({
        "user": [
            {
                "id":2,
                "name":this.get('name'),
                "passwort":this.get('admin')
            }
        ]
      });

This works temporally fine but after a reload on a route which uses findAll to get all users from mirage the new user is deleted out of the store. So is there any possibility to hold the new user into the ember store or to push the new object into the mirage database? I´ve tried to send a post-request on my own like this:

$.ajax({
  cache:false,
  type: 'POST',
  url: "/api/test",
  data: JSON.stringify(myData),
  contentType: "application/json",
  success:  function(data) {            
  //
    }
  });

My config.js part:

this.post('/test', function(schema, request) {
    console.log('foo');
  });

The foo comes up but how can I access my data? I have tried

this.post('/test', function(schema, request) {
    console.log('foo');
    console.log(request.data.user.name);
  });

this does not work because user is undefined.

Upvotes: 0

Views: 536

Answers (1)

Ember Freak
Ember Freak

Reputation: 12872

Add namespace points to /api in app/mirage/config.js

this.namespace = '/api';

this.post('/test', function(schema, request) {
    console.log('You can have look at this request object. this will contains whateever you sent in request ',request);
    var params = JSON.parse(request.requestBody);
    console.log('params :',params);
  });

You can inspect params object to get the required params. for more information refer mirage docs

Upvotes: 1

Related Questions