瀏覽代碼

修复pjax刷新页面后Dcat.init动态监听回调没有清除问题

jqh 4 年之前
父節點
當前提交
920074b867

+ 27 - 22
resources/assets/dcat/js/Dcat.js

@@ -105,32 +105,35 @@ export default class Dcat {
      * @param options
      */
     init(selector, callback, options) {
-        if (initialized[selector]) {
-            initialized[selector].takeRecords();
-            initialized[selector].disconnect();
-        }
+        let self = this,
+            clear = function () {
+                if (initialized[selector]) {
+                    initialized[selector].takeRecords();
+                    initialized[selector].disconnect();
+                }
+            };
 
-        let self = this;
+        self.onPjaxComplete(clear, true);
+        $document.one('pjax:responded', clear);
 
-        // 这里必须使用定时器,否则无法立即停止上次绑定的观察回调
-        setTimeout(function () {
-            initialized[selector] = $.initialize(selector, function () {
-                var $this = $(this);
-                if ($this.attr('initialized')) {
-                    return;
-                }
-                $this.attr('initialized', '1');
+        clear();
 
-                // 如果没有ID,则自动生成
-                var id = $this.attr('id');
-                if (! id) {
-                    id = "_"+self.helpers.random();
-                    $this.attr('id', id);
-                }
+        initialized[selector] = $.initialize(selector, function () {
+            var $this = $(this);
+            if ($this.attr('initialized')) {
+                return;
+            }
+            $this.attr('initialized', '1');
+
+            // 如果没有ID,则自动生成
+            var id = $this.attr('id');
+            if (! id) {
+                id = "_"+self.helpers.random();
+                $this.attr('id', id);
+            }
 
-                callback.call(this, $(this), id)
-            }, options);
-        }, 1)
+            callback.call(this, $(this), id)
+        }, options);
     }
 
     /**
@@ -154,6 +157,8 @@ export default class Dcat {
     pjaxResponded(value) {
         pjaxResponded = value !== false;
 
+        $document.trigger('pjax:responded');
+
         return this
     }
 

文件差異過大導致無法顯示
+ 0 - 0
resources/assets/dcat/plugins/jquery.initialize/jquery.initialize.min.js


文件差異過大導致無法顯示
+ 0 - 0
resources/dist/dcat/js/dcat-app.js


文件差異過大導致無法顯示
+ 0 - 0
resources/dist/dcat/js/dcat-app.js.map


文件差異過大導致無法顯示
+ 0 - 0
resources/dist/dcat/plugins/jquery.initialize/jquery.initialize.min.js


部分文件因文件數量過多而無法顯示