@@ -6,73 +6,7 @@ var DefaultRoute = Router.DefaultRoute;
6
6
var Routes = Router . Routes ;
7
7
var Link = Router . Link ;
8
8
var NotFoundRoute = Router . NotFoundRoute ;
9
-
10
- var api = 'http://addressbook-api.herokuapp.com/contacts' ;
11
- var _contacts = { } ;
12
- var _changeListeners = [ ] ;
13
- var _initCalled = false ;
14
-
15
- var ContactStore = {
16
-
17
- init : function ( ) {
18
- if ( _initCalled )
19
- return ;
20
-
21
- _initCalled = true ;
22
-
23
- getJSON ( api , function ( err , res ) {
24
- res . contacts . forEach ( function ( contact ) {
25
- _contacts [ contact . id ] = contact ;
26
- } ) ;
27
-
28
- ContactStore . notifyChange ( ) ;
29
- } ) ;
30
- } ,
31
-
32
- addContact : function ( contact , cb ) {
33
- postJSON ( api , { contact : contact } , function ( res ) {
34
- _contacts [ res . contact . id ] = res . contact ;
35
- ContactStore . notifyChange ( ) ;
36
- if ( cb ) cb ( res . contact ) ;
37
- } ) ;
38
- } ,
39
-
40
- removeContact : function ( id , cb ) {
41
- deleteJSON ( api + '/' + id , cb ) ;
42
- delete _contacts [ id ] ;
43
- ContactStore . notifyChange ( ) ;
44
- } ,
45
-
46
- getContacts : function ( ) {
47
- var array = [ ] ;
48
-
49
- for ( var id in _contacts )
50
- array . push ( _contacts [ id ] ) ;
51
-
52
- return array ;
53
- } ,
54
-
55
- getContact : function ( id ) {
56
- return _contacts [ id ] ;
57
- } ,
58
-
59
- notifyChange : function ( ) {
60
- _changeListeners . forEach ( function ( listener ) {
61
- listener ( ) ;
62
- } ) ;
63
- } ,
64
-
65
- addChangeListener : function ( listener ) {
66
- _changeListeners . push ( listener ) ;
67
- } ,
68
-
69
- removeChangeListener : function ( listener ) {
70
- _changeListeners = _changeListeners . filter ( function ( l ) {
71
- return listener !== l ;
72
- } ) ;
73
- }
74
-
75
- } ;
9
+ var ContactStore = require ( './ContactStore' ) ;
76
10
77
11
var App = React . createClass ( {
78
12
getInitialState : function ( ) {
@@ -219,38 +153,6 @@ var NotFound = React.createClass({
219
153
}
220
154
} ) ;
221
155
222
- // Request utils.
223
-
224
- function getJSON ( url , cb ) {
225
- var req = new XMLHttpRequest ( ) ;
226
- req . onload = function ( ) {
227
- if ( req . status === 404 ) {
228
- cb ( new Error ( 'not found' ) ) ;
229
- } else {
230
- cb ( null , JSON . parse ( req . response ) ) ;
231
- }
232
- } ;
233
- req . open ( 'GET' , url ) ;
234
- req . send ( ) ;
235
- }
236
-
237
- function postJSON ( url , obj , cb ) {
238
- var req = new XMLHttpRequest ( ) ;
239
- req . onload = function ( ) {
240
- cb ( JSON . parse ( req . response ) ) ;
241
- } ;
242
- req . open ( 'POST' , url ) ;
243
- req . setRequestHeader ( 'Content-Type' , 'application/json;charset=UTF-8' ) ;
244
- req . send ( JSON . stringify ( obj ) ) ;
245
- }
246
-
247
- function deleteJSON ( url , cb ) {
248
- var req = new XMLHttpRequest ( ) ;
249
- req . onload = cb ;
250
- req . open ( 'DELETE' , url ) ;
251
- req . send ( ) ;
252
- }
253
-
254
156
var routes = (
255
157
< Route handler = { App } >
256
158
< DefaultRoute handler = { Index } />
0 commit comments