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

When $save fails the item is modified regardless #368

Open
vprogramador opened this issue Feb 1, 2016 · 0 comments
Open

When $save fails the item is modified regardless #368

vprogramador opened this issue Feb 1, 2016 · 0 comments

Comments

@vprogramador
Copy link

this is the description of the reason for opening this issue

First, I create a restmod model:

app.factory('Negocio', function(restmod) {
  return restmod.model('http://localhost:3000/negocios')
})

Then, I'm resolving a collection for my controller using the model called 'Negocio':

resolve: {
  negocios: ['Negocio',
    function(Negocio){
      return Negocio.$search().$resolve().$asPromise()
    }],
},

and then adding it to the $scope

$scope.negocios = negocios

I'm using this collection for showing all the items using ng-repeat in an index view.
In this view I have a button that opens a modal for editing the chosen item.
This modal is created with a service provided by ui.bootstrap, and has
its own controller, which first resolve the chosen item and then add it to
its $scope.

resolve: {
  negocio: function() {
    return utils.encontrarPorId($scope.negocios, negocioId)
  },
}

// in the modal controller
$scope.negocio = negocio

The function called encontrarPorId receives as arguments an array (the collection)
and an Id (the one of the chose item) and returns the item of the collection that matches the Id.

In the view associated to the modal controller, I use $scope.negocio in a form for getting
the user input, and this form has a submit button that saves the changes:

$scope.negocio.$save()

The problem is that when the users edit the item info using the form, $scope.negocio changes
immediately because of the angular data binding, and it doesn't matter if the modal is cancelled (closed)
whithout pressing the submit button or if the user click the submit button and the PUT request fail,
the item info in the collection gets modified.

Right now I'm resolving the item in the controller making a new request to the API for avoiding this issue

resolve: {
  negocio: function() {
    return $scope.negocios.$find(negocioId).$resolve().$asPromise()
  },
}

But I'll like to resolve the item from the collection without making a new request to the API, because I
already have the item info, don't want to bring it again.

Looking for a solution to my problem found the restmod issue #49 and thought it could be related.

Thanks for your help :)

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

No branches or pull requests

1 participant