How to return/edit REST resource with AngularJS & Restangular
Asked Answered
T

1

3

Using Restangular for AngularJS, keep getting an object object from Mongolab.

I'm sure it has to do with Promise but not sure how to use/implement this coming from old Java OO experience.

(Side note, would something like Eloquent Javascript, some book or resource help me understand the 'new' Javascript style?)

The small web app is for Disabled Students and is to input/edit the students, update the time they spend after school and then output reports for their parents/caregivers every week.

Here's the code that returns undefined when popping up a new form (AngularJS Boostrap UI modal)

I personally think Restangular & the documentation is a great addition so hope it doesn't dissuade others - this is just me not knowing enough.

Thanks in advance

app.js ...

$scope.editStudent = function(id) {
    $scope.myStudent = Restangular.one("students", id);
    console.log($scope.myStudent);
}
Tetartohedral answered 22/6, 2013 at 18:44 Comment(5)
I'd probably use ngResource over restangular. Also, yes - there are some good JS books, something like Eloquent JavaScript would help you get started. There are also good books on AngularJS. Good luck!Dermatosis
Is the request being build up correctly, ie, hitting the right url? Try using firebug or another javascript console in your browser and check that traffic. As for ngResource versus Restangular, Restangular does have it's merrits: github.com/mgonto/restangular/blob/master/… (the promises part is not true anymore)Chung
Thanks for the input thus far Benjamin & Maarten. Maarten: Yes, it is hitting the URL, well at least for my getList to get the list of disabled students. It's a great library and fantastic documentation, its just I'm too dense lol.Tetartohedral
Hey @Chung it's true that the promise part is "kind of true now". The $promise implementation on $resource still sucks in my opinion. It still does this "magic" filling of your objects and you must get the $promise as a property of the real object or array that was the response. I like it more to return the promise by itself.Embowel
@Embowel have to trust you on that. We are using restangular fwiwChung
E
15

I'm the creator of Restangular :). Maybe I can help you a bit with this.

So, first thing you need to do is to configure the baseUrl for Restangular. For MongoLab you usually do have a base url that's similar to all of them.

Once you got that working, you need to check the format of the response:

If your response is wrapped in another object or envelope, you need to "unwrap" it in your responseExtractor. For that, check out https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case

Once you got that OK, you can start doing requests.

All Restangular requests return a Promise. Angular's templates are able to handle Promises and they're able to show the promise result in the HTML. So, if the promise isn't yet solved, it shows nothing and once you get the data from the server, it's shown in the template.

If what you want to do is to edit the object you get and then do a put, in that case, you cannot work with the promise, as you need to change values.

If that's the case, you need to assign the result of the promise to a $scope variable.

For that, you can do:

Restangular.one("students", id).get().then(function(serverStudent) {
    $scope.myStudent = serverStudent;
});

This way, once the server returns the student, you'll assign this to the scope variable.

Hope this helps! Otherwise comment me here!

Also check out this example with MongoLab maybe it'll help you :)

http://plnkr.co/edit/d6yDka?p=preview

Embowel answered 24/6, 2013 at 22:58 Comment(1)
hey the creater of restangular! :) can you look at my very related question about the put-issue?Berkman

© 2022 - 2024 — McMap. All rights reserved.