Changes between Version 16 and Version 17 of AJAX/Dojo/RefactoredFormSubmit
- Timestamp:
- Feb 16, 2007, 7:42:35 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
AJAX/Dojo/RefactoredFormSubmit
v16 v17 2 2 === Introduction === 3 3 4 Here's a simple recipe that worked for me. It is quite inspired by the [wiki:AjaxDojoFormSub] example―and from [http://www.b-list.org/weblog/2006/07/31/django-tips-simple-ajax-example-part-1 this tutorial]―except that this recipe is expected to use python-cjson for receiving JSONdata.4 Here's a simple recipe that worked for me. It is quite inspired by the [wiki:AjaxDojoFormSub] example―and from [http://www.b-list.org/weblog/2006/07/31/django-tips-simple-ajax-example-part-1 this tutorial]―except that this recipe is using python-cjson to receive JSON-encoded data. 5 5 6 My initial objective was to display validation errors without doing a page-refresh, by using JSON for carrying errors (or whatever else you could possibly imagine) back to the client browser. 6 My initial objective was to display validation errors without doing a page-refresh, while using 7 JSON for carrying errors (or whatever else you could possibly imagine) back to the client browser. 7 8 8 9 === The server view === … … 12 13 13 14 {{{ 15 #!python 14 16 import cjson 15 17 from django.http import HttpResponse … … 25 27 return HttpResponse(cjson.encode(form.errors), mimetype='text/javascript') 26 28 else: 27 # vanilla Http response fallback28 return HttpResponse(t.render(Context( {'errors' : form.errors }))29 # No JSON here! 30 return HttpResponse(t.render(Context(form.errors)) 29 31 else: 30 32 # Do something when the form has been validated. … … 34 36 === The client view === 35 37 36 Now that the server view is writtent, we could try next to arrange the client code to fit into one, unportable template chunk, however it's37 usually best to split the work in smaller pieces, at least for being nice to the maintainers (You). :-)38 Once the server view has been written, the idea is to write down the client UI (or view, 39 depending whether you're a purist or not.. ;-) 38 40 39 So let's move on in writing our own Dojo template, where we will make an abstraction of all our applied Dojo concepts. 40 41 Quite frankly, that part is going straight-forward, as we're only writing HTML: 41 Quite frankly, that step is straight-forward, and looks pretty-much like writing down 42 basic HTML: 42 43 {{{ 43 44 #!text/html 44 45 <script type="text/javascript"> 45 46 djConfig = { 46 isDebug: true,47 debugAtAllCosts: false47 isDebug: true, 48 debugAtAllCosts: false 48 49 }; 49 50 </script> … … 54 55 dojo.require("dojo.json"); // serialization to JSON 55 56 </script> 57 <!-- Here's the client UI script to play with Dojo --> 56 58 <script type="text/javascript" src="/media/js/ajax.js"></script> 57 59 }}} … … 61 63 or something else. 62 64 63 Optionally, you can "bake" your own Dojo file, and substract the baked modules 64 from the list of required modules above, so we wont load the already compiled 65 Dojo modules twice. 65 To avoid loading the Dojo modules twice (for speed), just roll your own Dojo script, 66 and then remove the implied ("baked") modules from the list of required modules above. 66 67 67 === Form validating w /dojo.io.bind ===68 === Form validating with dojo.io.bind === 68 69 69 70 Let's complete our application by writing down the core JS file, 70 71 for doing some-cute-AJAX-related-things-with-Dojo: 71 72 73 ajax.js: 72 74 {{{ 73 function validate() { 74 var fNode = dojo.byId('xForm') 75 function myinit() { 76 // connect the event with the good handler 77 var myButton = dojo.byId("xBtn1"); 78 dojo.event.connect(myButton, "onclick", "sendForm"); 79 }; 80 function sendForm() { 81 var fNode = dojo.byId('xForm') 75 82 var fParms = dojo.io.encodeForm(fNode) 83 var myErrorDiv = dojo.byId('errorBox1') 76 84 var bindArgs = { 77 85 url: ".", 78 mimetype: 'text/ json',86 mimetype: 'text/plain', 79 87 method: "post", 80 88 preventCache: true, 81 89 transport: "XMLHTTPTransport", 82 90 postContent: fParms, 83 error: function(type, error ){84 // Handle error s for the XMLHTTPTransport here.91 error: function(type, error, http){ 92 // Handle error here 85 93 alert(error.message) 94 if (http.responseText) 95 // Return the raw server response 96 // (useful when debugging) 97 document.write(http.responseText) 86 98 return false 87 99 }, 88 load: function(type, data, evt){ 89 // handle successful response here 100 load: function(type, data, http, kwArgs){ 101 // Handle "successful" responses here 102 var errStr = '' 90 103 if(type == 'load'){ 91 var myDiv = dojo.byId('errorBox1')92 // XXX Not sure what to do if the data is JSON or93 // something else..94 var json = dojo.json.evalJson(data)95 if(json){96 errStr = ''97 for(x in json){98 errStr += x + ': ' + json[x] + '<br />'99 }100 myDiv.innerHTML = errStr101 }else{102 myDiv.innerHTML = "hello, world!"103 }}104 }}105 var xmlhttp = dojo.io.bind(bindArgs)106 return xmlhttp 107 } 104 try { 105 var json = eval( '(' + data + ')' ) 106 } catch (e) { 107 // not json 108 document.write(data) 109 return false 110 } 111 for (x in json) 112 errStr += x + ': ' + json[x] + '<br />' 113 myErrorDiv.innerHTML = errStr 114 } 115 } 116 }; 117 var xmlhttp = dojo.io.bind(bindArgs) 118 return xmlhttp 119 }; 120 dojo.addOnLoad(myinit); 108 121 }}} 109 122