REST with Restangular - Good but not perfect? Are there other alternatives?

659 views
Skip to first unread message

Matt Kruse

unread,
Mar 17, 2014, 1:28:08 PM3/17/14
to ang...@googlegroups.com
I'm familiar with the theory of services, REST, etc, but I've never written a full app to consume them until now. So I don't really know what I don't know.

I quickly moved from $resource to Restangular to get added functionality. It's nice and does what I need, but I kind of expected things to be even simpler than what they are.
I have a few questions for those who have been through this in depth:

1) Is there a tool to abstract the routing a bit? I'd like to generate an Angular service, define some endpoints and their methods, and be done. It looks like all the pieces are there to build it myself, but is there an easier way? I consider this kind of like ORM, and I would expect some of it to be generated. Maybe.

2) Right now my list services are returning an 'href' attribute that points to the individual record url, like this:
url:  /games/1/players
[ {id:1,name:"Test",href:"/players/1"}, ... ]
Is there a better way? Can Restangular "discover" this routing in a better way than an "href" attribute embedded in the object which isn't really a field of the object? What if my object has its own "href" attribute?
Should/can I define this routing in my service, rather than depending on the server to supply it?

3) I use customPOST to call an endpoint like /players/1/publish. Is there a way the server can tell me that the publish endpoint is available, and the url to it, so it's more discoverable? Or do I have to hard-code this coupling on each side and hope they don't get out of sync?

4) What is typically returned by "state-altering" service calls? Should I get the refreshed object back? Or some metadata about what has changed? Or just a success message? My worry is that the server has changed the model in some calculated field, and now it's out of sync in my javascript. Should I manually request the object again after each state change?

5) Is there any better alternative for consuming REST services than Restangular? The API seems good, and right away it "made sense" to me, as a developer. But there is a lot of configuration options and a lot of "custom" methods, which makes me wonder if it's trying to be everything to everyone. Maybe there is an option that is a little more opinionated with fewer options? I kind of like opinionated frameworks and reusable code. As long as they do what I want, I'll follow their rules. :)

Thanks!

Matt Kruse

umi

unread,
Mar 18, 2014, 3:21:32 AM3/18/14
to ang...@googlegroups.com
You make some good points. Maybe look into breezejs if you're looking for a more ORM like service. 

terebentina

unread,
Mar 18, 2014, 6:22:11 AM3/18/14
to ang...@googlegroups.com
Hi,

2. You can alter the restangular models/add new methods to them. You could, for example, add a method to getHref() which would create and return the href based on the object fields. You can also make this a property, instead of a method.

3. This depends on your app a bit but again, you could create a player.publish() method (where player is a restangular model).

4. I do return the refreshed object and just change the model fields in js.

The place to extend the models is in the .config() or .run() blocks of your app. Something like this:

.run(function(Restangular) {
  // this adds a model.save() method to all models that does either create or update, depending on whether the model is new or not.
  Restangular.setOnElemRestangularized(function(elem, isCollection) {
    if (!isCollection) {
      elem.save = function(opts) {
        if (elem._id) {
          return elem.put(opts);
        } else {
          return elem.post(undefined, undefined, opts);
        }
      };
    }
    return elem;
  });

  // this adds player.getTitle() to all 'player' models (any other model will not get it).
  Restangular.extendModel('players', function(player) {
    player.getTitle = function() {
      return player.title + ' ' + player.createdAt;
  });

});


Cheers,
Dan

Felipe Rodrigues

unread,
Mar 19, 2014, 8:01:34 AM3/19/14
to ang...@googlegroups.com
Hi Matt,

About question 2 you can see in Restangular docs: https://github.com/mgonto/restangular#using-self-reference-resources

[]'s
Reply all
Reply to author
Forward
0 new messages