Properties disapear after getting the object form the cookiestore

100 Views Asked by At

I defined a $routeProvider that adds access to the route:

$routeProvider.when('/tracks/:trackTitle/:mediaTitle',
{
    templateUrl: 'views/track-detail.html',
    controller: 'MediaCtrl',
    access: access.user 
    ...
}

I am listening to $routeChangeStart change event and check that the user can access the page. If the does not I save the route in the cookiestore and load it after the user logs in.

$rootScope.$on("$routeChangeStart", function (event, next, current) {
    $rootScope.error = null;
    if (!Auth.authorize(next.access)) {
        if (!Auth.isLoggedIn())
      $cookieStore.put('current.user.originalPath', originalPath);
      $location.path('/signup');
    }});

The problem is that the next object for some reason changes between the put and get from the cookiestore.

Before the $cookieStore.put next is:

$$route: Object
params: Object
pathParams: Object
__proto__: Object

And when I get the object using $cookieStore.get the value is

params: Object
pathParams: Object
__proto__: Object

I am getting the object without $$route object.

Why is that and how can I fix it?

1

There are 1 best solutions below

0
On BEST ANSWER

This happens because values are "JSONified" by Angular before put into the cookie-store.
And Angular's toJson() method ignores any properties that start with $. You could use JSON.stringify() (not Angular's toJson()) to pass the value already "JSONified" or rename the property $$route to route (or anything that doesn't start with $).


Unfortunately, since values are "JSONified" it is not possible to store all kinds of preperties.
E.g. functions or non-JSONifiable objects (window, scopes, nodes etc) won't be retained.

See, also, this short demo.


I doubt you actually need the whole $$route object, so I suggest you store just the properties you are interested in (e.g. path, pathParams etc).