Переглянути джерело

:recycle: better conditional loading for list pages and single pages

tags/0.9.0
J 5 роки тому
джерело
коміт
04c7395179
2 змінених файлів з 15 додано та 8 видалено
  1. 5
    2
      vue-theme/src/pages/list.vue
  2. 10
    6
      vue-theme/src/pages/single.vue

+ 5
- 2
vue-theme/src/pages/list.vue Переглянути файл

81
     },
81
     },
82
     watch: {
82
     watch: {
83
         $route(to, from){
83
         $route(to, from){
84
-            this.setHeroAndGetPosts()
84
+            let type = convertTitleCase(this.type)
85
+            if(!this[`all${type}Loaded`]) this.setHeroAndGetPosts()
85
         }
86
         }
86
     },
87
     },
87
     created() {
88
     created() {
88
-        this.setHeroAndGetPosts()
89
+        let type = convertTitleCase(this.type)
90
+        console.log('already loaded ?:', this[`all${type}Loaded`])
91
+        if(!this[`all${type}Loaded`]) this.setHeroAndGetPosts()
89
     }
92
     }
90
 }
93
 }
91
 </script>
94
 </script>

+ 10
- 6
vue-theme/src/pages/single.vue Переглянути файл

132
          * !: posts watcher fires when this finishes
132
          * !: posts watcher fires when this finishes
133
          */
133
          */
134
         let type = convertTitleCase(this.type)
134
         let type = convertTitleCase(this.type)
135
-        if(!this[`all${type}Loaded`] && type) {
135
+        let allPostsOfType = this.$store.state[dePluralize(this.type)].all
136
+        
137
+        /**
138
+         * Load posts if they're not already in state
139
+         */
140
+        if(!this[`all${type}Loaded`] && allPostsOfType.length < 1) {
136
             const res = await this.$store.dispatch(`getAll${type}`)
141
             const res = await this.$store.dispatch(`getAll${type}`)
137
-            const post = Object.values(res).filter(post => post.slug == this.$route.params.slug)[0]
138
-            this.$store.dispatch(`getSingle${dePluralize(type)}`, post.id)
139
-        } else {
140
-            const post = Object.values(this.$store.state[this.type].all).filter(post => post.slug == this.$route.params.slug)[0]
141
-            this.$store.dispatch(`getSingle${dePluralize(type)}`, post.id)
142
+            allPostsOfType = res
142
         }
143
         }
143
 
144
 
145
+        const single = Object.values(allPostsOfType).filter(post => post.slug == this.$route.params.slug)[0]
146
+        console.log(this.$route.params.slug)
147
+        this.$store.dispatch(`getSingle${dePluralize(type)}`, single.id)
144
     }
148
     }
145
 }
149
 }
146
 </script>
150
 </script>

Завантаження…
Відмінити
Зберегти