浏览代码

Improve the refresh-options method

Dennis Hernández 10 年之前
父节点
当前提交
b9378582e1
共有 1 个文件被更改,包括 33 次插入0 次删除
  1. 33 0
      src/bootstrap-table.js

+ 33 - 0
src/bootstrap-table.js

@@ -107,6 +107,35 @@
         return defaultValue;
     };
 
+    var compareObjects = function (objectA, objectB, compareLength) {
+        // Create arrays of property names
+        var objectAProperties = Object.getOwnPropertyNames(objectA),
+            objectBProperties = Object.getOwnPropertyNames(objectB),
+            propName = '';
+
+        if (compareLength) {
+            // If number of properties is different, objects are not equivalent
+            if (objectAProperties.length != objectBProperties.length) {
+                return false;
+            }
+        }
+
+        for (var i = 0; i < objectAProperties.length; i++) {
+            propName = objectAProperties[i];
+
+            // If the property is not in the object B properties, continue with the next property
+            if ($.inArray(propName, objectBProperties) > -1) {
+                // If values of same property are not equal, objects are not equivalent
+                if (objectA[propName] !== objectB[propName]) {
+                    return false;
+                }
+            }
+        }
+
+        // If we made it this far, objects are considered equivalent
+        return true;
+    };
+
     var escapeHTML = function (text) {
         if (typeof text === 'string') {
             return text
@@ -2214,6 +2243,10 @@
     };
 
     BootstrapTable.prototype.refreshOptions = function (options) {
+        //If the objects are equivalent then avoid the call of destroy / init methods
+        if (compareObjects(this.options, options, false)) {
+            return;
+        }
         this.options = $.extend(this.options, options);
         this.trigger('refresh-options', this.options);
         this.destroy();