Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app" v-cloak>
  <div v-if="ready">
    <button @click="addCat" :disabled="addDisabled">Add Cat</button>
    <p/>
    <ul>
      <li v-for="cat in cats">
        {{cat.name}} is {{cat.age}} years old. <button @click="deleteCat(cat.id)">Delete</button>
      </li>
    </ul>
  </div>
</div>
              
            
!

CSS

              
                [v-cloak] {display: none}
              
            
!

JS

              
                Vue.config.productionTip = false;
Vue.config.devtools = false;

const DB_NAME = 'catdb';
const DB_VERSION = 1;

const app = new Vue({
  el:'#app',
  data: {
    db:null,
    ready:false,
    addDisabled:false,
    cats:[]
  },
  async created() {
    this.db = await this.getDb();
    this.cats = await this.getCatsFromDb();
    this.ready = true;
  },
  methods: {
    async addCat() {
      this.addDisabled = true;
      // random cat for now
      let cat = {
        name:"Cat" + Math.floor(Math.random() * 100),
        age:Math.floor(Math.random() * 10)+1
      };
      console.log('about to add '+JSON.stringify(cat));
      await this.addCatToDb(cat);
      this.cats = await this.getCatsFromDb();
      this.addDisabled = false;      
    },
    async deleteCat(id) {
      await this.deleteCatFromDb(id);
      this.cats = await this.getCatsFromDb();      
    },
    async addCatToDb(cat) {
      return new Promise((resolve, reject) => {

        let trans = this.db.transaction(['cats'],'readwrite');
        trans.oncomplete = e => {
          resolve();
        };

        let store = trans.objectStore('cats');
        store.add(cat);

      });
    },
    async deleteCatFromDb(id) {
      return new Promise((resolve, reject) => {
        let trans = this.db.transaction(['cats'],'readwrite');
        trans.oncomplete = e => {
          resolve();
        };

        let store = trans.objectStore('cats');
        store.delete(id);
      });
    },
    async getCatsFromDb() {
      return new Promise((resolve, reject) => {

        let trans = this.db.transaction(['cats'],'readonly');
        trans.oncomplete = e => {
          resolve(cats);
        };
        
        let store = trans.objectStore('cats');
        let cats = [];
        
        store.openCursor().onsuccess = e => {
          let cursor = e.target.result;
          if (cursor) {
            cats.push(cursor.value)
            cursor.continue();
          }
        };

      });
    },
    async getDb() {
      return new Promise((resolve, reject) => {

        let request = window.indexedDB.open(DB_NAME, DB_VERSION);
        
        request.onerror = e => {
          console.log('Error opening db', e);
          reject('Error');
        };
  
        request.onsuccess = e => {
          resolve(e.target.result);
        };
        
        request.onupgradeneeded = e => {
          console.log('onupgradeneeded');
          let db = e.target.result;
          let objectStore = db.createObjectStore("cats", { autoIncrement: true, keyPath:'id' });
        };
      });
    }
  }
})
              
            
!
999px

Console