From: zuber Date: Mon, 21 Dec 2009 10:16:42 +0000 (+0100) Subject: Zapisywanie via AJAX. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/f734261e68dc171da98aabe700e170c3ede3b6a5 Zapisywanie via AJAX. --- diff --git a/apps/wiki/forms.py b/apps/wiki/forms.py index e2474874..836d8f0d 100644 --- a/apps/wiki/forms.py +++ b/apps/wiki/forms.py @@ -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']) diff --git a/apps/wiki/views.py b/apps/wiki/views.py index 33d6610b..b242802b 100644 --- a/apps/wiki/views.py +++ b/apps/wiki/views.py @@ -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) diff --git a/platforma/static/js/main.js b/platforma/static/js/main.js index cec5e229..75e3746f 100644 --- a/platforma/static/js/main.js +++ b/platforma/static/js/main.js @@ -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')) { diff --git a/platforma/templates/wiki/document_details.html b/platforma/templates/wiki/document_details.html index 2c8030e3..e5c9d0c0 100644 --- a/platforma/templates/wiki/document_details.html +++ b/platforma/templates/wiki/document_details.html @@ -55,8 +55,8 @@ {% endblock %} \ No newline at end of file