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

Saving to storage and redirecting results in storage not being updated (zero $timeout doesn't help) #124

Closed
artemf opened this issue Jul 30, 2015 · 7 comments

Comments

@artemf
Copy link

artemf commented Jul 30, 2015

I have 2 routes with different templates / controllers, I have this code in first controller:

    $scope.$storage = $localStorage;

    $scope.editVideoTemplate = function (videoTemplate) {
        $scope.$storage.videoTemplate = videoTemplate;
        $timeout(function() { $location.path('/editor'); });
    }

The first time editVideoTemplate is run videoTemplate gets saved in localStorage, but on consequent runs (selecting different videoTemplates) it fails to update the value in localStorage. So after initial save, EditorCtrl always gets initially saved videoTemplate from localStorage.

If I add 300ms timeout (instead of zero), it works as it should - videoTemplate gets saved and successfully retrieved every time.

Can we have direct setters / getters for these edge cases, when there's no point to force user to wait and force digest cycle?

@egilkh
Copy link
Contributor

egilkh commented Jul 30, 2015

You could use the $sync function before doing the redirect.

@artemf
Copy link
Author

artemf commented Jul 30, 2015

@egilkh Unfortunately, it doesn't work:

    $scope.$storage = $localStorage;

    $scope.editVideoTemplate = function (videoTemplate) {
        $scope.$storage.videoTemplate = videoTemplate;
        $scope.$storage.sync();
        $timeout(function() { $location.path('/editor'); });
    }

It behaves exactly as without the $sync().

And looking at the source code of $sync(), it looks like it just reads the contents from localStorage, not writes it back?

@egilkh
Copy link
Contributor

egilkh commented Jul 30, 2015

Indeed. Too early and no coffee. My bad.

Maybe get/set should be added to enable this type of functionality. Or extend $sync with a write as well.

I'm in the process of doing some refactoring to enable a few of the things discussed (in other issues or PRs) and I'll keep this in mind.

@artemf
Copy link
Author

artemf commented Jul 30, 2015

@egilkh Thanks! Having more control in these corner cases (like having explicit get/set) would definitely help!

@egilkh
Copy link
Contributor

egilkh commented Aug 19, 2015

Use $apply() to force a write to the storage.

@egilkh egilkh closed this as completed Aug 19, 2015
@hariom282538
Copy link

@egilkh @artemf Please share code snippet.

@hariom282538
Copy link

var setLocalStorage = function (token, uname)
{
$localStorage.token = token;
$localStorage.name = uname;

}
$timeout(setLocalStorage(token, userForm.uname), 500);

Module Used : ngStorage

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

No branches or pull requests

3 participants