Browse Source

fix ui breaking on update

close violentmonkey/violentmonkey#68
Gerald 9 years ago
parent
commit
a72a4be42b
2 changed files with 25 additions and 22 deletions
  1. 1 0
      .gitignore
  2. 24 22
      src/options/app.js

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@ node_modules/
 *.nex
 *.crx
 *.log
+*.lock

+ 24 - 22
src/options/app.js

@@ -14,31 +14,33 @@ function initMain() {
     utils.features.reset('sync');
   });
   var port = chrome.runtime.connect({name: 'Options'});
-  port.onMessage.addListener(function (res) {
-    switch (res.cmd) {
-    case 'sync':
-      store.sync = res.data;
-      break;
-    case 'add':
-      res.data.message = '';
-      store.scripts.push(res.data);
-      break;
-    case 'update':
-      if (res.data) {
-        var script = store.scripts.find(function (script) {
-          return script.id === res.data.id;
-        });
-        script && Object.keys(script).forEach(function (key) {
-          Vue.set(script, key, res.data[key]);
-        });
-      }
-      break;
-    case 'del':
+  var handlers = {
+    sync: function (data) {
+      store.sync = data;
+    },
+    add: function (data) {
+      data.message = '';
+      store.scripts.push(data);
+    },
+    update: function (data) {
+      if (!data) return;
+      var script = store.scripts.find(function (script) {
+        return script.id === data.id;
+      });
+      script && Object.keys(data).forEach(function (key) {
+        Vue.set(script, key, data[key]);
+      });
+    },
+    del: function (data) {
       var i = store.scripts.findIndex(function (script) {
-        return script.id === res.data;
+        return script.id === data;
       });
       ~i && store.scripts.splice(i, 1);
-    }
+    },
+  };
+  port.onMessage.addListener(function (res) {
+    var handle = handlers[res.cmd];
+    handle && handle(res.data);
   });
 }
 function loadHash() {