Groups | Blog | Home
all groups > flash actionscript > october 2006 >

flash actionscript : Arrays stored in class


Nicholas Wood
10/10/2006 9:07:52 PM
Hi guys,

currently workng on my own private class to access and store an xml doc.

my current class looks like:

// ---------------------------

class Blogger {

public var feedURL:String;
private var totalEntries:Number;
public var blogEntries:Array = [];

public function Blogger(feedSource:String) {

feedURL = feedSource;
var data_xml= new XML();
data_xml.ignoreWhite = true;
var blogEntries:Array = [];

data_xml.onLoad = function(success) {
if (success) {
totalEntries = data_xml.firstChild.childNodes.length - 10;
for (var i:Number = 0; i < totalEntries; i++){

blogEntries.push({ author:data_xml.childNodes[0].childNodes[(10+i)].childNo
des[1].childNodes[0].childNodes[0].nodeValue,

date:data_xml.childNodes[0].childNodes[(10+i)].childNodes[4].childNodes[0].nodeV
alue,

title:data_xml.childNodes[0].childNodes[(10+i)].childNodes[7].childNodes[0].node
Value,

article:data_xml.childNodes[0].childNodes[(10+i)].childNodes[8].childNodes[0].no
deValue
});
}
}
}

data_xml.load(feedURL);
}
}

// -----------

Now all the xml is loading and being accessed fine, the problem I am having is
pushing this data into the array I have created to store it all "BlogEntries".
This is continuously coming back as length of 0 so the data is for some reason
not being pushed in.. any ideas or help gratefully received.

cheers everyone!
nick
LuigiL
10/10/2006 9:18:43 PM
Seems to me all you need to do is copy the local BlogEntries to the
classmember. Otherwise you need a local reference to the current object to
access class members in callback handlers - see second chunk of code.



class Blogger {

public var feedURL:String;
private var totalEntries:Number;
public var blogEntries:Array = [];//this is a static array!

public function Blogger(feedSource:String) {

feedURL = feedSource;
var data_xml= new XML();
data_xml.ignoreWhite = true;
var blogEntries:Array = [];

data_xml.onLoad = function(success) {
if (success) {
totalEntries = data_xml.firstChild.childNodes.length - 10;
for (var i:Number = 0; i < totalEntries; i++){
blogEntries.push({
author:data_xml.childNodes[0].childNodes[(10+i)].childNodes[1].childNodes[0].chi
ldNodes[0].nodeValue,

date:data_xml.childNodes[0].childNodes[(10+i)].childNodes[4].childNodes[0].
nodeValue,

title:data_xml.childNodes[0].childNodes[(10+i)].childNodes[7].childNodes[0]
..nodeValue,

article:data_xml.childNodes[0].childNodes[(10+i)].childNodes[8].childNodes[
0].nodeValue
}
);
}
}
this.blogEntries=blogEntries;
}

class Blogger {

public var feedURL:String;
private var totalEntries:Number;
public var blogEntries:Array;

public function Blogger(feedSource:String) {
var thisObj:Blogger=this;//local ref to the current object
blogEntries=new Array();
feedURL = feedSource;
var data_xml= new XML();
data_xml.ignoreWhite = true;
//var blogEntries:Array = [];

data_xml.onLoad = function(success) {
if (success) {
totalEntries = data_xml.firstChild.childNodes.length - 10;
for (var i:Number = 0; i < totalEntries; i++){
thisObj.blogEntries.push({
author:data_xml.childNodes[0].childNodes[(10+i)].childNodes[1].childNodes[0].chi
ldNodes[0].nodeValue,

date:data_xml.childNodes[0].childNodes[(10+i)].childNodes[4].childNodes[0].
nodeValue,

title:data_xml.childNodes[0].childNodes[(10+i)].childNodes[7].childNodes[0]
..nodeValue,

article:data_xml.childNodes[0].childNodes[(10+i)].childNodes[8].childNodes[
0].nodeValue
}
);
}
}
this.blogEntries=blogEntries;
}
AddThis Social Bookmark Button