2

Ok, some explanation. Even though I don't think it has anything to do with the problem itself. I have a small django project that maps some data using leaflet. On a mouseover some ajax functionality is added, using the dajax(which is a "lightweight library to implement AJAX inside django projects") framework. The call itself looks like this:

dajax.add_data(simplejson.dumps(series), 'my_test_flot')

My js function receives json data which looks like this (using alert)

[{"color": "#dddd00", 
"data": [[-0.5, -20.5]], 
"label": "Tweede Zandlaag"}]

The object has more data to it but the problem is not with the object. When I copy/paste the data directly into the function var series = [] the behaviour is as aspected. As aspected means, the graph I'm drawing with flot is actually being drawn. Otherwise the graph remains empty.

function my_test_flot(dat) {
    function MyFormatter(v, xaxis) {
        return " ";
    }
    $(function () {
        alert(dat)
        var series = dat; // here lies the problem, but why?
        ...

Can anyone help?

LarsVegas
  • 6,522
  • 10
  • 43
  • 67

2 Answers2

3

Ok, problem solved. Apparently you have to use JSON.parse(). How it's done is explained here.

Community
  • 1
  • 1
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
0

This does not copy the data - it just makes series a reference to the same object as dat. Therefore, if you later modify the object, all users retaining references to it see the changes. This is probably what causes your trouble.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122