浏览代码

Refactorisation chargement données

Loquicom 5 年之前
父节点
当前提交
28397f4ae6
共有 2 个文件被更改,包括 24 次插入18 次删除
  1. 1 1
      src/data/app.json
  2. 23 17
      src/index.html

+ 1 - 1
src/data/app.json

@@ -1,5 +1,5 @@
 {
     "id": "app",
     "title": "Application",
-    "content": "#### blabla...\n```javaSystem.out.println(\"Hello world\");```"
+    "content": "#### blabla...\n```java\nSystem.out.println(\"Hello world\");\n```"
 }

+ 23 - 17
src/index.html

@@ -140,28 +140,34 @@
                 toArray = toArray ? toArray : false;
                 const data = toArray ? [] : {};
                 return new Promise(resolve => {
-                    // Load from local storage
+                    // Data already load
                     if (localStorage.list) {
-                        const list = JSON.parse(localStorage.list)
+                        const list = JSON.parse(localStorage.list);
                         for (element of list) {
-                            toArray ? data.push(localStorage[element]) : data[element] = localStorage[element];
+                            console.log(element);
+                            toArray ? data.push(JSON.parse(localStorage.getItem(element))) : data[element] = JSON.parse(localStorage.getItem(element));
                         }
-                    } else {
-                        localStorage.setItem('list', JSON.stringify([]));
-                    }
-                    // Load from disk
-                    $.getJSON('data/list.json', (list) => {
-                        const total = list.length;
-                        let count = 0;
-                        list.forEach(element => {
-                            $.getJSON(`data/${element}.json`, (result) => {
-                                toArray ? data.push(result) : data[result.id] = result;
-                                if (++count === total) {
-                                    resolve(data);
-                                }
+                        resolve(data);
+                    } 
+                    // Load data on server
+                    else {
+                        const list = [];
+                        $.getJSON('data/list.json', (file) => {
+                            const total = file.length;
+                            let count = 0;
+                            file.forEach(element => {
+                                $.getJSON(`data/${element}.json`, (result) => {
+                                    toArray ? data.push(result) : data[result.id] = result;
+                                    list.push(result.id);
+                                    localStorage.setItem(result.id, JSON.stringify(result));
+                                    if (++count === total) {
+                                        localStorage.setItem('list', JSON.stringify(list));
+                                        resolve(data);
+                                    }
+                                });
                             });
                         });
-                    });
+                    }
                 });
             }