Explorar o código

Use cookies to move app code to main.js

Matthias Vogelgesang %!s(int64=7) %!d(string=hai) anos
pai
achega
4cb79bd940
Modificáronse 5 ficheiros con 48 adicións e 34 borrados
  1. 2 1
      bower.json
  2. 36 0
      nova/static/js/main.js
  3. 0 32
      nova/templates/base/search.html
  4. 7 0
      nova/templates/layout.html
  5. 3 1
      nova/views.py

+ 2 - 1
bower.json

@@ -16,6 +16,7 @@
     "jquery": "^1.11.3",
     "lodash": "^4.13.1",
     "vue": "^2.1.10",
-    "vue-resource": "^1.0.3"
+    "vue-resource": "^1.0.3",
+    "system.js": "^0.20.1"
   }
 }

+ 36 - 0
nova/static/js/main.js

@@ -0,0 +1,36 @@
+function readCookie(a) {
+    var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
+    return b ? b.pop() : '';
+}
+
+var app = new Vue({
+  el: '#search',
+  data: {
+    query: '',
+    message: '',
+    items: [],
+    token: readCookie('token'),
+  },
+  watch: {
+    query: function (data) {
+      this.searchQuery (data)
+    }
+  },
+  methods: {
+    searchQuery: _.debounce(
+      function (query) {
+        var params = {
+          q: query,
+          token: this.token,
+        }
+
+        this.$http.get('/api/search', {params: params}).then((response) => {
+          return response.json();
+        }).then((items) => {
+          this.items = items
+        });
+      },
+      250
+    )
+  }
+})

+ 0 - 32
nova/templates/base/search.html

@@ -49,36 +49,4 @@
 <script src="{{ url_for('static', filename='vue/dist/vue.min.js') }}"></script>
 <script src="{{ url_for('static', filename='vue-resource/dist/vue-resource.min.js') }}"></script>
 <script src="{{ url_for('static', filename='lodash/dist/lodash.min.js') }}"></script>
-<script>
-  var app = new Vue({
-    el: '#search',
-    data: {
-      query: '',
-      message: '',
-      items: []
-    },
-    watch: {
-      query: function (data) {
-        this.searchQuery (data)
-      }
-    },
-    methods: {
-      searchQuery: _.debounce(
-        function (query) {
-          var params = {
-            q: query,
-            token: '{{ current_user.token }}'
-          }
-
-          this.$http.get('/api/search', {params: params}).then((response) => {
-            return response.json();
-          }).then((items) => {
-            this.items = items
-          });
-        },
-        250
-      )
-    }
-  })
-</script>
 {% endblock %}

+ 7 - 0
nova/templates/layout.html

@@ -51,5 +51,12 @@
 
     <script src="{{ url_for('static', filename='jquery/dist/jquery.min.js') }}"></script>
     <script src="{{ url_for('static', filename='bootswatch-dist/js/bootstrap.min.js') }}"></script>
+    <script src="{{ url_for('static', filename='system.js/dist/system.js') }}"></script>
+    <script>
+      System.config({
+        baseURL: '{{ url_for("static", filename='js') }}',
+      });
+      System.import('main.js')
+    </script>
   </body>
 </html>

+ 3 - 1
nova/views.py

@@ -106,7 +106,9 @@ def login():
             login_user(user)
 
             flash('Logged in successfully')
-            return redirect(url_for('index'))
+            response = app.make_response(redirect(url_for('index')))
+            response.set_cookie('token', user.token)
+            return response
         else:
             return render_template('user/login.html', form=form, failed=True), 401