2 Replies - 209 Views - Last Post: 30 September 2018 - 09:46 AM

#1 JeremyBenson11   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 276
  • Joined: 10-January 14

Ajax Class Value Issues

Posted 30 September 2018 - 08:48 AM

I have a basic object set out to hold my character values. I set the character through ajax. For some reason my athletics skill has no value, despite an initial value. It also loses its value by the closing brace of success. In the class it has an initial value of 0, when I call my set script I get a value of 1 for athletics, by the end of success it's back to 0. The first alert shows undefined also, for some reason.

var character = new function Character() {
    this.name = '';
	this.cp = 0;
	this.cpSpent = 0;
    this.level = 1;
	this.hit = 0;
	this.aim = 0;
	this.dodge = 0;
	this.absorb = 0;
	this.arcane = 0;	
	this.athletics = 0;
	this.ride = 0;
	this.swim = 0;
	this.climb = 0;
	this.bluff = 0;
	this.intimidate = 0;
	this.wilderness = 0;
	this.dungeoneering = 0;
	this.diplomacy = 0;
	this.lore = 0;
	this.thievery = 0;
	this.appraisal = 0;
	this.traps = 0;
	this.listen = 0;
	this.hide = 0;
	this.spot = 0;
	this.weapons = 0;
	this.armor = 0;
	this.cooking = 0;
	this.music = 0;
	this.painting = 0;
	this.tailoring = 0;
	this.jewelering = 0;
	this.carving = 0;
	this.pottery = 0;
	this.spellcraft = 0;
	this.wands = 0;
	this.alchemy = 0;
	this.items = 0;
	this.advanceAmount = 2;
	this.maxSkill = 97;
	
	this.setCharacter = function(response, token)
	{
		
		 $.ajax({
		  type: "POST",
		  dataType: "json",
		  url: response, 
		  data: {charToken:token},
		  context:this,
		  success: function(data) {
			  
				  this.cp = data['characterPoints'];
				  this.level = data['level'];
				  
				  $.each(data, function(key, value){
					
					switch(key)
					{
						
						case 'athletics':
							alert('Before set: ' + this.athletics);
							this.athletics = value;
							alert('After set: ' + this.athletics);
						break;
						case 'ride':
							this.ride = value;
						break;
						case 'swim':
							this.swim = value;
						break;
						case 'climb':
							this.climb = value;
						break;
						case 'bluff':
							this.bluff = value;
						break;
						case 'intimidate':
							this.intimidate = value;
						break;
						case 'wilderness':
							this.wilderness = value;
						break;
						case 'dungeoneering':
							this.dungeoneering = value;
						break;
						case 'diplomacy':
							this.diplomacy = value;
						break;
						case 'lore':
							this.lore = value;
						break;
						case 'thievery':
							this.thievery = value;
						break;
						case 'appraisal':
							this.appraisal = value;
						break;
						case 'traps':
							this.traps = value;
						break;
						case 'listen':
							this.listen = value;
						break;
						case 'hide':
							this.hide = value;
						break;
						case 'spot':
							this.spot = value;
						break;
						case 'weapons':
							this.weapons = value;
						break;
						case 'armor':
							this.armor = value;
						break;
						case 'cooking':
							this.cooking = value;
						break;
						case 'music':
							this.music = value;
						break;
						case 'painting':
							this.painting = value;
						break;
						case 'tailoring':
							this.tailoring = value;
						break;
						case 'jewelering':
							this.jewelering = value;
						break;
						case 'carving':
							this.carving = value;
						break;
						case 'pottery':
							this.pottery = value;
						break;
						case 'spellcraft':
							this.spellcraft = value;
						break;
						case 'wands':
							this.wands = value;
						break;
						case 'alchemy':
							this.alchemy = value;
						break;
						case 'items':
							this.items = value;
						break;
						
					}
					
					 // end each
				  });
				  
				  alert('After set function: ' + this.athletics);
				  
				// end success
			  }
		 // end ajax 
		});
		
	};

// end class
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ajax Class Value Issues

#2 JeremyBenson11   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 276
  • Joined: 10-January 14

Re: Ajax Class Value Issues

Posted 30 September 2018 - 09:23 AM

I solved this making a temp var at the top of the function.

var characterVar = this



And then setting values to that, and to the object at the end.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5418
  • View blog
  • Posts: 14,330
  • Joined: 18-April 07

Re: Ajax Class Value Issues

Posted 30 September 2018 - 09:46 AM

I suggest you take a quick look at why this was happening because you are going to learn a LOT. Here is a hint.... what does "this" represent at the time of setting your athletics property?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1