1
1
define ( [
2
2
"hr/hr" ,
3
3
"api" ,
4
- "notifications"
5
- ] , function ( hr , api , notifications ) {
4
+ "notifications" ,
5
+ "collections/reports"
6
+ ] , function ( hr , api , notifications , Reports ) {
6
7
var User = hr . Model . extend ( {
7
8
defaults : {
8
9
'email' : hr . Storage . get ( "email" , "" ) ,
9
- 'token' : hr . Storage . get ( "token" , "" )
10
+ 'token' : hr . Storage . get ( "token" , "" ) ,
11
+ 'settings' : hr . Storage . get ( "settings" ) || { }
10
12
} ,
11
13
12
14
/*
@@ -15,14 +17,30 @@ define([
15
17
initialize : function ( ) {
16
18
User . __super__ . initialize . apply ( this , arguments ) ;
17
19
18
- // User change
19
- this . on ( "change" , function ( ) {
20
- hr . Storage . set ( "email" , this . get ( "email" , "" ) ) ;
21
- hr . Storage . set ( "token" , this . get ( "token" , "" ) ) ;
20
+ // Reports list
21
+ this . reports = new Reports ( ) ;
22
+ this . reports . on ( "add remove set" , function ( ) {
23
+ var settings = _ . extend ( { } , this . get ( "settings" , { } ) , {
24
+ 'reports' : this . reports . toJSON ( )
25
+ } ) ;
26
+
27
+ this . set ( "settings" , settings ) ;
28
+ } , this ) ;
22
29
30
+ // User change
31
+ this . on ( "change:token" , function ( ) {
23
32
this . connectNotifications ( ) ;
24
33
} , this ) ;
34
+
35
+ this . on ( "set" , _ . throttle ( this . syncSettings , 1000 ) , this ) ;
25
36
37
+ if ( this . isAuth ( ) ) {
38
+ console . log ( "user is logged" ) ;
39
+ this . syncSettings ( {
40
+ 'updateReports' : true
41
+ } ) ;
42
+ }
43
+
26
44
this . connectNotifications ( ) ;
27
45
return this ;
28
46
} ,
@@ -57,6 +75,8 @@ define([
57
75
'password' : password
58
76
} ) . done ( function ( data ) {
59
77
that . set ( data ) ;
78
+ this . syncLocal ( ) ;
79
+ this . reports . reset ( this . get ( "settings.reports" , [ ] ) ) ;
60
80
} ) ;
61
81
} ,
62
82
@@ -79,6 +99,7 @@ define([
79
99
* Log out the user
80
100
*/
81
101
logout : function ( ) {
102
+ if ( ! this . isAuth ( ) ) return this ;
82
103
hr . Storage . clear ( ) ;
83
104
this . set ( {
84
105
'email' : null ,
@@ -87,73 +108,42 @@ define([
87
108
return this ;
88
109
} ,
89
110
90
- /*
91
- * Key/Value storage for user settings
92
- *
93
- * TODO: add sync with server
94
- */
95
-
96
- getSettings : function ( key ) {
97
- return hr . Storage . get ( this . get ( "email" ) + "/" + key ) ;
98
- } ,
99
- setSettings : function ( key , value ) {
100
- hr . Storage . set ( this . get ( "email" ) + "/" + key , value ) ;
101
- this . trigger ( "settings.change." + key , key ) ;
102
- this . syncSettings ( ) ;
103
- return this ;
104
- } ,
105
111
106
112
/*
107
113
* Sync settings
108
114
*/
109
- syncSettings : function ( ) {
110
-
111
- } ,
112
-
113
-
114
- /*
115
- * Get list reports
116
- */
117
- reports : function ( ) {
118
- var reports = this . getSettings ( "reports" ) || [ ] ;
119
- return _ . reduce ( reports , function ( memo , report ) {
120
- if ( _ . isString ( report ) ) {
121
- memo . push ( {
122
- 'id' : _ . uniqueId ( 'report_' ) ,
123
- 'report' : report
124
- } ) ;
125
- } else if ( _ . isObject ( report ) ) {
126
- memo . push ( report ) ;
115
+ syncSettings : function ( options ) {
116
+ var that = this ;
117
+ if ( ! this . isAuth ( ) ) return this ;
118
+
119
+ // options
120
+ options = _ . defaults ( options || { } , {
121
+ 'updateReports' : false
122
+ } )
123
+
124
+ // Sync with server
125
+ return api . request ( "post" , this . get ( "token" ) + "/account/sync" , {
126
+ 'settings' : this . get ( "settings" , { } )
127
+ } ) . done ( function ( data ) {
128
+ // Update user
129
+ that . set ( data , {
130
+ silent : true
131
+ } ) ;
132
+
133
+ // Update reports
134
+ if ( options . updateReports ) {
135
+ that . reports . reset ( that . get ( "settings.reports" , [ ] ) ) ;
127
136
}
128
- return memo ;
129
- } , [ ] ) ;
130
- } ,
131
137
132
- /*
133
- * Add a new report
134
- */
135
- addReport : function ( report , reportId ) {
136
- var reports = this . reports ( ) ;
137
- reports . push ( {
138
- 'id' : reportId || _ . uniqueId ( 'report_' ) ,
139
- 'report' : report
138
+ that . syncLocal ( ) ;
140
139
} ) ;
141
- this . setSettings ( "reports" , reports ) ;
142
- return this ;
143
140
} ,
144
141
145
- /*
146
- * Remove a report
147
- */
148
- removeReport : function ( reportId ) {
149
- var reports = this . reports ( ) ;
150
- _ . each ( reports , function ( report , i ) {
151
- if ( report . id == reportId ) {
152
- delete reports [ i ] ;
153
- }
154
- } ) ;
155
- this . setSettings ( "reports" , reports ) ;
156
- return this ;
142
+ syncLocal : function ( ) {
143
+ // Sync in localStorage
144
+ hr . Storage . set ( "email" , this . get ( "email" , "" ) ) ;
145
+ hr . Storage . set ( "token" , this . get ( "token" , "" ) ) ;
146
+ hr . Storage . set ( "settings" , this . get ( "settings" , { } ) ) ;
157
147
}
158
148
} , {
159
149
current : null
0 commit comments