jsonp support
authorMarcin Koziej <marcin@lolownia.org>
Thu, 17 May 2012 09:52:20 +0000 (11:52 +0200)
committerMarcin Koziej <marcin@lolownia.org>
Thu, 17 May 2012 09:52:20 +0000 (11:52 +0200)
apps/search/views.py
apps/wolnelektury_core/static/js/search.js

index 56eef09..881815d 100644 (file)
@@ -18,6 +18,7 @@ from suggest.forms import PublishingSuggestForm
 from time import sleep
 import re
 import enchant
+import json
 
 dictionary = enchant.Dict('pl_PL')
 
@@ -108,17 +109,23 @@ def hint(request):
             c = c[len('pd_'):]
         return _(c)
 
-    return JSONResponse(
-        [{'label': t.name,
-          'category': category_name(t.category),
-          'id': t.id,
-          'url': t.get_absolute_url()}
-          for t in tags] + \
-          [{'label': b.title,
-            'category': _('book'),
-            'id': b.id,
-            'url': b.get_absolute_url()}
-            for b in books])
+    callback = request.GET.get('callback', None)
+    data = [{'label': t.name,
+              'category': category_name(t.category),
+              'id': t.id,
+              'url': t.get_absolute_url()}
+              for t in tags] + \
+              [{'label': b.title,
+                'category': _('book'),
+                'id': b.id,
+                'url': b.get_absolute_url()}
+                for b in books]
+    if callback:
+        return HttpResponse("%s(%s);" % (callback, json.dumps(data)),
+                            content_type="application/json; charset=utf-8")
+    else:
+        return JSONResponse(data)
+            
 
 
 def main(request):
index 293b9e3..3b6ea40 100644 (file)
@@ -7,6 +7,7 @@ var __bind = function (self, fn) {
     $.widget("wl.search", {
        options: {
           minLength: 0,
+         dataType: "json",
         },
 
        _create: function() {
@@ -15,6 +16,7 @@ var __bind = function (self, fn) {
                select: __bind(this, this.enter),
                focus: function() { return false; },
                source: this.element.data('source'),
+               dataType: options.dataType,
            };
 
            this.element.autocomplete($.extend(opts, this.options))