Jump to content

have a question, hope somebody can help......


meomike2000

Recommended Posts

ok i am fairly new to ajax and java script...... only got a few weeks under my belt.....

 

so here is the situation.....

the ajax script that does the request is called Ajax();

i have a script that gets a set of id # from a database via php....

this script is a singleton ie starts like this var getids = new function(){ };.......

inside that script after the ajax.doget() and data is retrieved the datahandler function uses a forloop to pass the id# one by one to another script alled (getinfo)......

should getinfo be a singleton or a function........

when set as a singleton i can make another call to ajax.doget() to get the info by the id. that i pass to it.....

if set up as a function i can not seem to get it to work correctly......

 

here are the two.....

 

var getfriends = new function() {
this.ajax = null;
this.uid = 0;
this.Div = null;
this.br = null;

this.init = function(uid) {
	var self = getfriends;
	self.ajax = new Ajax();
	self.uid = uid;
	self.Div = document.getElementById('mainhomeleft'); 
	self.br = document.createElement('br');
	self.br.className = 'clearBoth';
	self.getf();
};

this.getf = function() {
	var self = getfriends;
	var uid = self.uid;
	var date = new Date();
	self.ajax.doGet('scripts/getfriendids.php?uid=' + uid + 'date=' + date, self.sort);
};

this.sort = function(str) {
	var self = getfriends;
	var counter = 0; 
	var index = 0;
	var br = self.br;
	var friends = str.split('|'); 
	var div = self.Div;
	if (div.firstChild) {
		div.removeChild(div.firstChild);
	}
	for (var i = 1; i < friends.length; i++) {
		var afriend = new friend();
		afriend.init(friends[index],index);
		counter++;
		index++;
		if (counter == 3) {
			div.appendChild(br);	
			counter = 0;
		}			
	}

};

this.cleanup = function() {
	var self = getfriends;
	self.ajax = null;
	self.uid = 0;
	self.Div = null;
	self.br = null;
};
};

function friend() {
this.ajax = null; 
this.Div = null;
this.id = 0;
this.index = 0;
this.name = null;
this.pic = null;
this.status = null;
this.logtime = null;
this.logdate = null;
this.isInputDisabled = false; 

this.init = function(id,index) {
	var self = this;
	this.ajax = new Ajax();
	this.id = id;
	this.index = index; 
	this.Div = document.getElementById('mainhomeleft');
	this.Div.ondblclick = self.toggle;
	this.getfriend(); 

};

this.toggle = function(e) {
	var self = this;
	var elem = null;
	if (!e) {
		e = window.event;
	}
	elem = this.getSrcElem(e);
	id = elem.id.replace(/div|span/, '');
	if (id != 'editCancel' && !this.isInputDisabled) {
		this.editId = id;
		this.showoptions();
		this.disableEnableMainWinInput(false);
	}
	else if (id == 'editCancel') {
		this.showfriend();
		this.editId = '';
		this.disableEnableMainWinInput(true);
	}
};

this.getSrcElem = function(e) {
	var ret = null;
	if (e.srcElement) {
		ret = e.srcElement;
	}
	else if (e.target) {
		ret = e.target;
	}
	while (!ret.id && ret) {
		ret = ret.parentNode;
	}
	return ret;
};

this.disableEnableMainWinInput = function(enable) {
	var self = this;
	this.isInputDisabled = !enable;
};

this.getfriend = function() {
	var self = this;
	var id = this.id;
	var date = new Date();
	self.ajax.doGet('scripts/getfriendinfo.php?id=' + id + 'date=' + date, self.sortfriend);
};	

this.sortfriend = function(str) {
	var self = this;
	var id = self.id;
	var frnd = str.split("~");
	this.name = frnd[0];
	this.pic = frnd[1];
	this.status = document.createTextNode('online');
	this.logtime = frnd[3];
	this.logdate = frnd[4];
	this.showfriend(id);
};	

this.showfriend = function() {
	var self = this;
	var div = this.Div; 
	var fddiv = null;
	var status = null; 
	var fnamediv = null;
	var fpicdiv = null;
	var fpic = null;  
	var fonlinediv = null;
	var flogtime = null;
	var flogdate = null;		 		
	fddiv = document.createElement('div');
	fddiv.id = 'friend' + self.index;
	fddiv.className = 'friend';

	fnamediv = document.createElement('div');
	fpicdiv = document.createElement('div');
	fpic = document.createElement('img');
	fpic.src = this.pic;
	fpic.className = 'fpic';
	fonlinediv = document.createElement('div');
	fonlinediv.id = 'fonlinediv';
	flogtimediv = document.createElement('div');
	flogtimediv.id = 'flogtimediv';
	flogdatediv = document.createElement('div');
	flogdatediv.id = 'flogdatediv';
	fnamediv.appendChild(document.createTextNode('testname'));
	fpicdiv.appendChild(document.createTextNode(this.id));
	fddiv.appendChild(fnamediv);
	fddiv.appendChild(fpicdiv);			
	fonlinediv.appendChild(document.createTextNode(this.status));
	flogtimediv.appendChild(document.createTextNode(this.logtime));
	flogdatediv.appendChild(document.createTextNode(this.logdate));
	fddiv.appendChild(fonlinediv);
	if (self.status == "online") {
		fonlinediv.id = 'fonline';
	}
	else {
		fonlinediv.id = 'foffline';
		fddiv.appendChild(flogtimediv);
		fddiv.appendChild(flogdatediv);
	}			
	div.appendChild(fddiv);	
};

this.showoptions = function() {
	var self = this;
	var div = self.Div; 
	var fddiv = null;
	var status = null; 
	var fnamediv = null;
	var fpicdiv = null;
	var fpic = null;  
	var fonlinediv = null;
	var flogtime = null;
	var flogdate = null;	

	var fdright = null;
	var fdleft = null; 
	var cancelButton = null;
	cancelButton = document.createElement('input');
	cancelButton.type = 'button';
	cancelButton.className = 'inputButton';
	cancelButton.id = 'editCancel';
	cancelButton.onclick = self.toggle;
	cancelButton.value = 'Cancel';

	fddiv = document.createElement('div');
	fddiv.id = 'friend' + self.index;
	fddiv.className = 'friend';
	fdleft = document.createElement('span');
	fdleft.id = 'fdleft';
	fdright = document.createElement('span');
	fdright.id = 'fdright';		
	fnamediv = document.createElement('div');
	fpicdiv = document.createElement('div');
	fpic = document.createElement('img');
	fpic.src = self.pic;
	fpic.className = 'fpic';
	fonlinediv = document.createElement('div');
	fonlinediv.id = 'fonlinediv';
	flogtimediv = document.createElement('div');
	flogtimediv.id = 'flogtimediv';
	flogdatediv = document.createElement('div');
	flogdatediv.id = 'flogdatediv';
	fnamediv.appendChild(document.createTextNode(self.name));
	fpicdiv.appendChild(fpic);

	fdright.appendChild(fnamediv);
	fdright.appendChild(fpicdiv);

	fonlinediv.appendChild(document.createTextNode(self.status));
	flogtimediv.appendChild(document.createTextNode(self.logtime));
	flogdatediv.appendChild(document.createTextNode(self.logdate));

	fdright.appendChild(fonlinediv);
	if (self.status == "online") {
		fonlinediv.id = 'fonline';
	}
	else {
		fonlinediv.id = 'foffline';
		fdright.appendChild(flogtimediv);
		fdright.appendChild(flogdatediv);
	}
	fddiv.appendChild(fdright);

	fdleft.appendChild(cancelButton);
	fddiv.appendChild(fdleft);

	div.appendChild(fddiv);	
};

this.cleanup = function() {
	var self = this;
	self.Div = null;
	self.ajax = null; 
	self.id = 0;
	self.name = null;
	self.pic = null;
	self.status = null;
	self.logtime = null;
	self.logdate = null;
};
}		

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.