Forráskód Böngészése

Bugfix: nested components were initializing twice

Caleb Porzio 5 éve
szülő
commit
d4cba6574d
4 módosított fájl, 25 hozzáadás és 1 törlés
  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

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/alpine.js


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 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)
+})

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott