The fastest easiest way to get it right.

Returning Data with JSON

Andromeda allows you to send raw data back to the browser which can then be put to any purpose.

The example below shows a routine that requests some data from the server, and takes the resulting data and renders an HTML Table.

class x6jsonexample extends androX6 {
    function x6main() {
        # Conventional HTML generation...
        $top = html('div');
        $top->h('h1','Hello JSON/DATA');
        $input = $top->h('input','Fetch Data');
        $input->hp['type'] = 'button';
        $input->code['click'] = <<<JS
        function(e) {
            if(x6.json.execute()) {
                html = '';
                // The data that came back can be found
                // in "" plus the name provided by
                // the servers-side routine.
                for(var y in {
                    for(var x in[y]) {
'[y][x]; } } html+="
"; $('#put_table_here').html(html); } } JS; $div = $top->h('div'); $div->hp['id'] = 'put_table_here'; $top->render(); } # This routine handles the call function gimmeData() { $data = array( array('a','b','c') ,array('x','y','z') ,array('1','2','3') ,array( 4 , 5 , 6 ) ); x6data('exampStuff',$data); } } ?>

All of the code in x6main should be familiar now, so we will concentrate on the function gimmeData(), which is invoked by JSON call from the browser when the user clicks the button.

The example first generates an array of arrays, and then goes directly to the x6data() call. The x6data() function says, "here is some data to send back to the browser." The function has two parameters. The first is the name of the data element, which can be anything. The second is the data itself.

Now looking up towards the Javascript, we see that the data we sent back with the name "exampStuff" can be found browser-side in the "" object as "". After that it is a matter of simply recursing through the data and generating a simple HTML table by way of example.

This is an ideal method for sending database rows back to the browser.
comments powered by Disqus
Home |  Documentation |  Download |  Credits |  Contact |  Login
Andromeda © Copyright 2004-2013, Licensed under the GPL Version 2