Ho scritto questo codice per caricare dati in un oggetto da xml tramite l'oggetto ajax di jQuery:
codice:
$(document).ready(function()
{
var desktop=Desktop("../config/desktop.xml");
});
//TODO Desktop object
Desktop=function(xmlConfig){
var icons=null;
function loadIcons(xml)
{
var foundIcons=new Array();
//Finds every Icon occurrence into xml config file and loads them as Icon objects into foundIcons collection array
$(xml).find("Icon").each(function()
{
foundIcons.push(Icon($(this)));
});
//Load icons into Desktop object's icon array
icons=foundIcons;
}
//Init desktop
$.ajax({
type: "GET",
url: xmlConfig,
dataType: "xml",
success: function(xml) {
loadIcons(xml);
}
});
};
//Icon Object (module pattern model)
Icon=function(iconXMLEntry){
var id=iconXMLEntry.attr('id');
var title=iconXMLEntry.find('title').text();
var image=iconXMLEntry.find('image').text();
var position=iconXMLEntry.find('position').text();
alert(title);
}
Di seguito l'xml che ho tentato di fornirgli:
codice:
<?xml version="1.0" encoding="utf-8" ?>
<Icons>
<Icon id="01">
<title>Scrum Sprinter</title>
<image>scrum.png</image>
<position>20,30</position>
</Icon>
<Icon id="02">
<title>Stick it!</title>
<image>stickit.png</image>
<position>20,90</position>
</Icon>
<Icon id="03">
<title>Net paper</title>
<image>netpaper.png</image>
<position>20,150</position>
</Icon>
<Icon id="04">
<title>Net Documents</title>
<image>netdocuments.png</image>
<position>20,210</position>
</Icon>
</Icons>
Sostanzialmente per ogni oggetti Icon mi legge dall'xml soltanto l'id e non vede gli altri valori.
Non credo sia un problema di scope perchè il metodo .attr() per l'id viene chiamato correttamente.
Chi saprebbe aituarmi.
Il progetto è hostato su google code ed è open source:
jsCommonWorkspace