RESTful "controllers" in $resource

278 views
Skip to first unread message

Ben Nadel

unread,
Oct 17, 2012, 8:38:49 AM10/17/12
to ang...@googlegroups.com
I was stumped on something in $resource, and finally figured it out and thought I would pass it along.

I was trying to configure a $resource instance that could use RESTful controllers to act upon my resource representations.

In REST, you have:
  • Document
  • Collection
  • Store
  • Controller

The last one, Controller, is an action that you can carry out on a resource that would require logic beyond the simple CRUD-based operations provided by the vanilla HTTP verbs. For example, if I had a "messages" resource, I might have two controllers for:

  • /messages/clear-all
  • /messages/4/archive

The (bolded) controllers act upon their related resource. So anyway, long store short, I was having a lot of trouble figuring out how to define the AngularJS resource to account for this. I didn't see this in the documentation, but it turns out that you can define multiple parameter bindings in a single part of the URL template. As such, I can define, in item two of the URL template, TWO different bindings:

  • /messages/:listController:id/:docController

Notice that the second item is basically saying "listController OR id". Now, as long as I only use one of the "versions" at a time, AngularJS can manage the resource URL properly. 

This had me stumped for a good while. Hope this helps someone else.

For more info, here's my write-up: http://www.bennadel.com/blog/2433-Using-RESTful-Controllers-In-An-AngularJS-Resource.htm

Cheers,
Ben Nadel

Peter Bacon Darwin

unread,
Oct 17, 2012, 11:55:18 AM10/17/12
to ang...@googlegroups.com

Nice!

... sent from my tablet

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Ben Nadel

unread,
Oct 18, 2012, 1:05:51 PM10/18/12
to ang...@googlegroups.com
Thanks!

rul...@gmail.com

unread,
Oct 18, 2012, 6:19:03 PM10/18/12
to ang...@googlegroups.com
Since you're defining actions anyway, it seems there's no need for the /:listController:id/ part, which could just be shortened to /:id/, and then in your definition of the clear action: "clear: { method: "POST", id: "clear-all" }".

You're relying on the caller to respect the convention in any case, so why bother with the extra param var?
Reply all
Reply to author
Forward
0 new messages