Howto: External Javascript to call a QCubed method
Mon, 07/18/2011 - 21:18
Hello,
Sorry if this has been covered before.
I'm trying something new.
Is it possible to have for example an external Javascript and HTML onchange event calling a qcubed method, without using a Qform object?
Thanks.
Regards,
tronics

you could create your own event with a special EventName (i.e.: "ExternalEvent"), register a QAjax(Control)Action with the method that handles your external events.
On the js side:
onchange = function(){ qcubed.pA("myFormId","theControlIdwiththeregisteredevent","ExternalEvent","the_string_you_want_to_send_back"); }
Thank you.
I almost got there but one question to go =)
'ExternalEvent' is what exactly I want to call the OnChange event?
Eg. this does not work:
qcubed.pA('InvoiceEditForm','c3','click','the_string_you_want_to_send_back');
It should call this test ClickEvent:
$this->txtButton1 = new QButton($this);
//$this->txtButton1->ControlId // = c3 I checked that
$this->txtButton1->AddAction(new QClickEvent(), new QToggleDisplayAction($this->TestpanelObject));
Thank you.
Using the following line should work with your example:
qc.pA('InvoiceEditForm','c3','QClickEvent','the_string_you_want_to_send_back');
You have to use the class name of the event instead of the event name (my fault :))
the "ExternalEvent" was just an example you could create any event with any class name and event name.
But you have to register an event on a control or on the QForm and if it was no custom event it could be triggered (i.e.: a QClickEvent on a button control is not the best combination to use as a proxy :) ).
Example: create a custom event, register an ajax action for this event on QForm.
class QExternalEvent extends QEvent {const EventName = "ExternalEvent";
}
In your custom javascript you could use
$('.myselect').change(function() {qc.pA('InvoiceEditForm','c3','QExternalEvent',$(this).val());
});
Thank you. This is great.
It truly makes a lot of sense not to misuse a ClickEvent for the proxy. =)
However I have this now working with QClickEvent but it does not work yet with QExternalEvent.
Where do you suggest to put the code for the QExternalEvent class - is it suitable to be put in the same file as the EditForm (class InvoiceEditForm extends InvoiceEditFormBase)? Because this is where I tried it.
Another issue is that I have not realized how to get the actual value that was being sent. Can this be fetched with QApplication::QueryString('varname')?
Thanks again.
Best,
tronics
Sorry for the late reply.
@positioning of the derived event ... putting the code in the same file as the edit form should work.
@ return value: the last parameter of the "qc.pA" javascript method is returned to your ajax callback method as $strParameter
just define your callback method like this one:
public function myCustomFunction ($strFormId, $strControlId, $strParameter) { ... }if you use:
$('.myselect').change(function() {qc.pA('InvoiceEditForm','c3','QExternalEvent',$(this).val());
});
you will see that the current selection is returned as the $strParameter
I think you can also find the value of your control in $_POST("mycontrolId")
Thank you that helped a lot!!