Skip to content

Commit fc892c9

Browse files
committed
Added Create Method
1 parent 67d84db commit fc892c9

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/actions/Create.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1+
import Axios from '../orm/axios';
12
import Action from './Action'
3+
import Context from '../common/context'
24

35
export default class Create extends Action {
4-
static call ({ state, dispatch }, params = {}) {
5-
// console.log(state, dispatch, params);
6+
/**
7+
* Call $create method
8+
* @param {object} store
9+
* @param {object} params
10+
*/
11+
static async call ({ state, commit }, params = {}) {
12+
if(!params.data || typeof params !== 'object') {
13+
throw new TypeError("You must include a data object in the params to send a POST request", params)
14+
}
15+
16+
const context = Context.getInstance();
17+
const model = context.getModelFromState(state);
18+
const endpoint = Action.transformParams('$create', model, params);
19+
const axios = new Axios(model.methodConf.http);
20+
const request = axios.post(endpoint, params.data);
21+
22+
commit('onRequest');
23+
request
24+
.then(data => {
25+
commit('onSuccess')
26+
model.insertOrUpdate({
27+
data,
28+
});
29+
})
30+
.catch(error => commit('onError', error))
31+
32+
return request;
633
}
734
}

0 commit comments

Comments
 (0)