1

It is not clear to me the best practice for maintaining $scope of objects when making calls to a RESTful API. The problem I'm having is working with a Schema that has nested sub arrays, see below.

Is it best practice when making RESTful API call to expect a full JSON document in the response to update the $scope, or should I be able to maintain $scope on the client with Angular with nothing more than a http response code (ie 200)? It makes sense when making updates to the user object, but when I need to update profiles or favorites sections of the document, I'm just passing that portion of the document to the API and getting an http reponse code back, and my $scope doesn't reflect with the change, especially when pushing or pulling over PATCH route.

{
  "_id" : ObjectId("558d53eebdd9804820090fa1"),    
  "name" : "Frank",
  "email" : "Frank@FrankTheTank.com",   
  "profiles" : [ 
  {
    "avatar" : "div-male",
    "age" : "35",
    "gender" : "Male",
    "profilename" : "Oly Lifter",
    "_id" : ObjectId("558d5404bdd9804820090fa2"),
    "favorites" : [ 
          {
            "name" : "Power Clean"
          }, 
          {
            "name" : "Hang Clean"
          }, 
          {
            "name" : "Clean and Jerk"
          }
        ],
        "createdAt" : ISODate("2015-06-26T13:30:44.661Z")
    }
    ],
    "createdAt" : ISODate("2015-06-26T13:30:22.884Z"),
    "role" : "user"
}
CampbellGolf
  • 812
  • 12
  • 27
  • Returning a full object is typically fine depending on the object size. I've never run into an issue. Make your your PATCH/Update methods are returning a new Object if you want to reassign/update scope. If the scope on the page isn't updating, you may need to look into reinitializing the $digest cycle. – Christopher Marshall Jun 27 '15 at 00:49
  • 1
    @ChristopherMarshall, I understand now, so getting back a refreshed view of the document is negligible when comparing the amount of data transfer saved by doing API calls from the client. This makes sense, I just implemented it and it works great for my profiles. – CampbellGolf Jun 27 '15 at 01:38

0 Answers0