angularjsrestangular

Restangular POST returns empty parentResource


I don't know if I understood very well, but what I guess is that when you post with a previous Restangular element that came from server, it should result in a restangular element with the first element as parentResource.

Take a look at this code:

// GET: api/vessels
var vessels = Restangular.all('vessels').getList();
this.vessel = vessels[0];

// POST: api/vessels/125/events
this.vessel
  .post('events', event)
  .then(event => { // the server returns the same object but with id filled
    this.vessel.events.push(event);
  }

So event should come with 'this.vessel' as parent but it's coming empty: click here to see the element object on chrome debugger

The problem is that when I try to event.remove() the url of the action is DELETE api/events/2703 and not DELETE api/vessels/125/events/2703 as it should be.

What am I doing wrong?


Solution

  • I found the answer. This is what I should had done:

    // GET: api/vessels
    var vessels = Restangular.all('vessels').getList();
    this.vessel = vessels[0];
    
    // POST: api/vessels/125/events
    this.vessel
      .one('events')
      .post(null, event)
      .then(event => { // the server returns the same object but with id filled
        this.vessel.events.push(event);
      }