瀏覽代碼

:recycle: trying a different single template loading strategy

tags/0.9.0
J 4 年之前
父節點
當前提交
27114e254d
共有 1 個檔案被更改,包括 24 行新增13 行删除
  1. 24
    13
      vue-theme/src/pages/single.vue

+ 24
- 13
vue-theme/src/pages/single.vue 查看文件

2
 .page--single.f-col.between
2
 .page--single.f-col.between
3
     gallery(v-if="activeGalleryIndex >= 0" :activeImageIndex="activeImageIndex" :images="imagesInGallery" @close="closeGallery")
3
     gallery(v-if="activeGalleryIndex >= 0" :activeImageIndex="activeImageIndex" :images="imagesInGallery" @close="closeGallery")
4
 
4
 
5
-    article.w-max(v-if="!singlePost || loading")
5
+    article.w-max.f-grow.shadow(v-if="!singlePost || loading")
6
         header
6
         header
7
             p loading...
7
             p loading...
8
     article(v-else).w-max.f-grow.shadow
8
     article(v-else).w-max.f-grow.shadow
28
             ul
28
             ul
29
                 li.f-row.between(v-for="artist in p2pPostsByType['artist']")
29
                 li.f-row.between(v-for="artist in p2pPostsByType['artist']")
30
                     card(:content="artist" type="artist" :wide="true" :hide-type="true")
30
                     card(:content="artist" type="artist" :wide="true" :hide-type="true")
31
-            
31
+
32
         credits(v-if="type === 'episode' && singlePost" :post="singlePost")
32
         credits(v-if="type === 'episode' && singlePost" :post="singlePost")
33
-        
33
+
34
         //- end of article icon
34
         //- end of article icon
35
         footer.f-col
35
         footer.f-col
36
             img(src="../star.svg")
36
             img(src="../star.svg")
39
 </template>
39
 </template>
40
 
40
 
41
 <script>
41
 <script>
42
-import { mapGetters, mapState } from 'vuex'
43
 import card from '@/components/card.vue'
42
 import card from '@/components/card.vue'
44
 import sidebar from '@/components/sidebars/sidebar'
43
 import sidebar from '@/components/sidebars/sidebar'
45
 import gallery from '@/components/gallery/'
44
 import gallery from '@/components/gallery/'
56
     components: { sidebar, gallery, credits, card, breadcrumb },
55
     components: { sidebar, gallery, credits, card, breadcrumb },
57
     props: {
56
     props: {
58
         sidebar: { type: Boolean },
57
         sidebar: { type: Boolean },
59
-        id: { type: Number },
60
     },
58
     },
61
     mixins: [postTypeGetters, scrollTop, heroUtils],
59
     mixins: [postTypeGetters, scrollTop, heroUtils],
62
     data() {
60
     data() {
162
         // _setHeroInfo(post) {} from mixin
160
         // _setHeroInfo(post) {} from mixin
163
         // _clearHero(store) {} from mixin
161
         // _clearHero(store) {} from mixin
164
         /**
162
         /**
165
-         * Everytime the posts object changes
163
+         * Everytime the post object changes
166
          * we use this to set a new HERO
164
          * we use this to set a new HERO
167
          * in vuex
165
          * in vuex
168
-         * @param {object} posts
166
+         * @param {object} post
169
          */
167
          */
170
         checkAndSetHero(post) {
168
         checkAndSetHero(post) {
171
             this._clearHero(this.$store)
169
             this._clearHero(this.$store)
182
         },
180
         },
183
 
181
 
184
         async loadPostData() {
182
         async loadPostData() {
183
+            this.loading = true
185
             /**
184
             /**
186
              * Conditionally load based on post type
185
              * Conditionally load based on post type
187
              * which is derived from the route
186
              * which is derived from the route
188
              */
187
              */
189
             // modules are NOT plural because module key
188
             // modules are NOT plural because module key
190
             if (!this.$store.state[this.type]) return
189
             if (!this.$store.state[this.type]) return
191
-            let allPostsOfType = this.$store.state[this.type].all
190
+            const allPostsOfTypeInStore = this.$store.state[this.type].all
192
 
191
 
193
             /**
192
             /**
194
              * Load posts if they're not already in state
193
              * Load posts if they're not already in state
195
              */
194
              */
196
             // Find the single post from api if it's not already in state
195
             // Find the single post from api if it's not already in state
197
             // Then add it to our list
196
             // Then add it to our list
198
-            let singlePostData = allPostsOfType.filter(
197
+            let singlePostData = allPostsOfTypeInStore.filter(
199
                 post => post.slug == this.$route.params.slug,
198
                 post => post.slug == this.$route.params.slug,
200
             )[0]
199
             )[0]
201
 
200
 
202
-            if (!singlePostData) return console.error('could not get single post data...')
203
-            
204
-            // NOT plural
201
+            // Look if it exists before you try and load everything!
202
+            if (!singlePostData) {
203
+                console.error('Could not find single post in store; Fetching everything...')
204
+                const res = await this.$store.dispatch(
205
+                    `getAll${convertTitleCase(this.type)}s`,
206
+                    { sortType: null, params: null}
207
+                )
208
+                singlePostData = res.filter(
209
+                    post => post.slug == this.$route.params.slug,
210
+                )[0]
211
+            }
212
+
213
+            /**
214
+             * At the point we MUST have singlePostData
215
+             */
205
             this.checkAndSetHero(singlePostData)
216
             this.checkAndSetHero(singlePostData)
206
             await this.$store.dispatch(
217
             await this.$store.dispatch(
207
                 `getSingle${convertTitleCase(this.type)}`,
218
                 `getSingle${convertTitleCase(this.type)}`,
208
-                this.id,
219
+                singlePostData.id,
209
             )
220
             )
210
             this.loading = false
221
             this.loading = false
211
         },
222
         },

Loading…
取消
儲存