Zapisywanie via AJAX.
authorzuber <marek@stepniowski.com>
Mon, 21 Dec 2009 10:16:42 +0000 (11:16 +0100)
committerzuber <marek@stepniowski.com>
Mon, 21 Dec 2009 10:16:42 +0000 (11:16 +0100)
apps/wiki/forms.py
apps/wiki/views.py
platforma/static/js/main.js
platforma/templates/wiki/document_details.html

index e247487..836d8f0 100644 (file)
@@ -24,4 +24,5 @@ class DocumentForm(forms.Form):
         document = Document(self.get_storage(), name=self.cleaned_data['name'], text=self.cleaned_data['text'])
         storage.put(document, self.cleaned_data['author'], self.cleaned_data['comment'],
             self.cleaned_data['revision'])
+        return storage.get(self.cleaned_data['name'])
 
index 33d6610..b242802 100644 (file)
@@ -1,5 +1,6 @@
 from django.views.generic.simple import direct_to_template
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponse
+from django.utils import simplejson as json
 
 from wiki.models import storage, Document, DocumentNotFound
 from wiki.forms import DocumentForm
@@ -16,14 +17,15 @@ def document_detail(request, name, template_name='wiki/document_details.html'):
         document = storage.get(name)
     except DocumentNotFound:
         document = Document(storage, name=name, text='')
-        
+    
+
     if request.method == 'POST':
         form = DocumentForm(request.POST, instance=document)
         if form.is_valid():
-            form.save()
-            return HttpResponse('OK')
+            document = form.save()
+            return HttpResponse(json.dumps({'text': document.text, 'revision': document.revision()}))
         else:
-            print form.errors
+            return HttpResponse(json.dumps({'errors': form.errors}))
     else:
         form = DocumentForm(instance=document)
     
index cec5e22..75e3746 100644 (file)
@@ -379,12 +379,6 @@ $(function() {
             $('#save-button').click(function(event) {
                 event.preventDefault();
                 $.blockUI({message: $('#save-dialog')});
-                
-                // console.log(editor.getCode(), $('form input[name=text]').get(0));
-                //                 $('form textarea[name=text]').val(editor.getCode());
-                //                 $('form').ajaxSubmit(function() {
-                //                     alert('Udało się!');
-                //                 });
             });
             
             $('#save-ok').click(function() {
@@ -403,9 +397,16 @@ $(function() {
                 $.ajax({
                     url: document.location.href,
                     type: "POST",
-                    dataType: "html",
+                    dataType: "json",
                     data: data,                
-                    success: function() {
+                    success: function(data) {
+                        if (data.text) {
+                            editor.setCode(data.text);
+                            $('#document-revision').html(data.revision);
+                        } else {
+                            console.log(data.errors);
+                            alert(data.errors);
+                        }
                         $.unblockUI();
                     },
                     error: function(xhr, textStatus, errorThrown) {
@@ -413,6 +414,10 @@ $(function() {
                     },
                 })
             });
+            
+            $('#save-cancel').click(function() {
+                $.unblockUI();
+            });
 
             $('#simple-view-tab').click(function() {
                 if ($(this).hasClass('active')) {
index 2c8030e..e5c9d0c 100644 (file)
@@ -55,8 +55,8 @@
             </div>
         </div>
         <div id="save-dialog" style="display: none">
-            <p>Zapisywanie</p>
             <label for="komentarz">Komentarz</label><input type="text" name="komentarz" value="" id="komentarz" />
-            <button id="save-ok">Ok</button>
+            <button id="save-ok">Zapisz</button>
+            <button id="save-cancel">Anuluj</button>
         </div>
 {% endblock %}
\ No newline at end of file