npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-modal-caller

v1.0.31

Published

an easy way to call a modal for vue.js

Downloads

5

Readme

vue-modal-caller

The project trys to create an easier way to call custom modal with vue.js. The project is designed with singular instance model, so developers need not to worry repeated object created and problems with performance.
这个库致力于在vue框架中,创造一种简单的调用自制模态框的方法。这个基于单例模式设计,不会创建重复的实例,开发者不必担心性能问题。

Install

npm install vue-modal-caller --save

How to use

Include plugin in your main.js file. And init all your custom modals.

main.js

import Modal from 'vue-modal-caller'
import loginModal from './components/modals/loginModal'
import warnModal from './components/modals/warnModal'
import infoModal from './components/modals/infoModal'
...
Vue.use(Modal, {
  modules: [ loginModal, warnModal, infoModal ]
}})

Then in your vue page, you can call your modal with an instance we provide. Get it by $getModalHandler function which we have attached to Vue.prototype. In the created hook, the login modal will be called out, and you don't need to add codes in your template part.

home.vue

export default {
  data() {
    return {
      modalHandler: this.$getModalHandler()
    }
  },
  created() {
    this.modalHandler.show('loginHandler');
  }
/*
By default, show method will use "modal" name to locate component.
Please ensure an unique name property for every of your modal.
*/
}

Rcommend usage

We assume that you put all your custom modals into one directory, and here comes my suggested configuaration when init.

import Modal from 'vue-modal-caller'
let modals = require.context('./components/modals', true, /\.(vue)$/);
let modules = modals.keys().map(key => modals(key).default);

Vue.use(Modal, {
  modules
}})

Vue.prototype.$modal = Vue.prototype.$getModalHandler();
/*
require.context() is an api from webpack, which can import context dynamically. In this way when you add new modal, you are not supposed to import the module again.
*/

Everywhere you want to call your modal, you don't have to wirte additional code anymore:

somePage.vue

created() {
  this.$modal.show('loginModdal');
}
/*
Just by one line, modal is called in you page
*/

Communication between modal and parent

somePage.vue

created() {
  this.$modal.show('loginModdal', {
    props: {
      userName: "admin",
      password: "123"
    },
    invokers: {
      loginSuccess:() => {
        this.$modal.closeDisplayEl().then(modal => {
          alert('success login')
        })
      }
    }
  });
}
/*
You can pass your props and invokers into your modal. Props refer to the props your modal supposed to receive. And invokers can be called by Vue's api $emit();
*/

Let's check the code in loginModal.vue

loginModal.vue

props: ['userName', 'password'],
methods: {
  login() {
    if(this.userName === 'admin' && this.password == '123') {
      this.$close().then(modal => {
        this.$emit('loginSuccess');
      })
    }
  }
}

/*
Method $close() in your modal and Vue.prototype.$modal.closeDisplayEl() could close current modal. The method mixins by default.

Both method will return a Promise, which delivers a $modal instance so you can do you work after a modal is closed.

You can also close your modal by using $emit('close'), but it won't send Promise Object.
*/

Call a toast and dialog

The default modal injected are toast and dialog. You can call them:

  • Vue.prototype.$modal.showDialog({
       confirm: Function, // the method invoked by 'confirm'
       cancel: Function // the method invoked by 'cancel'
    });
  • Vue.prototype.$modal.showToast({
      message: String
    });

The recommend is to create your general toast and dialog. Your modal could also named with 'toast' or 'dialog', and it won't conflict width default modal.

I will implement vue-modal-caller as soon. Please enjoy it.

Welcome your issue.