Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js mixins call parent method in overridden implementation

I'm using vuejs-datepicker component in a project however I need some custom behaviour that's why I decided to create my own datepicker and inject vuejs-datepicker as a mixin. The solution works fine but I'm looking for a way to call the parent method inside my overridden one. That's how my component looks for now:

  import Datepicker from 'vuejs-datepicker'

  export default {
    props: {
      /**
       * My custom property startDate to open a calendar on the given date by default
       */
      startDate: {
        validator: function (val) {
          return val === null || val instanceof Date || typeof val === 'string'
        },
        default: new Date()
      }
    },

    mixins: [ Datepicker ],

    methods: {
      /**
       * I override parent method setPageDate to read default startDate when none is specified
       */
      setPageDate (date) {
        // my code to set default date from the property
        if (!date) {
          date = this.startDate
        }

        // this part is the same as in the original method
        this.pageDate = new Date(date.getFullYear(), date.getMonth(), 1, date.getHours(), date.getMinutes()).getTime()
      }
    }
  }

Just one line of copied code isn't a big deal but I expect I need to override more methods in the future. So I'm looking for the best way to call that parent method inside my implementation, something like this.parent(date) or this.super(date). Is it possible at all?

like image 267
ConstantineUA Avatar asked Aug 12 '17 19:08

ConstantineUA


1 Answers

There is no super or anything like that. You could do something like this:

  setPageDate (date) {
    // my code to set default date from the property
    if (!date) {
      date = this.startDate
    }

    Datepicker.methods.setPageDate.call(this, date)
  }
like image 140
Bert Avatar answered Sep 21 '22 09:09

Bert