Переглянути джерело

Merge pull request #33 from jameslkingsley/turbolinks-support

Add Turbolinks support
Ryan Chandler 5 роки тому
батько
коміт
e0f81e3c0f
1 змінених файлів з 11 додано та 5 видалено
  1. 11 5
      src/index.js

+ 11 - 5
src/index.js

@@ -17,11 +17,10 @@ const Spruce = {
         await domReady()
 
         this.emit('init')
+        
+        this.attach()
 
-        document.querySelectorAll('[x-subscribe]').forEach(el => {
-            el.setAttribute('x-init', buildInitExpression(el))
-            el.removeAttribute('x-subscribe')
-        })
+        document.addEventListener('turbolinks:render', this.attach)
 
         this.stores = createObservable(this.stores, {
             set: (target, key, value, oldValue) => {
@@ -42,6 +41,13 @@ const Spruce = {
         }
     },
 
+    attach() {
+        document.querySelectorAll('[x-subscribe]').forEach(el => {
+            el.setAttribute('x-init', buildInitExpression(el))
+            el.removeAttribute('x-subscribe')
+        })
+    },
+
     store: function (name, state) {
         if (! this.stores[name]) {
             this.stores[name] = state
@@ -102,4 +108,4 @@ window.deferLoadingAlpine = function (callback) {
     deferrer(callback)
 }
 
-export default Spruce
+export default Spruce