Trond,

Nice stuff. Thanks for sharing it. I was just looking at JSON a couple
dyas ago so it is quite timely.

Rich


I know this is a bit of an old message, but I thought I'd just say
that I've just started using JSON in my templates, and I'm very
satisfied with how it works.

In this case, the template would look something like this:

{
  img: '[img]',
  shp: [ [shpminx], [shpminy], [shpmaxx], [shpmaxy] ],
  queryfile: '[queryfile]'
}

And my function to parse the template looks like this:

AJAX_getMapObject = function (content) {
    var mapobj;
    try {
        var code = "(// " + content + ")";
        mapobj = eval(code);
    } catch (e) {
        alert("eval failed: " + e.message);
    }
    if (typeof mapobj == "undefined") {
        alert("Could not create mapobject:\n" + req.responseText);
    }
    return mapobj;
};

You invoke it with the responseText from the XMLHttpRequest-object, like this:

var result = AJAX_getMapObject(req.responseText);

and the variables are now available as properties on result:

var img = result.img;
var shpminx = result.shp[0];

etc.


I also use this on the main requests (i.e. when the user zooms or pans
the map), and that template looks like this:

// start template
{
  mapserver: {
    version: '[version]',
    id:      '[id]',
    host:    '[host]',
    port:    '[port]',
    errmsg:  decodeURIComponent('[errmsg_esc]')
  },
  image: {
    center:   '[center]',
    center_x: '[center_x]',
    center_y: '[center_y]',
    mapsize:  '[mapsize]',
    mapwidth: '[mapwidth]',
    scale:    '[scale]',
    cellsize: '[cellsize]'
  },
  file: {
    img:       '[img]',
    ref:       '[ref]',
    legend:    '[legend]',
    scalebar:  '[scalebar]',
    queryfile: '[queryfile]',
    map:       '[map]'
  },
  map: {
    mapx:   '[mapx]',
    mapy:   '[mapy]',
    maplon: '[maplon]',
    maplat: '[maplat]',
    dx:     '[dx]',
    dy:     '[dy]',
    mapext:        [ [minx],    [miny],    [maxx],    [maxy]    ],
    rawext:        [ [rawminx], [rawminy], [rawmaxx], [rawmaxy] ],
    mapext_latlon: [ [minlon],  [minlat],  [maxlon],  [maxlat]  ],
    refext:        '[refext]'.split(' '),

    mapext_str:        '[mapext]',
    rawext_str:        '[rawext]',
    mapext_latlon_str: '[mapext_latlon]',
    refext_str:        '[refext]'
  },
  layer: {
    layers:        decodeURIComponent('[layers_esc]').split(' '),
    toogle_layers: decodeURIComponent('[toggle_layers_esc]').split(' ')
  },
}
// end template

With this template, I get an object that contains all the variables
that are available from mapserver (except the dynamic ones, of course,
but as far as I can tell, those are only necessary in a completely
javascript-free application.

I'm using decodeURIComponent() combined with the _esc-variant of the
variables that could contain ', to prevent syntax errors during eval().

--
Trond Michelsen



--
Richard Greenwood
[EMAIL PROTECTED]
www.greenwoodmap.com

Reply via email to