Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restangular - Specify custom ID key

In Restangular if I declare a service, and do a PUT/PATCH/POST operation it uses the id of the item by default as a primary key. But what if we want to use a custom key? Like a slug or a number ?

// GET to /users
Users.getList().then(function(users) {
  var user = users[0]; // user === {id: 1, number: 123456, name: "Tonto"}
  user.name = "Gonto";
  // PUT to /users/1 <-- Here the id is used. But I'd like to use number to post to PUT to /users/123456
  user.put();
})
like image 490
gkpo Avatar asked Nov 09 '22 14:11

gkpo


1 Answers

Ok I found what I was looking for:

https://github.com/mgonto/restangular#setrestangularfields

    RestangularProvider.setRestangularFields({
      id: "number"
    });
like image 188
gkpo Avatar answered Nov 14 '22 23:11

gkpo