Hallo Guys,
I had written for you the AJAX chapter. As attachment in this mail!


Tamer
How to write AJAX Application in bluebream

first of all guys, I'll explain you the basics of an AJAX application in 
bluebream. What does ajax stand for ?! It stands for "Asynchronous Javascript 
and XML". 

In an Ajax application the requests are being done in the background. Therefore 
after loading a certain website, and accomplishing a certain task the whole 
thing is done in the background. 

The Javascript Application that had been delivered with the website on your 
computer does it all for you. 

Now enough. I'll explain now how the AJAX Page is written. 

Requirements:
Dojo Library (the whole toolkit is for this little course not required):

http://www.dojotoolkit.org/download/


Step 1:
we create the view in HTML, and the JS Routine, where the XHR Request should be 
made of. This is also our main template in which will appear, after the site 
had been loaded, and save it under the filename: "index.pt" (all files of 
course are saved in the projectsfolder ? src/projectname/):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Out beautifull site.....</title>
</head>
<script type="text/javascript" src="@@/dojo.js"></script>
<script language="JavaScript">  
        function FireRequest(option)
        {
                var tagetNode = dojo.byId("contentID");
                var xhrArgs = 
{url:'http://127.0.0.1:8090/xhr',handleAS:'text',load: function(data)
                {
                        tagetNode.innerHTML = data;
                },error: function(error){
                        tagetNode.innerHTML = 'error!!! Could not make the 
request: <br>' + error;                      
                }};
                
                var deferred = dojo.xhrGet(xhrArgs);                            
                                
        }       
        //dojo.addOnLoad(FireRequest);  
</script>
<body>
        <div id="contentID" style="width:150px;">       
        </div>
        
        <div id="buttons">
                <button onclick="FireRequest(null);">Hit me!</button>
        </div>
</body>
</html>



Line 7: integrates the dojo library in the view. 
Line 9-20 : is a simple function, which makes our AJAX Request. Now in general 
explained:

The DOJO XHR function has a paramater which defines the options, and it looks 
like this:

var xarg = {url:'http://....',load: function(data){...routine....},error: 
function(error){...errorroutine...}}

dojo.xhrGet(xarg);


General: function(data) and function(error), the parameters are provided by the 
dojo functions. You can consider thinking of, a try (for success) and catch 
(error) routine in replacement.

Line 12: the xhr parameter es described about

function(data): Is the routine, when a successfully request had been done. In 
this routine, you decide what with the data should happen. 

Line 13-15: The request was successful and "function(data)" es being executed. 
In this sample we copy the content in our div element.

function(error): Is the routine when the request couldn't be done. Example: the 
url doesn't exist, is not reachable. 

Line 15-17: 
There was no success, we decided to have a standard error message + the error 
message itself (which we could leave out). 


Line 19:
performs the Ajax request!


In both both cases, you can decide what will happen. Other functions are being 
executed, and you provide somehow the return. 

Professional developers, encapsulate the return with JSON, and decode it with 
Javascript once again, to perform certain decisions. 





Step 2:
making the entries in the "configuration.zcml":

Create a file ressource for our "dojo.js" file, which has in the view the 
following entry in the head section (Line 7)



<browser:resource
    name="dojo.js"
    file="dojo.js" />

and 2 browser page entry. One for the class and one for the view. 

 The class, with the respond has nothing to do with the template. Therefor, 
they registered 2x time in "configuration.zcml", because the template has a 
different name, and hasn't got anything todo with our class eother, both have 
different names respectively. 

<browser:page
    name="start"
    for="*"
    template="index.pt"
    permission="zope.Public" />
    
  <browser:page
    name="xhr"
    for="zope.site.interfaces.IRootFolder"
    class=".XHR.XHR"
    permission="zope.Public" />



Step 3:
We create one template and one class for our simple application. The template 
is for our view, which we already displayed at Step 1. 

The class XHR.py looks like this:

from zope.publisher.browser import BrowserView

class XHR(BrowserView):
    
    def __call__(self):
        return "This is our XHR Response"
        


Step 4 (last words):
After creating our file ressource (hopefully you downloaded the dojo.js 
library: , the view and the class) and registering, you should have a small 
ajax application. 

These magical things could be done with "namedtemplates", classes with 
templates, and all things you love.

Be continued, with more samples, creating complete different contents (choosen 
by the Get and post variables). 
_______________________________________________
bluebream mailing list
[email protected]
https://mail.zope.org/mailman/listinfo/bluebream

Reply via email to