@@ -59,7 +59,7 @@ See the [Example User Model](./src/modules/user/model.js) in the code or the fol
59
59
` ./modules/project/model.js `
60
60
61
61
``` js
62
- var $ = require (' jquery ' )
62
+ var request = require (' superagent-promise ' )
63
63
var sprintf = require (' util' ).format
64
64
var BASE_URL = ' https://www.optimizelyapis.com/experiment/v1'
65
65
var ENTITY = ' projects'
@@ -71,22 +71,20 @@ exports.entity = ENTITY
71
71
* @return {Promise}
72
72
*/
73
73
exports .fetch = function (id ) {
74
- return $ .ajax ({
75
- url: sprintf (' %s/%s/%s' , BASE_URL , ENTITY , id),
76
- method: ' GET' ,
77
- contentType: ' application/json' ,
78
- })
74
+ return request
75
+ .get (sprintf (' %s/%s/%s' , BASE_URL , ENTITY , id))
76
+ .accept (' json' )
77
+ .end ()
79
78
}
80
79
81
80
/**
82
81
* @return {Promise}
83
82
*/
84
83
exports .fetchAll = function () {
85
- return $ .ajax ({
86
- url: sprintf (' %s/%s' , BASE_URL , ENTITY ),
87
- method: ' GET' ,
88
- contentType: ' application/json' ,
89
- })
84
+ return request
85
+ .get (sprintf (' %s/%s' , BASE_URL , ENTITY ))
86
+ .accept (' json' )
87
+ .end ()
90
88
}
91
89
92
90
/**
@@ -95,21 +93,17 @@ exports.fetchAll = function() {
95
93
*/
96
94
exports .save = function (instance ) {
97
95
if (instance .id ) {
98
- return $ .ajax ({
99
- url: sprintf (' %s/%s/%s' , BASE_URL , ENTITY , instance .id ),
100
- method: ' PUT' ,
101
- contentType: ' application/json' ,
102
- data: JSON .stringify (instance),
103
- dataType: ' json'
104
- })
96
+ return request
97
+ .put (sprintf (' %s/%s/%s' , BASE_URL , ENTITY , instance .id ))
98
+ .type (' json' )
99
+ .send (instance)
100
+ .end ()
105
101
} else {
106
- return $ .ajax ({
107
- url: sprintf (' %s/%s' , BASE_URL , ENTITY ),
108
- method: ' POST' ,
109
- contentType: ' application/json' ,
110
- data: JSON .stringify (instance),
111
- dataType: ' json'
112
- })
102
+ return request
103
+ .post (sprintf (' %s/%s' , BASE_URL , ENTITY ))
104
+ .type (' json' )
105
+ .send (instance)
106
+ .end ()
113
107
}
114
108
}
115
109
@@ -118,10 +112,10 @@ exports.save = function(instance) {
118
112
* @return {Promise}
119
113
*/
120
114
exports .delete = function (instance ) {
121
- return $ . ajax ({
122
- url : sprintf (' %s/%s/%s' , BASE_URL , ENTITY , instance .id ),
123
- method : ' DELETE ' ,
124
- contentType : ' application/json ' ,
115
+ return request
116
+ . del ( sprintf (' %s/%s/%s' , BASE_URL , ENTITY , instance .id ))
117
+ . type ( ' json ' )
118
+ . end ()
125
119
})
126
120
}
127
121
```
0 commit comments