Search code examples
javascriptangularjsrestrestangular

How to get Restangular to PUT without ID?


I'm working on the front-end for a web application that has a somewhat unorthodox "RESTful" back-end. The CRUD actions look like this:

model
  GET    :id
  POST   {payload}
  PUT    {payload}
  DELETE :id

In other words, the PUT action does not have an ID. Instead, the back-end retrieves the ID from the payload object. This creates a problem when Restangular attempts to generate a URL for a single resource:

Restangular.one('model', 2).get().then ($model) ->
  $model.name = 'newName'
  $model.save()

The request then generated by Restangular is, of course:

PUT /model/2

When I actually just need it to be

PUT /model

Is there any programmatically reliable way to accomplish this? It only applies to updating records. Thanks!


Solution

  • You can do it like this:

    Restangular.one('model').customPUT($model.plain());
    

    With above code and Restangular.service and Restangular.extendModel you can define your own save().