0

When i have the following global variable on my application start :

events = [];

Then if i go fetch something with ajax with the following simplified code snippet :

events[0] = [];

setTimeout(function fetchEventsThisWeek(){

    $.ajax({
      url: '/event/getEventsBetweenDates',
      type: 'POST',
      data : { from_date : currentweek.from_date.toString("yyyy-MM-d") , to_date :  currentweek.to_date.toString("yyyy-MM-d"), limit : limit, offset : offset },
      success: function(data) {
            jQuery.each(data, function(index){
                events[0].push(data[index]['attributes']);
            });




            offset = offset + limit;
            entry_count = entry_count + data.length;

            if(data.length < limit) { // We reached the end of the table so we don't need to call the function again
                renderEvents(current, offset - limit, entry_count);
                //Make sure the current next week button gets enabled because we are done with the results

            } else {
                renderEvents(current,  offset - limit, offset);
                setTimeout(fetchEventsThisWeek, 150); // There are more results, continue
            }

      }
    })
}, 150);

This recursive function just fetches all events between two dates and keeps calling itself until there is no record in the db left. My problem is:

With the variable:

events[0] = [];

I want to specify the index of the array as my week entry. So if i look for a specific week, i can get all the entries that already have been fetched from my array by the array index.

My problem is, when i want to fetch more weeks, so for example:

events[1] = [];// Index 1 would represent the next week

The array just expands in size and all gets appended to the end, so i have one big array and not a multidimensional one. Why is this? And how can i achieve this behaviour?

Edit: Let me expand on my question.

I need several arrays of json objects in the events variable. So..

events[0] = [ /*contains array of json objects */];
events[1] = [ /*contains array of json objects */];
events[2] = [ /*contains array of json objects */];

Each array index represent 1 week. So index 0 is the current week, index 1 is week 1, index 2 is week 2 and so forth. I even want to do the following but i don't know if this is even possible:

events[-1] = [ /*contains array of json objects */];

Where the index -1 would be 1 week in the past. Could anybody let me know if this is possible?

1
  • I don't understand the problem. Do you simply want to parametrize the hardcoded 0 index? And, by the way, what is setTimeout doing there? Commented Nov 30, 2012 at 19:47

1 Answer 1

1

You're looking for Array.unshift:

events.unshift([]);

Documentation

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

Comments

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.