PDA

View Full Version : createCallback



seldon
1 Dec 2006, 8:40 AM
Hi,

I'm calling the el.getUpdateManager().update(url, vars, callback); function and want to pass some vars along with the callback. So in a previous post I was told to do:

el.getUpdateManager().update(url, vars, callback.createCallback(myvar1, myvar2);

Then my callback function would look like

function callback(var1, var2){
}

Now this is fine, but I would also like to get the responseObject to be able to parse the responseText. So I would like this to work:

function callback(var1, var2,o){
alert(o.responseText);
}

How to do this?

Thanks!

Seldon

jack.slocum
1 Dec 2006, 8:54 AM
Use createDelegate instead and pass "appendArgs" as true (or a number to insert them in a specific spot). Check the docs for more information on createDelegate.

seldon
1 Dec 2006, 9:27 AM
As in:

saveBtn = dialog.addButton('Opslaan', this.submitEditDialog.createDelegate(this, new Array(tablename, id), true), dialog);

?

jay@moduscreate.com
1 Dec 2006, 11:36 AM
The only way i could figure out how to do that is to use global vars (limited quantities).

jack.slocum
1 Dec 2006, 11:51 AM
Yeah, although it's faster to say:

saveBtn = dialog.addButton('Opslaan',
this.submitEditDialog.createDelegate(this, [tablename, id], true), dialog);

Then those arguments get appended to the standard arguments that get passed.

You can also replace true with 0 to have the arguments isnerted first and have the standard arguments last. You can use the number to insert the arguments anywhere.

seldon
1 Dec 2006, 1:27 PM
Thnx! My code seems to go wrong however on that line (without giving a js error) ?
Any idea?

tryanDLS
1 Dec 2006, 1:30 PM
try wrapping in a try-catch to see if maybe an error is getting swallowed before it gets back to the browser.

seldon
1 Dec 2006, 4:55 PM
Yeps that worked. Thnx! I got a new/different but related question, which is due to a lack of javascript knowledge I guess. What should be at the place of the ??? to get this to work:


submitEditDialog : function(tablename, id, o){
....

var submitSuccess = function(tablename, id, o){
....
};

YAHOO.util.Connect.asyncRequest('POST', 'url',
{success: ????.submitSucces.createDelegate(???, [tablename, id], 0), failure: submitFailure});
},

Thanks again!

Seldon

Animal
1 Dec 2006, 11:51 PM
What "this" scope does submitSuccess have to run in?

Are you inside an object at that point?

If you just have a load of global functions swimming around, then it gets difficult. We can't tell from that code snippet, but if you're not in a particular object, then there is no context, so nothing goes there, and you use createCallback rather than createDelegate.

If you are inside an object, you use "this".

seldon
2 Dec 2006, 3:08 AM
Well, the total code looks like this:


Example = function(){
....
return {
....
submitEditDialog : function(tablename, id, o){
....

var submitSuccess = function(tablename, id, o){
....
};

YAHOO.util.Connect.asyncRequest('POST', 'url',
{success: ????.submitSucces.createDelegate(???, [tablename, id], 0), failure: submitFailure});
},
....
}
....
}();

So I'm in the Example object, hence use this? (at both places of the ???) ?

Thanks (i ask this, because i tried this, but it doesnt seem to work)

Animal
2 Dec 2006, 3:23 AM
It won't work, because you are not calling a constructor.

The Example object ends up as just the result of the immediate call of that anonymous function.

Try



function Example(<whatever params you need to set it up>)
{
// Your setup code which sets up all your this.<whatever you want to keep in an instance of Example>
}


and



Example.prototype =
{
submit: function(url)
{
},

submitSuccess: function(<whatever you set up in your createDelegate call)>
{
},

etc...
};


Then



myExample = newExample(<whatever params you need to set it up>);
myExample.submit("http://blah");


The functions that you call now using myExample will have a "this" that they can store things in and access things from. That's what you need to pass into createDelegate.

seldon
6 Dec 2006, 11:42 AM
I'm sorry for asking again, but I just cant seem to get it right. This is what i have:


YAHOO.ext.UpdateManager.defaults.loadScripts = true;

function Page(){

var dialog, saveBtn;
var info, wait, error;

this.init = function(){
...
}

...

this.submitEditDialog = function(tablename, id, o){
YAHOO.util.Connect.setForm(document.getElementById('form_' + tablename));
YAHOO.util.Connect.asyncRequest('POST', 'index.php?action=updateRecord&selectedtable=' + tablename + '&id=' + id, {success: this.submitSucces.createDelegate(this, [tablename, id], 0), failure: this.submitFailure});
}

this.submitSuccess = function(tablename, id, o){
alert('a');

if(o.responseText == ''){
alert(o.responseText);
//getEl('holder_' + tablename).getUpdateManager().update('index.php', 'tablename=' + tablename + '&id=' + id);
dialog.hide();
} else {
error.radioClass('active-msg');
getEl('edit-panel').dom.innerHTML = o.responseText;
}
}

this.submitFailure = function(o){
alert('b');

}
}

var origo = new Page();
YAHOO.ext.EventManager.onDocumentReady(origo.init, Page, true);

The error is still: this.submitSuccess has no properties. What am i doing wrong?

THanks again!

Seldon

Slapyo
6 Dec 2006, 11:59 AM
Not sure if it's a typo or if it really is misspelled, but shouldn't it be submitSuccess

{success: this.submitSucces.createDelegate(

{success: this.submitSuccess.createDelegate(
Because below your function is this.submitSuccess

seldon
6 Dec 2006, 1:08 PM
I did have that wrong. Sorry for not seeing that :|. Corrected it now, but still the same error :S.