{% extends "base.html" %} {% block javascript%} function reload_content() { $("#content").load("{% url game-main-content %}"); } function buy_map(id) { url = "{% url game-buy-map%}"; data = {"id":id}; $.getJSON(url, data, function() { reload_content(); } ); } function buy(id) { return transaction(id,true); } function sell(id) { return transaction(id,false); } // Buy gets called to open a purchase dialog function transaction(id, buy) { // One humongous function call... // Request the contents of the purchase dialog if (buy) url = "{% url game-buy %}"; else url = "{% url game-sell %}"; $.getJSON(url+"?id="+id, function (data) // Deal with the response { // data is now a dictionary containing the html for the dialog // and some additional parameters to ensure consistency of the offer // during the process. dom = $(data["html"]); dom.dialog(); // creates a dialog from the html data form = dom.find("form"); form.find("#submit").click(function() { // this function gets called whenever the "ok" button is clicked number = form.find("#number").val(); max_number = data["max_number"]; valid = true; // validate number if (number > max_number) { form.find("#number").val(max_number); valid = false; } if (valid) { // the params are straight from the response data. But some time may have passed // since the request. Conditions may have changed, and the server must know // the conditions the user thinks he acts on to verify them. params = {"id":data["id"],"number": number, "confirm":"yes", "price":data["price"]}; // perform the transaction $.getJSON(url,params, function(data) { // Deal with response. if (data["success"]==1) { dom.dialog("close"); reload_content(); //reloads only content div } else {dom.html("Sorry, offer expired meanwhile!")} } ); } return false; }); } ); } function travel_to(id) { $.getJSON("{% url game-travel-to%}", {"id":id}, function(){ reload_content(); }); } {% endblock %} {% block content %} {% include "game/main_content.html"%} {% endblock%}