Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am customising a plugin that has array (if that is what is it) that holds config items:

        var config = {
            width:  "100%",
            height: "100%",
            source: 'js/timeline/test.json',                
            css:    'js/timeline/compiled/css/timeline.css',    
            js:     'js/timeline/compiled/js/timeline-min.js'   
        }

What I want to do is take the source: data and replace it with a variable eg:

var mysource = 'path/to/source.json';

var config = {
                width:  "100%",
                height: "100%",
                source: mysource ,              
                css:    'js/timeline/compiled/css/timeline.css',    
                js:     'js/timeline/compiled/js/timeline-min.js'   
            }

But as it above it does not work. Can anyone point me in the right direction? Thanks

EDIT: Added full code as requested

$(function() {

    $.getJSON('http://www.mysite.com/json.php',

    function(data) {

        jsonObject = eval(data);
        var eventdata = jsonObject.tlall;

    });

}); //END ON LOAD

var config = {
    width: "100%",
    height: "100%",
    source: eventdata,
    //source: 'js/timeline/test.json',
    //start_at_end: true,   //OPTIONAL              
    //hash_bookmark: true,  //OPTIONAL              
    css: 'js/timeline/compiled/css/timeline.css',
    js: 'js/timeline/compiled/js/timeline-min.js'
}​
share|improve this question
    
Is it because you have 2 commas on the line before? –  Aesthete Aug 23 '12 at 6:21
1  
@nbrooks sorry guys - that was a mistake when I was transcribing the code. The problem still remains –  MeltingDog Aug 23 '12 at 6:23
2  
I don't see any problem! Do you have them in same block/function ? –  Mihai Iorga Aug 23 '12 at 6:24
    
Works fine for me –  Aesthete Aug 23 '12 at 6:25
2  
That's not an array btw, it's an object, or list of key:value pairs. Similar to a dictionary in other languages. Somtimes referred to as a hash or map... –  danwellman Aug 23 '12 at 6:38

3 Answers 3

up vote 1 down vote accepted

I don't know how exactly you plan to use config, but you certainly need it to be within the same scope in order to access eventdata. I would say get rid of that altogether, and just add it as a property of the object in the ajax callback (shown in code below).

The difficulty here is that you are stuck waiting on the AJAX callback to fire before you can use config in your scripts; this is why ajax is designed to be used with callbacks executed after a particular event rather than linearly; I would advise you to use config only within that success callback, or add a check to see if getJSON has finished executing.

$(function() {

    var config = {
        width: "100%",
        height: "100%",            
        css: 'js/timeline/compiled/css/timeline.css',
        js: 'js/timeline/compiled/js/timeline-min.js'
    };

    $.getJSON('http://www.mysite.com/json.php', function(data) {
        jsonObject = eval(data);
        config.source = jsonObject.tlall;
    });

}); //END ON LOAD​​​​​​​​​​​​​​​​
share|improve this answer
    
Thanks! This has gotten rid of my Uncaught Reference Error –  MeltingDog Aug 23 '12 at 6:50

Change this line:

var eventdata = jsonObject.tlall;

To this:

config.source = jsonObject.tlall;

And remove this line from your config definition:

source: eventdata,
share|improve this answer

Scoping issue, try :

$(function() {
    $.getJSON('http://www.mysite.com/json.php', function(data) {
        config.source = data.tlall;
    });

    var config = {
        width: "100%",
        height: "100%",
        css: 'js/timeline/compiled/css/timeline.css',
        js: 'js/timeline/compiled/js/timeline-min.js'
    }
});​
​
share|improve this answer
    
I'm not sure that this would work, due to the fact that eventdata is only assigned once the AJAX call returns. –  Jonathan Maddison Aug 23 '12 at 6:39
    
@nbrooks - why is that ? –  adeneo Aug 23 '12 at 6:50
    
Ah sorry, tried it and it works, good to know :) +1 –  nbrooks Aug 23 '12 at 6:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.