0

I'm trying to save a object with ngResource this way:

  var Tasks = $resource("http://example.com/task", {
    task: {
      id: '@id'
    }
  }, {
    'save': {
      method: 'POST',
      params: {
        key: "abc"
      }
    }
  });

  var task = new Tasks();
  task.id = "123";
  task.$save();

This is generating this URL:

 http://example.com/task?task=%7B%22id%22:%22@id%22%7D&key=abc

Why is the "@id" string not getting replaced with the actual data? Why is the key parameter not showing up?

fstr
  • 900
  • 3
  • 10
  • 31
  • your save method is 'POST'. That's why! by the way, you should use POST to change something at the server, never GET. – Fals Sep 08 '14 at 18:25
  • Thanks but I don't follow....what do I change to fix this? The code I'm writing here is for adding a new task, not getting one. – fstr Sep 08 '14 at 19:06

1 Answers1

0

I would highly suggest you modify the api to not pass json strings, but if you absolutely have to you can do something like the following

var Tasks = $resource("http://example.com/task", {
  task: '@json'
}, {
  'save': {
    method: 'POST',
    params: {
      key: "abc"
    }
  }
});

var task = new Tasks();
task.id = '123';
task.json = angular.toJson(task);
Vadim
  • 17,897
  • 4
  • 38
  • 62
  • My URL contains this now: task?id=123&key=abc which isn't what the API wants. The URL must contain a "task" parameter, and that parameter contains an ID parameter. – fstr Sep 08 '14 at 19:05
  • @fstr so the `@` helper only works one level deep. You will have to do the encoding manually. – Vadim Sep 08 '14 at 19:32
  • Ah, good to know. What's the cleanest was I can do it manually? Can I keep using $resource for this? – fstr Sep 08 '14 at 19:57
  • @fstr I updated my answer and you might have to play with the following. However, I would urge you to change your api if you can. – Vadim Sep 08 '14 at 20:02
  • OK, thanks! :) By the way, what's the justification for changing the API? They're doing it in a nonstandard way? – fstr Sep 08 '14 at 20:12