Hi all, im working on a plugin that needs to do some ajax calls. It's
all working, but im wondering if this is the right way to do it..
I create a button (in a template inside the plugin dir, so that recent
addition is working great):
<roundcube:button command="plugin.xs4all_greenlist-add" type="input"
class="button" label="xs4all_greenlist.xs4all_greenlist_add" />
In JS:
rcmail.register_command('plugin.xs4all_greenlist-add',
'xs4all_greenlist_add' , true);
function xs4all_greenlist_add()
{
rcmail.http_post('plugin.xs4all_greenlist-add',
'_greenlist_entry='+entry, true);
}
And in PHP:
$this->register_action('plugin.xs4all_greenlist-add', array($this,
'xs4all_greenlist_add'));
function xs4all_greenlist_add($args) { global $OUTPUT;
....
$OUTPUT->command('xs4all_greenlist_add_greenlist_row');
}
Up to here it was pretty straightforward. But i was fiddling a bit to
get the ajax return correct. It's returning a JSON reply with an exec
field that can execute a JS function. I ended up doing this in my js
file, otherwise the function couldnt be found:
rcube_webmail.prototype.xs4all_greenlist_add_greenlist_row = function() { ..... }
This seems to be working, but i just want to make sure this is the
correct way to handle this, before I end up having to change this
plugin later on.
Thanks,
Cor _______________________________________________ List info: http://lists.roundcube.net/dev/
Cor Bosman wrote:
Hi all, im working on a plugin that needs to do some ajax calls. It's all working, but im wondering if this is the right way to do it..
[...]
Up to here it was pretty straightforward. But i was fiddling a bit to get the ajax return correct. It's returning a JSON reply with an exec field that can execute a JS function. I ended up doing this in my js file, otherwise the function couldnt be found:
rcube_webmail.prototype.xs4all_greenlist_add_greenlist_row = function() { ..... }
This seems to be working, but i just want to make sure this is the correct way to handle this, before I end up having to change this plugin later on.
Hi Cor
It looks like we've overseen something when we implemented the API. To be honest, your use case is not yet respected in the current version of the API.
You're absolutely right, the only way to make use of the handy $OUTPUT->command('...') command is to add the callback function to the rcube_webmail prototype. Not a very nice solution.
One solution would be to pass the callback function reference to rcmail.http_post() but then your function gets the entire response and you cannot use $OUTPUT->command('...') on the server side.
Another way to solve this would be to send $OUTPUT->command('plugin.somecommand') and the client will then trigger an event. The client part of the plugin will have to register an event listener for 'plugin.somecommand'. This would be the callback function for the asynchronous request. To make things easier we would limit the number of arguments to one. Then it'll perfectly fit into the event-system of the RoundCube client.
What do you think, any other ideas?
~Thomas _______________________________________________ List info: http://lists.roundcube.net/dev/
It looks like we've overseen something when we implemented the API.
To be honest, your use case is not yet respected in the current
version of the API.You're absolutely right, the only way to make use of the handy
$OUTPUT->command('...') command is to add the callback function to
the rcube_webmail prototype. Not a very nice solution.One solution would be to pass the callback function reference to
rcmail.http_post() but then your function gets the entire response
and you cannot use $OUTPUT->command('...') on the server side.Another way to solve this would be to send $OUTPUT-
command('plugin.somecommand') and the client will then trigger an
event. The client part of the plugin will have to register an event
listener for 'plugin.somecommand'. This would be the callback
function for the asynchronous request. To make things easier we
would limit the number of arguments to one. Then it'll perfectly fit
into the event-system of the RoundCube client.What do you think, any other ideas?
I was myself thinking along the lines of your second method was well.
Have the plugin register an event listener for a command (something
like: rcmail.register_callback('plugin.somecommand',
'my_callback_function');
When you say, limit the arguments to one you mean like:
function my_callback_function(e,args) { var status = args.status; var message = args.message; ... }
As long as you can pass back multiple pieces of information i think
thats fine.
Cor _______________________________________________ List info: http://lists.roundcube.net/dev/