How to make Javascript object available outside function?
I am retrieving some JSON data with the following function. It is called when an element is clicked and the data is used to create a table, populate the cells with images and append the table to a div.
function LoadImagesByCategory() {
$.getJSON("/Service/GetJson.ashx?data=images", function(data) {
jsObjectData = data.ImageCollection.Images;
//Create a table named imageTable
$("#imagesByCategory").append(imageTable);
}
})
the jsObjectData looks like this.
{"ImageCollection":
{"Images":
[{ "ImageID":"68",
"CatID":"1",
"Name":"uploadedFile",
"Location":"/Images/Art/Full/68.gif",
"ClipLocation":"/Images/Art/Clips/68.gif",
"FullHeight":"504",
"FullWidth":"451"
},
{ "ImageID":"69",
"CatID":"1",
"Name":"uploadedFile",
"Location":"/Images/Art/Full/69.gif",
"ClipLocation":"/Images/Art/Clips/69.gif",
"FullHeight":"364",
"FullWidth":"500"
},
etc. etc
]
}
}
It contains additional info about the images like the FullHeight and the FullWidth that I would like to be able to retrieve when an img is clicked. For example if I made the id of the img something like "68ArtImage" where 68 is the Image开发者_开发知识库ID I would like to be able to pass the 68 into a function attached to the jsObjectData and retrieve the corresponding Image Data. The problems are first that I don't know how to make the object avail. outside the function and second I don't know how to attach the function to the object.
After reading your question again, perhaps this is what you want?
function getImageData(id, json){
for(i in json.ImageCollection.Images){
if( json.ImageCollection.Images[i].ImageID == 69){
return json.ImageCollection.Images[i];
}
}
return false;
}
getImageData
will search the given json object and return the image object (like an associative array) if it exists, else false.
Example:
var json = {"ImageCollection":
{"Images":
[{ "ImageID":"68",
"CatID":"1",
"Name":"uploadedFile",
"Location":"/Images/Art/Full/68.gif",
"ClipLocation":"/Images/Art/Clips/68.gif",
"FullHeight":"504",
"FullWidth":"451"
},
{ "ImageID":"69",
"CatID":"1",
"Name":"uploadedFile",
"Location":"/Images/Art/Full/69.gif",
"ClipLocation":"/Images/Art/Clips/69.gif",
"FullHeight":"364",
"FullWidth":"500"
}
]
}
}
function getImageData(id, json){
for(i in json.ImageCollection.Images){
if( json.ImageCollection.Images[i].ImageID == 69){
return json.ImageCollection.Images[i];
}
}
return false;
}
if(image = getImageData(69, json)){
alert('found the image wooo!');
// now do something with your image object
}
else{
alert('no image with that id found');
}
Here's some pseudo code:
//global
var gImageTable = {};
function LoadImagesByCategory() {
$.getJSON("/Service/GetJson.ashx?data=images", function(data) {
jsObjectData = data.ImageCollection.Images;
// Add the images to the image table
for (var i=0; i < jsObjectData.length; i++) {
var img = jsObjectData[i];
gImageTable[img.ImageId] = img;
}
// Create a table named imageTable. Should add the id into each of the images
// so we can access its data from the click handler
$("#imagesByCategory").append(imageTable);
})
}
// The click handler that knows about image map structure
$("#imagesByCategory").click(function (e) {
// or something smarter to detect that it
// was a click within one of the images
if (e.target.tagName == "IMG") {
var imageData = gImageTable[e.target.id];
console.log(imageData.CatID, imageData.FullHeight);
}
});
This is not how I would put it into production. I hate globals, so I would have an object that manages that table. Then the object could hold the reference to the imageTable
精彩评论