Restangular option to update an object without getting it from server - angularjs

I have started using Restangular and it seems to be a very good library. However I have the following query. In order to update an object I am using the below code
Restangular.one("accounts", $scope.accountEdit.id).get().then(function(account) {
account = Restangular.copy($scope.accountEdit);
account.put();
});
In the above code I have to do a get request to the server and then update it. Is there a better way to just avoid the server call as I would like to update the object from my scope to the server.

you can use customPUT like this,
Restangular.one("accounts").customPUT($scope.accountEdit, $scope.accountEdit.id).then(function(account) {
TO-DO
});
customPUT([elem, path, params, headers]): Does a PUT to the specific
path. Optionally you can set params and headers and elem. Elem is the
element to post. If it's not set, it's assumed that it's the element
itself from which you're calling this function.
besides this you can extend your object with Restangular which gives you same result as your callback function did...
angular.extend($scope.accountEdit, Restangular.one("accounts", $scope.accountEdit.id));
$scope.accountEdit.put();

Related

Issue with performing a $save on a new resource in Angular JS

I am attempting to implement a sample application with Angular that interacts with a backend REST API using $resource objects. However, the backend system does not generate id's for the resources, so these need to be defined on the objects being created on the client. This causes a problem when invoking the $save method on the new'ed resource because it forces the JSON data to be POSTed to the wrong URL, i.e., it POSTs to:
/resources/employees/1234
rather than:
/resources/employees
I would prefer not to have to drop down to using the low level $http service if I can avoid it.
Does anyone know how I can work around this issue?
Thanks.
This is because of the fact that you configured your $resource constructor in this way, for example:
$resource('resources/employees/:employeeId', {
employeeId: #id
});
That means that when you call methods like $save or $delete etc. on the resource objects made by this constructor, the variable :employeeId in the url will be filled with the value id that exist on the object on which you called the method. To avoid this you have to modify the constructor config so that the url variable does not depend on the object id property.

Backbone PUT request explicitly with out using id

I'm trying to PUT the data and my model doesn't have an id.
Is it possible to explicitly tell the Save() method to PUT the data irrespective of ID.
The save method has an options parameter that can override anything on the XHR:
model.save(newVals, { type: 'PUT' })
You can also override the isNew method. PUT vs POST is determined by the result of that method. You'll also want to make sure the URL is being created correctly for new and non-new objects.
Also consider setting the idAttribute correctly so that your model does have an id field that can be used to generate a correct url. Using POST and PUT correctly (POST new items, PUT updates to items) makes your api more intuitive.

Play 2.3/Angular JS $resource/routing issue

So I'm trying to AJAX a single solr doc from my results list to a "doc view" view. I'm trying to use AngularJS to AJAX to my view render method and display the doc that way, but I can't seem to get the angular to work and I'm not sure I'm doing things correctly on the Play side either. Would you at least be willing to tell me if what I'm trying to do will work? The Angular error comes from the docText.text(); call. Here is my code:
Angular controller code:
var docText = $resource("http://localhost:9000/views/full-doc-text.html", {
text: {method: 'PUT'}
});
$scope.handleViewText = function(value) {
docText.text({doc: value});
}
Java code:
public static Result viewText() {
JsonNode json = request().body().asJson();
//do stuff here
return ok(viewtext.render(json));
}
route:
GET /views/full-doc-text.html controllers.Application.viewText()
I see three problems with the code above;
1.The definition of docText resource is not correct. if your read the angularjs manual here you'll see that $resource has 4 parameters. First one is resource url, second is parameter defaults, third one is custom actions and forth one is resource options where last three of them are optional. In your code you pass custom actions as the second parameter, which should be the third. And since you don't have any parameters in your resource url second parameter must be null. So first correction is:
var docText = $resource("http://localhost:9000/views/full-doc-text.html", null, {
text: {method: 'PUT'}
});
2.You define your text action's HTTP method as PUT however in your routes file you are handling GET requests for your desired action. You should change your route definition as:
PUT /views/full-doc-text.html controllers.Application.viewText()
3.PUT method is usually used for update operations when implementing a RESTFULL service. In your case you don't seem to be updating anything. So I suggest to use POST method just for convention.

Restangular put loses part of the url

I want to modify one object via a REST API using Restangular on the client side. I do the following:
Restangular.setBaseUrl('/api/v1');
return Restangular.one('lists', item.listId).one('items',item.order).get().then(function(elem) {
elem.text = item.text;
elem.put();
});
But the put method gets the wrong URL missing the ID for the last member (item.order).
The expected url is /lists/2/items/22 but i get /lists/2/items and the PUT fails.
What could I be doing wrong?
Ok, let me try a theory :)
My comment above was a bit lame because I was focusing on the first part of the code and not the put().
The first part is perfect, you tell Restangular to build the URL with one() methods and the GET request must be very fine.
Then, base on a new Restangular-aware object, you try to persist the change in your API.
However, I believe that Restangular does not actually know what field to use as id in order to build the right query for your elem object.
Did you try to configure, in your Restangular Entity Provider the fields property?
RestangularProvider.setRestangularFields({
id: "orderId",
});
I believe that specifying the right field as id would suffice.
As a bonus, it seems that .save() will use PUT or POST accordingly if it's a new object or not.
Hope this helps

How to refresh local data fetched using $resource service in AngularJS

I have AngularJS application that use $resource service to retrieve data using query() method and create new data using model.$save() method. This works fine exactly as the docs say it should.
My question is how to update my local data fetched using MyService.query() in the first place after I've changed it?
I took the most simple approach for now and I simply call the query() method again. I know this is the worst way efficiency-wise but it's the simplest one.
In my server-side I return the whole state-representation of the new model. How can I add the newly created model to the array of the local data?
UPDATE
I've end up simply pushing the model return from the server but I'll still be happy to know if that's the way to go. From what I can understand from the source code the return array is plan-old-javascript-array that I can manipulate myself.
This is the code I used
$scope.save = function () {
var newComment = new CommentsDataSource();
newComment.Content = $scope.todoText;
newComment.$save({ id: "1" }, function (savedComment) {
$scope.comments.push(savedComment);
});
}
I would simply get the whole list again, to be able to see the modifications brought to the list by other users.
But if the solution you're using suits you, then use it. It's corrrect. Angular uses bare-bones JavaScript objects. Adding a new instance to a list in the scope will refresh the list displayed on the page.

Resources