Solved

Problems with function literal used as DWR callback.

Posted on 2007-12-03
4
949 Views
Last Modified: 2008-02-27
I have written a piece of code to be executed on form submit. The code consists of a containing object called EventHandler which handles both initialisation and execution of event handlers.

I use prototype for the eventhandling code. The problem is that the property error of type StringBuffer (another object) does not play well with the function literal myCallback (which is invoked by DWR).

I want to store all validation errors in the error property for access later but neither of the two versions I have attached the code to does the trick for me. I am guessing it has to do with the scope in JavaScript but I can't pinpoint the problem.

Any help greatly appreciated!
//
//Invoked by
//
Event.observe(matchPlayerFormId, 'submit', this.formSubmit.bind(this));
 
//
//Version 1
//
EventHandler.prototype.formSubmit = function(event) {
	this.error = new StringBuffer();
 
	var myCallback = function(data){
		console.log("in callback, result = '" + data + "'");
		if (data.length > 0) {
			this.error.append(data);
			console.log("Intermediate: " + error.toString());
		}
	};
 
	for (var i = 0; i < this.validationNodes.length; i++){
		var variableId = this.validationNodes[i].id;
			
		var variableName = this.validationNodes[i].name;
	
		var variableValue = document.getElementById(variableId).value;
		
		JMatchValidator.validate(variableName, variableValue, myCallback.bind(this));
 
	}
		
	if (this.error.length() > 0){
		console.log("Submit cancelled");
		Event.stop(event);
		
	}
}
 
//
//Version 2
//
EventHandler.prototype.formSubmit = function(event) {
	var error = new StringBuffer();
 
	var myCallback = function(data){
		if (data.length > 0) {
			error.append(data);
		}
	};
 
	alert("Start submit");
	for (var i = 0; i < this.validationNodes.length; i++){
		var variableId = this.validationNodes[i].id;
			
		var variableName = this.validationNodes[i].name;
	
		var variableValue = document.getElementById(variableId).value;
 
		JMatchValidator.validate(variableName, variableValue, myCallback.bind(this));
 
	}
		
	if (error.length() > 0){
		console.log("Submit cancelled");
		Event.stop(event);
		
	}
}

Open in new window

0
Comment
Question by:kveasana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 20524057
// Try this:
//
var self = this;
JMatchValidator.validate(variableName, variableValue, myCallback.bind(this, self));

// I have noticed that "bind" drops the first object from the argument list
// The first object bwcomes the object that is "called"
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20999905
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question