Integrating Vault with Vue.js

You can use dhtmlxVault in an application created with the Vue.js framework. Check the demo on Github.

Please note that the implementation provided below is not the only way to use dhtmlxVault in a Vue.js-based application. It gives you initial schema of integration and implies further extension of the app functionality depending on your goals.

Installation

The way of adding Vault sources into a Vue.js-based app makes a certain impact on its further reuse within the application. The most handy and least overburdening for an app variant is to install Vault through NPM.

If for some reason it doesn't suit you, you may choose between two more options: include Vault sources from a package, or make use of CDN.

Initialization

There are two possible scenarios of initializing Vault inside a Vue application. One consists in isolating Vault structure and data inside of the Vue component and another one suggests separating view and data parts with the possibility of interaction between them.

Scenario 1. Isolating Vault in a Vue component

In this variant Vault configuration and data are held inside of the Vie component with no bonds with the external part of the application.

Vault initialization

  • Create a Vault.vue file and add a container for Vault inside the <template></template> tags. Define the name of the container in the ref attribute:

Vault.vue

<template>
    <div ref="container" class="widget-box"></div>
</template>
  • Define the JS part of the Vue component and use the new Vault constructor to initialize Vault inside of the container that you've set above:

Vault.vue

<script>
export default {
    mounted:function(){
      this.vault = new Vault(this.$refs.container, {
        mode:"grid",
      });
    }
  }
</script>

Loading data and changing config

  • Next you can load data into the Vault and change its configuration, e.g. add a new control into the toolbar:

Vault.vue

<script>
export default {
    mounted:function(){
      this.vault = new Vault(this.$refs.container, {
        mode:"grid",
      });
      this.vault.data.parse([
        { name:"myfile_12.png", size: 32420 },
        { name:"myfile_13.png", size: 55674 },
        { name:"myfile_14.png", size: 12440 },
        { name:"info.doc", size: 243441 },
      ]);
      this.vault.toolbar.data.add({
        value:"MyAction"
      });
    }
  }
</script>

Scenario 2. Exposing Vault data and config

This variant adds flexibility in the control over Vault data and configuration by allowing access to them from other parts of the application.

Vault initialization

  • The first step is the same. Create a file, let it be Vault2.vue this time, and add a container for the Vault inside the <template></template> tags:

Vault2.vue

<template>
    <div ref="container" class="widget-box"></div>
</template>
  • Then initialize Vault with the new Vault constructor and define the configuration properties of Vault in the object passed as a second parameter of the constructor:

Vault2.vue

<script>
  export default {
    mounted:function(){
      /* globals dhx */
      this.vault = new Vault(this.$refs.container, {
        data: this.data,
        mode: this.mode,
        uploader:{
          autosend: this.autosend,
          target: this.target
        },
        toolbar: this.toolbar
      });
    }
  }
</script>

Working with configuration options

  • Set the list of used Vault configuration properties and their types in the props configuration option:

Vault2.vue

<script>
  export default {
    props: {
      target: String,
      mode: { type:String, default:"list" },
      autosend: Boolean,
      toolbar: Boolean,
      data: Object
    },
    mounted:function(){
      /* globals dhx */
       this.vault = new Vault(this.$refs.container, {
        data: this.data,
        mode: this.mode,
        uploader:{
          autosend: this.autosend,
          target: this.target
        },
        toolbar: this.toolbar
      });
    }
  }
</script>

The properties of Vault are exposed and available to work with outside the component. For example, you can:

  • Create another file and change the configuration of Vault from there. In the example below the "grid" mode of rendering a list of files is enabled::

BasicSample.vue

<template>
<div class='app-box'>
  <Vault toolbar mode="grid"></Vault>
</div>
</template>

Working with data

Work with data in this variant of using Vault in a Vue.js application can follow the MVVM pattern. Data collection represents Model and Vault represents View. Data collection and Vault are kept separately, and communicate with each other via particular links. Any changes made in the Model (Data collection) trigger corresponding changes in the View (Vault).

Usage of the MVVM pattern allows working with data outside Vault and all data-related manipulations can be moved to a separate file.

  • Create a DataSample.vue file and create a new data collection named files in it:

DataSample.vue

<script>
import Vault from './Vault.vue';
 
export default {
  components: {
    Vault
  },
  created:function(){
    this.files = new DataCollection();
  }
}
</script>
  • Make use of the v-bind directive of Vue.js to bind the created data collection to Vault:

DataSample.vue

<Vault v-bind:data="files" class='base-size'></Vault>

Due to data binding you can:

  • react to changes in the data

For example, by using the change event:

DataSample.vue

<script>
import Vault from './Vault.vue';
 
export default {
  components: {
    Vault
  },
  created:function(){
    this.files = new DataCollection();
    this.files.events.on("change", () => {
      this.count = this.files.getLength()
    });
  }
}
</script>
  • make changes in the data

For example, you can add files into the Vault and clear the list by clicking the bound buttons. In the example below buttons are subscribed to the click event via the v-on directive.

DataSample.vue

<template>
    <input type="button" v-on:click="add" value="Add a file">
    <input type="button" v-on:click="clear" value="Clear all">
</template>
 
<script>
import Vault from './Vault.vue';
 
export default {
  components: {
    Vault
  },
  created:function(){
    this.files = new DataCollection();
  },
  methods:{
    add:function(){ this.files.add({ name:"myfile.png", size:24560 }); },
    clear:function(){ this.files.removeAll() }
  }
}
</script>

A click on the "Add a file" button adds a file into the Vault and a click on the "Clear all" button cleans the list of files, as it's described in the methods configuration option.

Back to top