I have done this in the past and in different ways.
The idea of $('selector').data
is probably one of the most useful methods. I like this way of storing data because I can store data in a logical, intuitive and orderly way.
Let's say you have an ajax call that retrieves 3 articles when the page loads. Articles may contain data related to the title, date / time, source, etc. Suppose you want to show headings and when the headline lights up, you want to show the full article and its data.
To illustrate the concept a bit, let's say we get json that looks something like this:
{ articles: [ { headline: 'headline 1 text', article: 'article 1 text ...', source: 'source of the article, where it came from', date: 'date of the article' }, { headline: 'headline 2 text', article: 'article 2 text ...', source: 'source of the article, where it came from', date: 'date of the article' }, { headline: 'headline 3 text', article: 'article 3 text ...', source: 'source of the article, where it came from', date: 'date of the article' } ] }
From such a call ajax.,.
$.ajax({ url: "news/getArticles", data: { count: 3, filter: "popular" }, success: function(data){
The idea is that we can store the associated data with the dom element and receive / manipulate / delete this data later, when necessary. This reduces the potential for additional data calls and effectively caches data to a specific dom element.
at any time after adding a title to the document, the data can be accessed through the jquery selector. To access article data for the first heading:
$('#headlines .headline:first()').data.article.headline $('#headlines .headline:first()').data.article.article $('#headlines .headline:first()').data.article.source $('#headlines .headline:first()').data.article.date
Accessing your data through a selector and jquery object is sorted neatly.