Johann Woelper 7 سال پیش
والد
کامیت
fac1647429
3فایلهای تغییر یافته به همراه59 افزوده شده و 60 حذف شده
  1. 7 7
      src/main.rs
  2. 29 0
      static/adapter.js
  3. 23 53
      static/index.html

+ 7 - 7
src/main.rs

@@ -41,15 +41,15 @@ fn set(obj: String) -> String {
     let json_result = json::parse(obj.to_owned().as_str());
     match json_result {
         Ok(j) => {
-            println!("{:?}", j)
+            println!("JSON DECODED {:?}", j);
+            println!("GOT {:?}", obj);
+            let mut data = OBJECT.lock().unwrap();
+            *data = obj;
+            String::from("ok")
         },
-        Err(e) => println!("Invalid Json {:?}", e)
+        Err(_e) => String::from("Invalid Json")
     }
 
-    println!("GOT {:?}", obj);
-    let mut data = OBJECT.lock().unwrap();
-    *data = obj;
-    String::from("ok")
 }
 
 // Static file serving
@@ -72,7 +72,7 @@ fn disk_committer() {
                     let result: &String = &*obj;
                     let mut buffer = File::create("db").unwrap();
                     buffer.write(result.as_bytes()).unwrap();
-                    println!("Commiting to disk");
+                    // println!("Committing to disk");
                 },
                 _ => ()
             }

+ 29 - 0
static/adapter.js

@@ -0,0 +1,29 @@
+var adapter = {
+    server_url: "",
+    responseJson: {},
+    onset: function() {},
+    onget: function() {},
+    setter: function () {
+        xhr = new XMLHttpRequest();
+        xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(this.onset())));
+        xhr.onload = function () {
+            console.log(xhr.responseText);
+        };
+        xhr.send();
+    },
+    getter: function () {
+        var self = this;
+        var xhr = new XMLHttpRequest();
+        xhr.open('GET', this.server_url + '/get');
+        xhr.onload = function () {
+            try {
+                var response = JSON.parse(this.responseText);
+                self.responseJson = response;
+                self.onget();
+            } catch (err) {
+                console.warn("json could not be parsed:", err, this.responseText);
+            }
+        };
+        xhr.send();
+    },
+};

+ 23 - 53
static/index.html

@@ -12,27 +12,22 @@
         background-color: aquamarine;
         padding: 2em;
     }
-
-    .descriptor {
+    .title {
         display: inline-block;
-        width: 5em;
-    }
-
-    .log {
-        font-size: 0.8em;
-        line-height: 1em;
-        background-color: aliceblue;
-        padding: 1em;
-    }
-
-    .json-tree-value-number {
-        color: red;
+        width: 6em;
     }
 </style>
 
+
 <div id="app">
-    <button onclick="store.setter(app.config)">save</button>
-    <button onclick="store.getter()">load</button>
+    <button onclick="adapter.setter()">save</button>
+    <button onclick="adapter.getter()">load</button>
+    <template v-for="(value, key) in config">
+        <br>
+
+       <div class="title">{{key}}</div><input v-model="config[key]">
+
+    </template>
     <pre>
         {{config}}
     </pre>
@@ -40,50 +35,27 @@
 
 
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
-<script>
-    var app = '';
-
-    var store = {
-        server_url: "http://localhost:8000",
-        target: app.config,
-        setter: function (o) {
-            xhr = new XMLHttpRequest();
-            xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(o)));
-            xhr.onload = function () {
-                console.log(xhr.responseText);
-            };
-            xhr.send();
-        },
-        getter: function () {
-            xhr = new XMLHttpRequest();
-            xhr.open('GET', this.server_url + '/get');
-            xhr.onload = function () {
-                var decodedResponse = this.responseText;
-                console.log(decodedResponse);
-                try {
-                    var response = JSON.parse(decodedResponse);
-                    app.config = response;
-                } catch (err) {
-                    console.warn("json could not be parsed:", this.responseText);
-                }
-                // return response;
-            };
-            xhr.send();
-        },
-    };
+<script src="adapter.js"></script>
 
+<script>
 
     var app = new Vue({
         el: '#app',
         data: {
             config: {
-                num_lives: 42,
-                levels: ["Fluff"],
+                age: 42,
+                attributes: ["Fluff", "yo"],
             },
         },
         mounted: function () {
-            // store.object = this.config;
-            store.getter();
+            adapter.onget = function () {
+                app.config = adapter.responseJson
+            }
+            adapter.onset = function () {
+                return app.config;
+            }
+
+            // adapter.getter();
         },
         watch: {},
         computed: {
@@ -93,5 +65,3 @@
 </script>
 <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
 
-
-<!-- CSS ================================= -->