Search code examples
angularjsrestangular

restangular nested resources, different url for single resource


I have the following end points:

/quotes (GET/POST/PATCH/PUT)
/quotes/{id}/purchase-orders (GET/POST)
/purchase-orders (GET)
/purchase-orders/{id} (GET/PATCH/PUT/DELETE)

When I create a purchase order via:

Restangular.one('quote', {id}).all('purchase-orders').post({params}).then(function(purchaseOrder){
     console.log(purchaseOrder);
};

The url on the purchaseOrder object is /quotes/{id}/purchase-orders/{id} but I want it to be /purchase-orders/{id}. I am achieving this using the code below:

Restangular.extendModel('purchase-orders', function(purchaseOrder) {
    if (purchaseOrder.parentResource) baseRestangular.restangularizeElement(null, purchaseOrder, 'purchase-orders');
    return angular.extend(purchaseOrder, PurchaseOrder);
});

Is this the best/only way?


Solution

  • There is a configuration option to setup parentless routes.

    Restangular.setParentless(true)