0

I'm creating the "Discussions" module of my website with AngularJS.

I've actually two types of resources for client<->server communication :

  1. Discussion $resource (used to retrieve Discussion-related infos, like 'title', 'status', etc. Discussions also contain an array of Message.
  2. Message $resource

The problem : When I load the page, I do a Discussion.query(), for retrieving discussion title, etc. In order to economize client<->server requests, the messages of the discussion are also returned (in addition to Discussion info). Well, that's great, but what if I want to edit or delete messages ? As I got them using "Discussion" model object, messages are not considered as $resource Message and I can't $update them or *$delete them.

Actually, I could just do a "Messages.get()" for getting real Messages $resources, but it would cost a new request to my server (latency, SQL query, ...).

Currently, I found two workarounds for achieving that :

  1. Using Message $resource static methods (Message.delete(...), etc)
  2. "Raw" $http requests

I'm not satisfied by these two solutions, because I want a code as simple as possible.

--

Is it possible to tell to Angular "hey this object is actually a Message $resource !" when I retrieve messages using another resource ? Sorry for strange explanations. I don't think any code would help, but ask me I needed.

Neozaru
  • 1,109
  • 1
  • 10
  • 25
  • it sounds like the discussion service should have a dep on the messages service and it should set the .messages property on messagesService when it loads discussions; not familiar with $resource, so didn't leave a full answer – jcollum Oct 18 '13 at 18:29
  • Yes I already did this. The problem is that resulting messages are not considered as $resource, so I can't edit/delete them – Neozaru Oct 18 '13 at 18:37
  • Post some code ... Is hard to follow what you are saying. – fabrizioM Oct 18 '13 at 18:57

1 Answers1

1

This sounds like a duplicate of this question here: ngResource resolving nested resources

But to summarize: there isn't really a great way to do what you want using $resource. Check out that post and the possible solutions I suggested, but as you can see, we never really came up with a good solution.

You can also check out the Restangular library to see if that will fit your needs.

Community
  • 1
  • 1
tennisgent
  • 14,165
  • 9
  • 50
  • 48