瀏覽代碼

:recycle: grabbing all for single because we need the id

tags/0.9.0
J 4 年之前
父節點
當前提交
b1de443e28
共有 1 個文件被更改,包括 22 次插入5 次删除
  1. 22
    5
      vue-theme/src/pages/single.vue

+ 22
- 5
vue-theme/src/pages/single.vue 查看文件

187
              */
187
              */
188
             // modules are NOT plural because module key
188
             // modules are NOT plural because module key
189
             if (!this.$store.state[this.type]) return
189
             if (!this.$store.state[this.type]) return
190
+            let allPostsOfType = this.$store.state[this.type].all
190
 
191
 
192
+            /**
193
+             * Load posts if they're not already in state
194
+             */
195
+            // Find the single post from api if it's not already in state
196
+            // Then add it to our list
197
+            let singlePostData = allPostsOfType.filter(
198
+                post => post.slug == this.$route.params.slug,
199
+            )[0]
200
+
201
+            // Look if it exists before you try and load everything!
202
+            if(!singlePostData) {
203
+                const res = await this.$store.dispatch(`getAll${convertTitleCase(this.type)}s`, { sortType: null, params: null})
204
+                allPostsOfType = res
205
+                singlePostData = allPostsOfType.filter(
206
+                    post => post.slug == this.$route.params.slug,
207
+                )[0]
208
+            }
209
+
210
+            if (!singlePostData) return console.error('could not get single post data...')
191
             
211
             
192
             // NOT plural
212
             // NOT plural
193
-            const singlePostData = await this.$store.dispatch(
213
+            this.checkAndSetHero(singlePostData)
214
+            await this.$store.dispatch(
194
                 `getSingle${convertTitleCase(this.type)}`,
215
                 `getSingle${convertTitleCase(this.type)}`,
195
                 singlePostData.id,
216
                 singlePostData.id,
196
             )
217
             )
197
-            
198
-            if (!singlePostData) return console.error('could not get single post data...')
199
-            this.checkAndSetHero(singlePostData)
200
-
201
             this.loading = false
218
             this.loading = false
202
         },
219
         },
203
     },
220
     },

Loading…
取消
儲存