Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about add some hooks to 'v-on' directive, such as 'before' and 'after' #2493

Closed
Jokcy opened this issue Mar 16, 2016 · 3 comments
Closed

Comments

@Jokcy
Copy link

Jokcy commented Mar 16, 2016

New feature request:
How about add some hooks to 'v-on' directive, such as 'before' and 'after'.

What I want to do:
I want to add a some validate before click event being triggered, and lots of button not to be validated before click.

How it works:
If I have the before hook, I need to run the 'before method' and only the method return true I will do the real event binding.

@airtonix
Copy link

How I handle this at the moment:

...
  methods: {
    someButtonClickHandler () {
      new Promise( (resolve, reject) => {
          // setup logic
      })
        .then( (result) => {
           // before logic,
        })
        .then( (result) => {
           // the handler
        })
        .catch( (err) => {
           // handle exceptions
        })
        .finally( () => {
          // cleanup ?
        })
    }  
  }
...

Might not be the right way, but that's how I'm dealing with your use case.

specifically:

import store from 'application/store';
import {storeAnswer} from 'application/store/actions';
import validate from 'validate.js';

export default {

  vuex: {
    storeAnswer
  },

  computed: {

    firstName: {
      get () { return store.state.Thing.firstName; },
      set (value) {
        validate.async(data, constraints)
          .then( () => {
             this.storeAnswer(value);
          });
          .catch((err)=>{
            // handle validation errors ?
          });
      }
    }

  }
};

@yyx990803
Copy link
Member

You can simply do it inside the handler. If you need to reuse the same logic, you can either make that a filter or just compose it inside JS:

function guard (handler) {
  return function (e) {
    if (...) {
      e.preventDefault()
    }
    return handler.call(this, e)
  }
}

methods: {
  onClick: guard(function (e) {
    // ...
  })
}

@Jokcy
Copy link
Author

Jokcy commented Mar 17, 2016

@yyx990803 All right, I guess I get your point, THX a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants