Skip to content

Namespaced Mixins #7501

Closed
Closed
@GesJeremie

Description

@GesJeremie

What problem does this feature solve?

I want to be able to create a namespaced mixin.

Let's take this notification mixin for example:

(function()  {
    'use strict';

    window.mixins = window.mixins || {}

    window.mixins.notification = {
        methods: {
            show: function(type, message, duration) {
                duration = duration || 5000;

                new Noty({
                    type: type,
                    text: message,
                    timeout: duration,
                    theme: 'custom'
                }).show();
            }
        }
    }
}());

I can import it in my component and use it:

(function() {
    'use strict';

    Vue.component('basic-component', function() {
        mixins: [window.mixins.notification],

        created: function() {
            this.show();
        }
    })
})

But this doesn't scale since you don't really know where the show() method comes from.

This can be already "solved" using a prefix in the method such as:

(function() {
    'use strict';

    Vue.component('basic-component', function() {
        mixins: [window.mixins.notification],

        created: function() {
            this.mixinNotificationShow();
        }
    })
})

but it seems a bad "implementation".

What does the proposed API look like?

Let's take my first example:

(function()  {
    'use strict';

    window.mixins = window.mixins || {}

    window.mixins.notification = {
        methods: {
            show: function(type, message, duration) {
                duration = duration || 5000;

                new Noty({
                    type: type,
                    text: message,
                    timeout: duration,
                    theme: 'custom'
                }).show();
            }
        }
    }
}());

This could be injected in the component such as:

(function() {
    'use strict';

    Vue.component('basic-component', function() {
        mixins: [{mixin: window.mixins.notification, as: 'notification'}],

        created: function() {
            // Will produce a mixins namespace suffixed with the name given for the mixin
            this.mixins.notification.show();
        }
    })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions