Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:04:30+00:00 2026-06-14T06:04:30+00:00

I would like to use $resource to call my RESTful web service, (which I

  • 0

I would like to use $resource to call my RESTful web service, (which I am still working on) but I would like to find out if I got my AngularJS script correct first.

The todo DTO has: {id, order, content, done}

:cmd is so I can call api/1/todo/reset to clear the todo table in database.

Here is the code with comment of my understanding:

function TodoService($resource) {
    var src = $resource('api/1/todo/:id:cmd',
              {id: "@id", cmd: "@cmd"}, //parameters default
              {
                ListTodos: { method: "GET", params: {} },
                GetTodo: { method: "GET", params: { id: 0 } },                            
                CreateTodo: { method: "POST", params: { content: "", order: 0, done: false } },
                UpdateTodo: { method: "PATCH", params: { /*...*/ } },
                DeleteTodo: { method: "DELETE", params: { id: 0 } },
                ResetTodos: { method: "GET", params: { cmd: "reset" } },
              });

    //Usage:

    //GET without ID
    //it calls -> api/1/todo
    src.ListTodos();

    //GET with ID
    //it calls -> api/1/todo/4
    src.GetTodo({ id: 4 });

    //POST with content, order, done
    //it calls -> api/1/todo
    src.CreateTodo({ content: "learn Javascript", order: 1, done: false });

    //UPDATE content only
    //it calls -> api/1/todo/5
    src.UpdateTodo({ id: 5, content: "learn AngularJS" }); 

    //UPDATE done only
    //it calls -> api/1/todo/5
    src.UpdateTodo({ id: 5, done: true });

    //RESET with cmd
    //it calls -> api/1/todo/reset
    src.ResetTodos();
}

One particular thing I am not sure is the PATCH method, I don’t want to update everything, can I update just one field? Am I constructing this piece of code correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:04:31+00:00Added an answer on June 14, 2026 at 6:04 am

    $resource was meant to retrieve data from an endpoint, manipulate it and send it back. You’ve got some of that in there, but you’re not really leveraging it for what it was made to do.

    It’s fine to have custom methods on your resource, but you don’t want to miss out on the cool features it comes with OOTB.

    EDIT: I don’t think I explained this well enough originally, but $resource does some funky stuff with returns. Todo.get() and Todo.query() both return the resource object, and pass it into the callback for when the get completes. It does some fancy stuff with promises behind the scenes that mean you can call $save() before the get() callback actually fires, and it will wait. It’s probably best just to deal with your resource inside of a promise then() or the callback method.

    Standard use

    var Todo = $resource('/api/1/todo/:id');
    
    //create a todo
    var todo1 = new Todo();
    todo1.foo = 'bar';
    todo1.something = 123;
    todo1.$save();
    
    //get and update a todo
    var todo2 = Todo.get({id: 123});
    todo2.foo += '!';
    todo2.$save();
    
    //which is basically the same as...
    Todo.get({id: 123}, function(todo) {
       todo.foo += '!';
       todo.$save();
    });
    
    //get a list of todos
    Todo.query(function(todos) {
      //do something with todos
      angular.forEach(todos, function(todo) {
         todo.foo += ' something';
         todo.$save();
      });
    });
    
    //delete a todo
    Todo.$delete({id: 123});
    

    Likewise, in the case of what you posted in the OP, you could get a resource object and then call any of your custom functions on it (theoretically):

    var something = src.GetTodo({id: 123});
    something.foo = 'hi there';
    something.UpdateTodo();
    

    I’d experiment with the OOTB implementation before I went and invented my own however. And if you find you’re not using any of the default features of $resource, you should probably just be using $http on it’s own.

    Update: Angular 1.2 and Promises

    As of Angular 1.2, resources support promises. But they didn’t change the rest of the behavior.

    To leverage promises with $resource, you need to use the $promise property on the returned value.

    Example using promises

    var Todo = $resource('/api/1/todo/:id');
    
    Todo.get({id: 123}).$promise.then(function(todo) {
       // success
       $scope.todos = todos;
    }, function(errResponse) {
       // fail
    });
    
    Todo.query().$promise.then(function(todos) {
       // success
       $scope.todos = todos;
    }, function(errResponse) {
       // fail
    });
    

    Just keep in mind that the $promise property is a property on the same values it was returning above. So you can get weird:

    These are equivalent

    var todo = Todo.get({id: 123}, function() {
       $scope.todo = todo;
    });
    
    Todo.get({id: 123}, function(todo) {
       $scope.todo = todo;
    });
    
    Todo.get({id: 123}).$promise.then(function(todo) {
       $scope.todo = todo;
    });
    
    var todo = Todo.get({id: 123});
    todo.$promise.then(function() {
       $scope.todo = todo;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create one EMF object and use it in various Resource
I would like to use R to extract the speaker out of scripts formatted
I would like to use the logout function from Django but not sure how
I would like to use GIO (which is part of GLIB) on Android by
I would like to use the MFMailComposeViewController mailComposeDelegate property with completion block syntax, but
I have a Sinatra based REST service app and I would like to call
I have an array of Integers in Java, I would like use only a
I would like to use Maven's password encryption such as it uses for nodes
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.