Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active February 27, 2025 15:49

Revisions

  1. Kcko revised this gist Feb 27, 2025. 3 changed files with 105 additions and 1 deletion.
    1 change: 0 additions & 1 deletion obj
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    _
    64 changes: 64 additions & 0 deletions obj.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    <!-- https://www.vuemastery.com/courses/component-design-patterns/one-object-to-rule-them-all -->

    <!-- To heavy , to complicated, all props we dont need ...-->
    <template>
    <main>
    <Component
    v-for="content in apiResponse"
    :key="content.id"
    :is="content.type"
    :article-title="content.title"
    :article-content="content.body"
    :ad-image="content.image"
    :ad-heading="content.heading"
    @click="content.type === 'NewsArticle' ? openArticle : openAd"
    @mouseover="content.type === 'NewsArticle' ? showPreview : trackAdEvent"
    />
    </main>
    </template>


    <!--Much better -->
    <template>
    <main>
    <Component
    v-for="content in apiResponse"
    :key="content.id"
    :is="content.type"
    v-bind="feedItem(content).attrs"
    v-on="feedItem(content).events"
    />
    </main>
    </template>

    <script>
    export default {
    methods: {
    feedItem(item) {
    if (item.type === 'NewsArticle') {
    return {
    attrs: {
    'article-title': item.title,
    'article-content': item.content
    },
    events: {
    click: this.openArticle,
    mouseover: this.showPreview
    }
    }
    } else if (item.type === 'NewsAd') {
    return {
    attrs: {
    'ad-image': item.image,
    'ad-heading': item.heading
    },
    events: {
    click: this.openAd,
    mouseover: this.trackAdEvent
    }
    }
    }
    }
    }
    }
    </script>
    41 changes: 41 additions & 0 deletions shorten.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <img v-bind="{ src: imageAttrs.src, alt: imageAttrs.text }" />
    <img v-bind="imageAttrs" />


    <template>
    <img v-on:click="openGallery" v-on:mouseover="showTooltip" />
    </template>

    <script>
    export default {
    methods: {
    openGallery() { ... },
    showTooltip() { ... }
    }
    }
    </script>

    <!-- ////////////////////////////// -->

    <template>
    <img v-on="{ click: openGallery, mouseover: showTooltip }" />
    </template>

    <template>
    <img v-on="inputEvents" />
    </template>

    <script>
    export default {
    computed: {
    inputEvents: {
    click: this.openGallery,
    mouseover: this.showTooltip
    }
    },
    methods: {
    openGallery() { ... },
    showTooltip() { ... }
    }
    }
    </script>
  2. Kcko created this gist Feb 27, 2025.
    1 change: 1 addition & 0 deletions obj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    _