aboutsummaryrefslogtreecommitdiff
path: root/web.js
blob: 61b82d56c6c13b3d24a2fce562293acd3e6f65a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const web = new Vue({
  el: '#web',
  data: {
    word: '',
    timer: undefined,
    definitions: null,
    is_word: null
  },
  methods: {
    searchWord: function() {
      if(!this.word) return;
      if(this.timer) {
        clearTimeout(this.timer);
        this.timer = undefined;
      }
      console.log(this.word);
      fetch(`/def?w=${this.word}`)
        .then((response) => {
          this.is_word = response.ok;

          if( response.ok ) {
            response.arrayBuffer().then(res => {
              this.definitions = msgpack.decode(new Uint8Array(res));
              console.log(this.definitions);
            });
          }
      })
    }
  },
  watch: {
    word: function(w) {
      if( w === '' ) {
        this.is_word = null;
      }
      if(this.timer) {
        clearTimeout(this.timer);
        this.timer = undefined;
      }
      this.timer = setTimeout(this.searchWord, 800);
    }
  }
});