Facebook
From Cobalt Baboon, 3 Years ago, written in HTML5.
Embed
Download Paste or View Raw
Hits: 209
  1.  
  2.     <script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
  3. </head>
  4.  
  5.     <div id="main">
  6.         <button id="clearBtn">Clear</button>
  7.         <button id="updateRef">Update ref</button>
  8.     </div>
  9.  
  10.  
  11.     <script>
  12.         const { computed, shallowRef, watch, ref } = Vue;
  13.  
  14.         // A computed that for example exists for an entire lifecycle of an application
  15.         const someRefThatExistsLongerThanA = ref(true);
  16.  
  17.         class MyClass {
  18.           computedField = computed(() => {
  19.             return this.id + ": " + someRefThatExistsLongerThanA.value;
  20.           })
  21.  
  22.           constructor(id) {
  23.             this.id = id;
  24.            
  25.             // Just to make sure the dependencies are added
  26.             console.log(this.computedField.value);
  27.           }
  28.         }
  29.  
  30.         let aInstances = Array.from({ length: 10}, (x, i) => new MyClass(i));
  31.  
  32.        
  33.         console.log("Added new A instances")
  34.  
  35.         document.getElementById("clearBtn").onclick = () => {
  36.           // Allow cleaning up.
  37.           aInstances = [];
  38.           console.log("Cleared instances")
  39.         }
  40.  
  41.         document.getElementById("updateRef").onclick = () => {
  42.           someRefThatExistsLongerThanA.value = !someRefThatExistsLongerThanA.value;
  43.         }
  44.     </script>
  45. </body>
  46.  
  47. </html>