Reputation: 24233
I have a java web service, that returns a list of json objects to me. In my UI, I want to use that data. For that Here's what I am doing.
function loadList(){
$.get("myservice",function(data){
alert(data.list[2].key1);
}
I have an element in my html, for which I have set some onmouseover event. But for that, I would want this data to be used. Now since I am not sure when that even will happen, and I don't know how to use this data
that I have got from web service, to use outside this function. I thought of copying the data to another temp variable, but my size of json result can be huge sometimes.
Also, I'll be using D3JS for the Data Element Manipulation when I am playing with the mouseover etc.
So, is there a way to use the data that I have got from web service, somewhere outside this get method? Thanks.
Upvotes: 0
Views: 93
Reputation: 2082
If it's going to be on the element only, store the data on the element using the "data()" method:
function loadList(){
$.get("myservice",function(data){
$('.myElement').data('svc-data',data);
}
}
Then, when you need it in your mouseover, just retrieve it using the same data method:
$('.myelement').on('mouseover',function() {
var data = $(this).data('svc-data');
alert(data.list[2].key1);
});
Doc on Data: http://api.jquery.com/category/miscellaneous/data-storage/
Update: further clarification
'svc-data' is a "key" you give your data on the element. The data method stores data (objects, json, strings, etc...) by associating it with the element specified using the key for reference. So, 'svc-data' is what I called that reference since it was data from your service (svc = service to me). If you save JSON data (ie: list.sub.item) you will still be able to access it as JSON when you retrieve it since jQuery stores the object you give as the same type - no serialization is used.
The second part shows how to retrieve the data associated with an element. When you retrieve using $('selector').data('key') (see above) the data comes back as the type you saved and can be used as such. In the case of JSON, you retrieve a JSON object and can still use it as such (ie: list.sub.item) as I tried to show with the alert.
The key with this is the data is associated with the element specified with your selector so you can only retrieve the data by first referencing the element you want to retrieve the data from. At the same time, you can have 5 elements with 'svc-data' on them and you don't have to worry about conflicts.
Update2: Example in fiddle for you to see what I mean: http://jsfiddle.net/zYdRH/
Update3: One more example added is an instance variable. http://jsfiddle.net/zYdRH/3/
Notice how everything is wrapped in an anonymous, self-executing function. This is a great approach to keep variables and functions out of the global scope. This means each item inside this function is only accessible to this function. I can't call any of these functions from another element, nor can I access the variables. Because of that, you can also store your JSON data in this scope to be accessed from any elements also in this scope. The newest example I added has a specific one called instanced but all the json vars can be used the same way - it was just more obvious for an example. But, note how instanced is a JSON object that is not stored to anything. It's global to the scope. Double click on a button to see each one showing it's associated word by the class.
If your JSON data is unique to each button, the data() method is the way to go. However, if it's the same JSON for each but each element has it's own piece of the data it needs, the instance var is the way to go.
Upvotes: 1