forked from episerver/musicfestival-vue-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepiDataModel.js
68 lines (58 loc) · 1.95 KB
/
epiDataModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* The module that is responsible for handling the state of the current content
* that is being either viewed or edited. This module will handle talking to
* the API when the model needs to be updated when navigating or editing the
* site.
*/
import api from '@/Scripts/api/api';
//actions for the epiDataModel module
export const UPDATE_MODEL_BY_URL = 'epiDataModel/UPDATE_MODEL_BY_URL';
export const UPDATE_MODEL_BY_CONTENT_LINK = 'epiDataModel/UPDATE_MODEL_BY_CONTENT_LINK';
const state = {
model: {},
modelLoaded: false
};
const UPDATE_MODEL = 'epiDataModel/UPDATE_MODEL';
const mutations = {
[UPDATE_MODEL](state, newModel) {
state.model = newModel;
state.modelLoaded = true;
},
};
const parameters = {
expand: '*'
};
const actions = {
[UPDATE_MODEL_BY_URL]({commit}, friendlyUrl) {
/**
* When updating a model by friendly URL we assume that the friendly URL
* contains every querystring parameter that we might need on the server.
*/
return api.getContentByFriendlyUrl(friendlyUrl, parameters).then(response => {
commit(UPDATE_MODEL, response.data);
});
},
[UPDATE_MODEL_BY_CONTENT_LINK]({commit, rootState}, contentLink) {
/**
* Updating a model by content link is done when something is being
* edited and when viewing a block. In order to be sure that we get the
* correct model, we need to keep any previously existing query string
* from the friendly URL.
*
* See the implementation of ExtendedContentModelMapper.GetContextMode
* for additional details.
*/
const params = Object.assign({},
parameters,
rootState.route.query
);
return api.getContentByContentLink(contentLink, params).then(response => {
commit(UPDATE_MODEL, response.data);
});
}
};
export default {
state,
mutations,
actions
};