Guest User

Untitled

a guest
Aug 14th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. const state = {
  2. pets: []
  3. };
  4.  
  5. const mutations = {
  6. SET_PETS (state, response) {
  7. state.pets = response;
  8. }
  9. };
  10.  
  11. const actions = {
  12. FETCH_PETS: (state) => {
  13. setTimeout(function() {
  14. state.commit('SET_PETS', ['t7m12qbvb/apple_9', '6pat9znxz/1448127928_kiwi'])
  15. }, 1000)
  16. }
  17. }
  18.  
  19. const getters = {
  20. pets(state){
  21. if(!state.pets.length){
  22. state.dispatch("FETCH_PETS")
  23. }
  24. return state.pets
  25. }
  26. }
  27.  
  28. const store = new Vuex.Store({
  29. state,
  30. mutations,
  31. actions,
  32. getters
  33. });
  34.  
  35. store.registerModule('session', {
  36. namespaced: true,
  37. state: {
  38. session: {hasPermission:{}},
  39. sessionLoaded:false
  40. },
  41. mutations: {
  42. changeSession: function (state, value)
  43. {
  44. state.session = value;
  45. },
  46. changeSessionLoaded: function (state)
  47. {
  48. state.sessionLoaded = true;
  49. }
  50.  
  51. },
  52. actions: {
  53. loadSession(context)
  54. {
  55. // your Ajax-request, that will set context.state.session=something
  56. }
  57. }
  58. });
  59.  
  60. Vue.mixin({
  61. computed: {
  62. $session: function () { return this.$store.state.session.session; },
  63. },
  64. mounted:function()
  65. {
  66. if(this.$parent==undefined && !this.$store.state.session.sessionLoaded)
  67. {
  68. this.$store.dispatch("session/loadSession");
  69. this.$store.commit("changeSessionLoaded");
  70. }
  71. },
  72. });
Add Comment
Please, Sign In to add comment