Przeglądaj źródła

Bugfix: nested components were initializing twice

Caleb Porzio 5 lat temu
rodzic
commit
d4cba6574d
4 zmienionych plików z 25 dodań i 1 usunięć
  1. 0 0
      dist/alpine.js
  2. 0 0
      dist/alpine.js.map
  3. 3 1
      src/index.js
  4. 22 0
      test/constructor.spec.js

Plik diff jest za duży
+ 0 - 0
dist/alpine.js


Plik diff jest za duży
+ 0 - 0
dist/alpine.js.map


+ 3 - 1
src/index.js

@@ -74,7 +74,9 @@ const Alpine = {
     },
 
     initializeComponent: function (el) {
-        el.__x = new Component(el)
+        if (! el.__x) {
+            el.__x = new Component(el)
+        }
     }
 }
 

+ 22 - 0
test/constructor.spec.js

@@ -232,3 +232,25 @@ test('auto-detect x-data property changes at run-time', async () => {
 
     await wait(() => { expect(document.querySelector('span').innerText).toEqual(1) })
 })
+
+test('nested components only get registered once on initialization', async () => {
+    global.MutationObserver = class {
+        constructor(callback) {}
+        observe() {}
+    }
+
+    var initCount = 0
+    window.registerInit = function () {
+        initCount = initCount + 1
+    }
+
+    document.body.innerHTML = `
+        <div x-data x-init="registerInit()">
+            <div x-data x-init="registerInit()"></div>
+        </div>
+    `
+
+    Alpine.start()
+
+    expect(initCount).toEqual(2)
+})

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików