Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to update existing Resource #64

Closed
francescobragagna opened this issue Jul 8, 2016 · 6 comments
Closed

how to update existing Resource #64

francescobragagna opened this issue Jul 8, 2016 · 6 comments
Assignees
Labels

Comments

@francescobragagna
Copy link

This is a question, for a clarification.

I get a Resource from server, and I would like to update it.
Looking the source code if I understood well the Resources are created with not configurable fields.
How should I do to change a Resource attribute and send it back to the server, as updated?

E.g.:
$http({url: '/message/23'}).then(function(message){
message.text = 'new Text'; //I cant't ! <<<<<<<<
message.$post();
});

I would like to above to try to clone the resource, or make a copy with configurable attribute...
I can't understand if I should use $response() or any other method.

Thanks

Francesco

@maennchen maennchen self-assigned this Jul 8, 2016
@maennchen
Copy link
Contributor

maennchen commented Jul 8, 2016

Resources are not mutable by design.

Your code should look like this:

$http({url: '/message/23'})
  .then((message) => {
    return message.$request().$post('self', {}, {
      text: 'New Text',
    });
  })
  .then(() => {
    console.log('Saved');
  })
  .catch((error) => {
    console.error(error);
  });

@maennchen
Copy link
Contributor

If you want to execute HTTP calls, you always have to use resource.$request().$[http method].

You can find the methods here:
https://github.com/LuvDaSun/angular-hal/blob/master/src/resource/hal-resource-client.factory.js

@maennchen
Copy link
Contributor

maennchen commented Jul 8, 2016

By the way: Which version of angular-hal are you using? My answer only applies to the latest major release (>= v2.0.0)

@maennchen
Copy link
Contributor

By the way: the Pull Request #63 will add a shorter syntax for HTTP calls on the resource itself. As soon as it is merged, the example code will look like this:

$http({url: '/message/23'})
  .then((message) => {
    return message.$request().$postSelf({
      text: 'New Text',
    });
  })
  .then(() => {
    console.log('Saved');
  })
  .catch((error) => {
    console.error(error);
  });

@francescobragagna
Copy link
Author

francescobragagna commented Jul 8, 2016

I will update to major soon.
I have one more question related.

In my client angular application, I maintain some resources list, and when i need to change one resource I can use at this point the $postSelf , or $post('self' ... )

But after a change like the described above, do you suggest to make an immediately $get, to obtain a new resource changed and replace the old immutable one?

As client, receiving 200, 201 I exepect the resource is changed as i requested.
So making a $get just to replace the resource seems to me be onerous.

@maennchen
Copy link
Contributor

But after a change like the described above, do you suggest to make an immediately $get, to obtain a new resource changed and replace the old immutable one?

Yes.

As client, receiving 200, 201 I exepect the resource is changed as i requested.
So making a $get just to replace the resource seems to me be onerous.

If you need a mutable object, just copy it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants