Save in save order ()

I am playing with Restangular and I want to make a .save () call to update or create an object. From Restangular github page I see that it is possible to update or create a new account. But if there are no accounts on the server, the get method does not exist. (firstAccount undefined)

Restangular.all('accounts').getList().then(function(accounts) {
  var firstAccount = accounts[0];
  firstAccount.title = "New title"
  // PUT /accounts/123. Save will do POST or PUT accordingly
  firstAccount.save();
});

      

My question is, how do I make firstAccount a referencing object that will navigate to the correct URL (POST / accounts) when I call firstAccount.save () if there are no accounts in the response?

+3


source to share


1 answer


If you are trying to create a new element, you can restangularize it:

var account = {
    title: 'New Title'
};
var restangularAccount = Restangular.restangularizeElement(null, account, 'accounts');
restangularAccount.save();

      



This will make an HTTP POST to / accounts

+2


source







All Articles