angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.55k stars 3.01k forks source link

How to change resolved data? #1791

Closed piernik closed 9 years ago

piernik commented 9 years ago

I've got states like this - details->edit

$stateProvider
    .state('app.companies.details', {
        url: '{id:[0-9]+}/',
        resolve: getCompanyDetails
    })
    .state('app.companies.details.edit', {
        url: 'edit/'
    })
;

When entering app.companies.details app gets data with resolve to itemDetails local - I can inject it and use with $scope. Then when entering app.companies.details.edit app has already itemDetails from parent view - it's not loading it. I'm using those details to edit form - $scope.data=itemDetails;.

If user changes form (eg. name) but not submitting it and returns to app.companies.details (using breadcrumbs) the itemDetails remembers change (and should not) so I have to use $scope.data=angular.copy(itemDetails);

But when user is submitting form, app returns to app.companies.details but I have old data (since I used angular.copy). How to update resolved data? Using itemDetails=$scope.data; after submitting is not working.

matgardon commented 9 years ago

Hi,

I'm not so sure this is an issue : I myself use this feature as such, so that I can backup the current data my user is working on in a step by step wizard, and enable him to keep the current modifications active between states for the current work session.

If my user does not press 'save' and quit the app or reload the page, the data is reloaded from the server, which is the desired behavior. If however he wants to 'submit'/'save' the changes, then the data is pushed back to the server. If he presses 'clear', the data is reset to defaults.

If this behavior was to be changed, then I would not have automatic synchronization of current modifications of the shared data between states, and I would have to code the synchronization manually.

For your use case, as far as I understand, the main problem is that you don't want to enable your user to keep the modifications he made on a form (only for the current working session) if he didn't submitted : then you just have to keep your form data separate from the injected resolved itemsDetails, and on a submit() call you juste push back the modifications made in your formData to the itemsDetails.

piernik commented 9 years ago

Thanks for response. So You suggest to work (form stage) on angular.copy data and after submiting data You should reload data from server?