0

I need to move the data out of HTML code and load it on demand.

I need to do something like this:

function processData( data )
{
   if ( data.length===0 )
   {         
      data = get data from server using Ajax or even...
      data = [['2011-06-01',1],['2011-06-02',3]] ; // just for educational purposes
   }
   else go do stuff with data ;
} 

storeData = [] ;
processData( storeData ) ; // first time storeData doesn't contain any data
processData( storeData ) ; // now storeData contains data

I can't figure out how to stuff the data from within the function. Is there a way of accomplishing this?

2 Answers 2

1
function processData()
{
   if ( storeData.length===0 )
   {         
      storeData = get data from server using Ajax
   }
   else go do stuff with storeData ;
} 

storeData = [] ;
processData( storeData ) ; // first time storeData doesn't contain any data
processData( storeData ) ; // now storeData contains data

storeData is a global anyway. When you specify processData( data ) you are doing what's called a pass by value. Basically your making a copy of the data. Once the program exits the function, the copy is lost to garbage collection. An alternative would be to pass by reference, but because it's a global anyway (declared outside the function) there's little point.

Edit

read here

http://snook.ca/archives/javascript/javascript_pass

Sign up to request clarification or add additional context in comments.

Comments

0

It might help to know more concrete details since it seems like you might be going about your task in an unusual way. There may be a better way of accomplishing what you want.

Have you just tried something as simple as:

function processData( data )
{
    ...
    return data;
} 

storeData = [] ;
storeData = processData( storeData ) ; // first time storeData doesn't contain any data
storeData = processData( storeData ) ; // now storeData contains data

1 Comment

Not elegant, but you could have the function return two values (an array of 2 values), one containing the jPlot object and the other containing the data. Also, it sounds like the other Joseph that answered your question has a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.