File tree 2 files changed +35
-0
lines changed 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,22 @@ var PathUtils = require('./PathUtils');
9
9
*/
10
10
class Location {
11
11
12
+ /**
13
+ * Revives the location from its serialized form.
14
+ * Use `Location.revive` as a second argument to `JSON.parse`.
15
+ *
16
+ * var serialized = JSON.stringify(location);
17
+ * var deserialized = JSON.parse(serialized, Location.revive);
18
+ *
19
+ */
20
+ static revive ( key , value ) {
21
+ if ( key === '' ) {
22
+ return new Location ( value . path , value . navigationType ) ;
23
+ } else {
24
+ return value ;
25
+ }
26
+ }
27
+
12
28
constructor ( path , navigationType ) {
13
29
this . path = path ;
14
30
this . navigationType = navigationType || NavigationTypes . POP ;
@@ -26,6 +42,13 @@ class Location {
26
42
return PathUtils . getQuery ( this . path , options ) ;
27
43
}
28
44
45
+ toJSON ( ) {
46
+ return {
47
+ path : this . path ,
48
+ navigationType : this . navigationType
49
+ } ;
50
+ }
51
+
29
52
}
30
53
31
54
module . exports = Location ;
Original file line number Diff line number Diff line change 1
1
var expect = require ( 'expect' ) ;
2
2
var Location = require ( '../Location' ) ;
3
+ var NavigationTypes = require ( '../NavigationTypes' ) ;
3
4
4
5
describe ( 'Location' , function ( ) {
5
6
var location ;
6
7
8
+ it ( 'can be revived' , function ( ) {
9
+ location = new Location ( '/the/path' , NavigationTypes . POP ) ;
10
+
11
+ var serialized = JSON . stringify ( location ) ;
12
+ var revived = JSON . parse ( serialized , Location . revive ) ;
13
+
14
+ expect ( revived instanceof Location ) . toEqual ( true ) ;
15
+ expect ( revived . path ) . toEqual ( location . path ) ;
16
+ expect ( revived . navigationType ) . toEqual ( location . navigationType ) ;
17
+ } ) ;
18
+
7
19
describe ( 'with a query string' , function ( ) {
8
20
beforeEach ( function ( ) {
9
21
location = new Location ( '/the/path?the=query' ) ;
You can’t perform that action at this time.
0 commit comments