Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const state = {
- pets: []
- };
- const mutations = {
- SET_PETS (state, response) {
- state.pets = response;
- }
- };
- const actions = {
- FETCH_PETS: (state) => {
- setTimeout(function() {
- state.commit('SET_PETS', ['t7m12qbvb/apple_9', '6pat9znxz/1448127928_kiwi'])
- }, 1000)
- }
- }
- const getters = {
- pets(state){
- if(!state.pets.length){
- state.dispatch("FETCH_PETS")
- }
- return state.pets
- }
- }
- const store = new Vuex.Store({
- state,
- mutations,
- actions,
- getters
- });
- store.registerModule('session', {
- namespaced: true,
- state: {
- session: {hasPermission:{}},
- sessionLoaded:false
- },
- mutations: {
- changeSession: function (state, value)
- {
- state.session = value;
- },
- changeSessionLoaded: function (state)
- {
- state.sessionLoaded = true;
- }
- },
- actions: {
- loadSession(context)
- {
- // your Ajax-request, that will set context.state.session=something
- }
- }
- });
- Vue.mixin({
- computed: {
- $session: function () { return this.$store.state.session.session; },
- },
- mounted:function()
- {
- if(this.$parent==undefined && !this.$store.state.session.sessionLoaded)
- {
- this.$store.dispatch("session/loadSession");
- this.$store.commit("changeSessionLoaded");
- }
- },
- });
Add Comment
Please, Sign In to add comment