@@ -16,86 +16,84 @@ const camelize = function (snakeText, capitalizeFirstLetter = true) {
16
16
return out
17
17
}
18
18
19
- const mutations = function ( singular ) {
20
- const mutations = { }
19
+ export default {
20
+ mutations ( singular ) {
21
+ const mutations = { }
21
22
22
- mutations [ `set_${ singular } ` ] = ( state , entity ) => { Vue . set ( state , entity . id , entity ) }
23
- mutations [ `remove_${ singular } ` ] = ( state , id ) => { Vue . delete ( state , id ) }
23
+ mutations [ `set_${ singular } ` ] = ( state , entity ) => { Vue . set ( state , entity . id , entity ) }
24
+ mutations [ `remove_${ singular } ` ] = ( state , id ) => { Vue . delete ( state , id ) }
24
25
25
- return mutations
26
- }
27
-
28
- const actions = function ( endpoint , singular , plural , editable = true , destroyable = true ) {
29
- const camelSingular = camelize ( singular )
30
- const camelPlural = camelize ( plural )
31
- const actions = { }
26
+ return mutations
27
+ } ,
28
+ actions ( endpoint , singular , plural , editable = true , destroyable = true ) {
29
+ const camelSingular = camelize ( singular )
30
+ const camelPlural = camelize ( plural )
31
+ const actions = { }
32
32
33
- actions [ `load${ camelPlural } ` ] = ( { commit} ) => {
34
- api . get ( endpoint )
35
- . then ( ( data ) => {
36
- for ( let i = 0 ; i < data . length ; i ++ ) {
37
- commit ( `set_${ singular } ` , data [ i ] )
38
- }
39
- } )
40
- . catch ( ( error ) => { console . log ( `ERROR in load${ camelPlural } :` , error ) } )
41
- }
42
- actions [ `load${ camelSingular } ` ] = ( { commit} , id ) => {
43
- api . get ( `${ endpoint } /${ id } ` )
44
- . then ( ( data ) => { commit ( `set_${ singular } ` , data ) } )
45
- . catch ( ( error ) => { console . log ( `ERROR in load${ camelSingular } :` , error ) } )
46
- }
47
- if ( editable ) {
48
- actions [ `create${ camelSingular } ` ] = ( { commit} , payload ) => {
49
- api . post ( endpoint , payload )
33
+ actions [ `load${ camelPlural } ` ] = ( { commit} ) => {
34
+ api . get ( endpoint )
50
35
. then ( ( data ) => {
51
- // Let's assume "data" is the new article, sent back by the API
52
- // We don't want to store the user input in our store : )
53
- commit ( `set_ ${ singular } ` , data )
36
+ for ( let i = 0 ; i < data . length ; i ++ ) {
37
+ commit ( `set_ ${ singular } ` , data [ i ] )
38
+ }
54
39
} )
55
- . catch ( ( error ) => { console . log ( `ERROR in create ${ camelSingular } :` , error ) } )
40
+ . catch ( ( error ) => { console . log ( `ERROR in load ${ camelPlural } :` , error ) } )
56
41
}
57
- actions [ `update${ camelSingular } ` ] = ( { commit} , payload ) => {
58
- api . patch ( `${ endpoint } /${ payload . id } ` , payload )
59
- . then ( ( data ) => {
60
- // Let's assume "data" is the updated article
61
- commit ( `set_${ singular } ` , data )
62
- } )
63
- . catch ( ( error ) => { console . log ( `ERROR in update${ camelSingular } :` , error ) } )
42
+ actions [ `load${ camelSingular } ` ] = ( { commit} , id ) => {
43
+ api . get ( `${ endpoint } /${ id } ` )
44
+ . then ( ( data ) => { commit ( `set_${ singular } ` , data ) } )
45
+ . catch ( ( error ) => { console . log ( `ERROR in load${ camelSingular } :` , error ) } )
64
46
}
65
- }
66
- if ( destroyable ) {
67
- actions [ `destroy${ camelSingular } ` ] = ( { commit} , id ) => {
68
- api . delete ( `${ endpoint } /${ id } ` )
69
- . then ( ( ) => { commit ( `remove_${ singular } ` , id ) } )
70
- . catch ( ( error ) => { console . log ( 'ERROR in DESTROY_ARTICLE:' , error ) } )
47
+ if ( editable ) {
48
+ actions [ `create${ camelSingular } ` ] = ( { commit} , payload ) => {
49
+ api . post ( endpoint , payload )
50
+ . then ( ( data ) => {
51
+ // Let's assume "data" is the new article, sent back by the API
52
+ // We don't want to store the user input in our store :)
53
+ commit ( `set_${ singular } ` , data )
54
+ } )
55
+ . catch ( ( error ) => { console . log ( `ERROR in create${ camelSingular } :` , error ) } )
56
+ }
57
+ actions [ `update${ camelSingular } ` ] = ( { commit} , payload ) => {
58
+ api . patch ( `${ endpoint } /${ payload . id } ` , payload )
59
+ . then ( ( data ) => {
60
+ // Let's assume "data" is the updated article
61
+ commit ( `set_${ singular } ` , data )
62
+ } )
63
+ . catch ( ( error ) => { console . log ( `ERROR in update${ camelSingular } :` , error ) } )
64
+ }
65
+ }
66
+ if ( destroyable ) {
67
+ actions [ `destroy${ camelSingular } ` ] = ( { commit} , id ) => {
68
+ api . delete ( `${ endpoint } /${ id } ` )
69
+ . then ( ( ) => { commit ( `remove_${ singular } ` , id ) } )
70
+ . catch ( ( error ) => { console . log ( 'ERROR in DESTROY_ARTICLE:' , error ) } )
71
+ }
71
72
}
72
- }
73
-
74
- return actions
75
- }
76
-
77
- const getters = function ( singular , plural ) {
78
- const lowCamelSingular = camelize ( singular , false )
79
- const lowCamelPlural = camelize ( plural , false )
80
-
81
- const getters = { }
82
73
83
- getters [ lowCamelPlural ] = state => state
84
- getters [ lowCamelSingular ] = state => id => state [ id ] || undefined
74
+ return actions
75
+ } ,
76
+ getters ( singular , plural ) {
77
+ const lowCamelSingular = camelize ( singular , false )
78
+ const lowCamelPlural = camelize ( plural , false )
85
79
86
- return getters
87
- }
80
+ const getters = { }
88
81
89
- export default function ( endpoint , singular , plural , editable = true , destroyable = true ) {
90
- const module = {
91
- state : { } , mutations : {
92
- ...mutations ( singular ) ,
93
- } , actions : {
94
- ...actions ( endpoint , singular , plural , editable , destroyable ) ,
95
- } , getters : {
96
- ...getters ( singular , plural ) ,
97
- } ,
98
- }
82
+ getters [ lowCamelPlural ] = state => state
83
+ getters [ lowCamelSingular ] = state => id => state [ id ] || undefined
99
84
100
- return module
85
+ return getters
86
+ } ,
87
+ generateModule ( endpoint , singular , plural , editable = true , destroyable = true ) {
88
+ return {
89
+ state : { } ,
90
+ mutations : {
91
+ ...this . mutations ( singular ) ,
92
+ } , actions : {
93
+ ...this . actions ( endpoint , singular , plural , editable , destroyable ) ,
94
+ } , getters : {
95
+ ...this . getters ( singular , plural ) ,
96
+ } ,
97
+ }
98
+ } ,
101
99
}
0 commit comments