Can you add your own methods to them? For example, in my app I need a
method that makes a call to the server, but then refreshes part of the
$resource object and possibly some associated objects. Let's say the
object is called Profile and call the method setFlag(). I could create
a service to do this like this:
$myservice.setFlag(profile)
or I could add a setFlag method to the profile and do this:
profile.setFlag()
There are pros and cons to each approach and it seems that Angular wants
to treat the $resource objects as simple data objects - with the
exception of $save() and $delete().
But I though I'd ask if it was possible to add methods. The related
question is, if it is possible, is it a good idea?
Thanks,
Sean
You can define your own methods. For example, I wanted to create verbs
that were inline with Ruby on Rails, so I added them as follows:
angular.service('SelectedPhotos', function($resource) {
return $resource('selected_photos/:selected_photo_id', {},
{ 'create': { method: 'POST' },
'index': { method: 'GET', isArray: true },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }});
});
However, I wrote this for 0.9.x, and although it continued to work
until 0.10.5, it is broken in 0.10.6. I am at present trying to figure
out how to fix it, but without success thus far. I would be very
interested if someone from the AngularJS team would chime in on how to
define resources in 0.10.6.
http://docs-next.angularjs.org/api/angular.module.ng.$resource looks
like it should help, but the examples don't provide the surrounding
context, so I don't know what needs to be in place for the "x =
$resource" to work.
-Daniel
I've done this in 9.x, 10.5, *and* 10.6. But that's not what I'm asking
for.
I want to add some custom JavaScript methods to the resource object
using something like jQuery.extend()
Is there any explicit support for doing this? e.g. a way to specify
mix-ins that get automatically applied by Resource.get()?
> However, I wrote this for 0.9.x, and although it continued to work
> until 0.10.5, it is broken in 0.10.6.
(I can't share my code but let me take a look at it and might get back
you with some tips)
-- Sean
1) added a module, like this:
var mym = angular.module('mym', []);
2) Changed service definitions from:
angular.service('Message', function($resource) {
to:
mym.factory('Message', function($resource) {
3) I also added the following config code:
mym.config(function($locationProvider, $httpProvider) {
// Configure existing providers
$httpProvider.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';
$locationProvider.hashPrefix('!');
});
I had to do this because, I needed to override the default content-type
of "application/json".
I'm pretty sure those were the only changes needed to move the services
from 10.5 to 10.6.
-- Sean
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/id2bPyhjKKoJ.}--
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.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.