8

What is the best practices when dealing with nested resources with restangular? i.e.

Restangular.one("accounts", 1).one("projects", 1).get().then(function (project) {
project.getList("proofs").then(function(proofs){
    project.proofs = proofs;
    _.each(proofs, function(proof){
        proof.comments = proof.getList("comments");
    });
});
$scope.project = project;

});

This would allow me to access each inside the view nicely.

<li ng-repeat="proof in project.proofs">Total: {{proof.comments.length}}</li>

If I then want to make a update to the project, it would then send EVERYTHING to the projects REST endpoint (including all proofs, and all proofs.comments).

project.name = 'New Name!';
project.put();

This makes me think that I must be implementing something wrong, and there muse be a better way of dealing with it?

Short of defining everything straight to individual $scope's with something like this (not tested)? i.e.

Restangular.one("accounts", 1).one("projects", 1).get().then(function (project) {
project.getList("proofs").then(function(proofs){
    $scope.projectProofs = proofs;
    _.each(proofs, function(proof){  
      $scope.proofComments[proof.id].push(proof.getList("comments"));
    });
});
$scope.project = project;

});

What is the suggested best practice for this?

4
  • Two things that might be helpful: 1) put the requests themselves in a service and expose only a method that will return a promise. This way you can do your promise chaining/grouping within a service so you only have to resolve the "final" promise in your controller. 2) use a config service to define constants to be able to easily configure the actually used URLs throughout your project. Commented Nov 18, 2013 at 7:59
  • 1
    3) have you looked at $q.all to resolve everything :)? Commented Nov 18, 2013 at 8:02
  • @mgonto do you know what is going on here (with the POST)? I'm interested in this as well, as I'm currently evaluating using restangular in my app. And I have tons of nested scenarios like this. Commented Mar 8, 2014 at 23:26
  • which version of Restangular are you using? Commented Jul 31, 2014 at 1:10

2 Answers 2

1

There are two options: write a custom PUT method or delete the property from the object

Option #1: write a custom PUT method

The default method for putting an element will submit all the parameters that are set.

You can see this in the Restangular source code where every Resource is initialized with a default set of methods that accept all parameters and do not do any filtering to remove parameters.

You can create a custom method that strips the 'proofs' property from the object or only selects certain fields to PUT in the request.

Option #2: delete the property from the object.

Before you put() your project, you can do something like this:

/* ... code to get the project object and the nested proof objects ... */

project.name = 'New Name!';

delete project.proofs;
project.put();

The delete keyword will remove the property from the object. Then when you call put() the project object will no longer have the the nested resource in it. You can read more about the delete keyword here.

You can print out the object at the console to see exactly what you're putting to the server:

console.log(project);
Sign up to request clarification or add additional context in comments.

Comments

0

If only modify one field, you can use element.patch with a param, like this:

// get project...

update = function(field, value){
  project.patch({ field, value });
}

In addition, most of the time your field's value was changed through <input ng-model="project.FIELD_NAME"/>, so you can reuse it:

In your controller:

// get project...

$scope.update = function(field){
  payload = {};
  payload[field] = $scope.project[field];
  project.patch(payload);
}

In your template

<!-- Use submit button or ng-blur... -->
<input ng-model="project.FIELD_NAME" /> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.