I have an object with a single Method to load content from a xml file. The problem is... how do I add a property to the object to store the data loaded?? I tryed adding a simple Array inside the object, but didn't work.
function(){
var name = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
*** HERE...add point to a collection (may an array) as property inside DataCollection object
});
});
}
}
var dc = new DataCollection();
dc.load();
Thanks!
08-05-2011, 05:43 PM
twseitex
maybe this
Hi,
collection is property of object or return value
var dc = new DataCollection();
dc.load(); // // properties of dc created during load() ?
var ptCollection=cd.load(); // no property but return
function(){var name .... // local to function, no property
08-06-2011, 02:30 AM
astupidname
Code:
function DataCollection()
{
var obj = this; //reference to the created instance of DataCollection
this.points = []; //or you could do: obj.points = []; here within the constructor code
this.load = function(filename)
{
$.get(filename, function(xml){
$(xml).find("marker").each(
function(){
var name = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
*** HERE...add point to a collection (may an array) as property inside DataCollection object
obj.points.push(point);
});
});
}
}
var dc = new DataCollection();
dc.load();
Or, more appropriately, use prototype methods rather than define object methods in the constructor:
Code:
function DataCollection() {
this.points = [];
}
DataCollection.prototype.load = function(filename){
var obj = this;
$.get(filename, function(xml){
$(xml).find("marker").each(
function(){
var name = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
*** HERE...add point to a collection (may an array) as property inside DataCollection object
obj.points.push(point);
});
});