On Dec 4, 2009, at 1:34 PM, Philippe Clérié wrote:

> Are you using a library or did you code it yourself? What would be a good 
> library to start with?
> 
> -- 
> 
> 
> Philippe
> 

I use jQuery, and that makes it simple.

First, have a view that returns JSON. Basically, this is all you need in your 
view:

#where return_data is a dictionary 
return HttpResponse(simplejson.dumps(return_data), mimetype="application/json")

Then, you'd have JavaScript like this:

#a GET version:
        

        //Note: The variable name 'data,' supplied in the anonymous
        //function definition below will receive the JSON sent by
        //the view. If you have a key in your returned dictionary
        //named 'balance,' you can refer to it with data.balance.

        $.get("/balance_json/{{ch.slug}}/", {}, function (data){
                $('#current_balance').html("$" + data.balance);
        }, 'json');

#a POST version

        //Same as above -- the returned dictionary (serialized to JSON)
        //contains a key 'success' or 'fail.' In this case, the variable is 
        //named resp instead of data. Blame my predecessor for the 
inconsistency.

        $.post("/patient/{{ch.slug}}/appointment/", data, function(resp){
            if(resp.success) {
                $("#messages").html("<div id='success-message' 
class='success'>" + resp.success + "</div>");
            } else if (resp.fail) {
                $("#messages").html("<div id='fail-message' class='fail'>" + 
resp.fail + "</div>");
            }
        }, "json");



Also, if you've never used jQuery before, you should have a JavaScript section 
like this:

$(document).ready(function(){

        //put your code here -- this is the far superior replacement
        //for putting stuff in an 'onLoad' section.

}

One more thing. Since you want to do do this repeatedly, you should use a 
setTimeout() call at the end of your function so it happens again and again.
You may put it within an 'if' check to see whether the JSON returned a value 
indicating that the process is complete.



Like this:   setTimeout('your_function_name_here()', 5000) //this is a 
five-second delay


Shawn










--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to