Hello,
I'm learning the new ajax procedures to retrieve data and use commands, and encount a little problem.
In a form, I would like to allow user to add new actions. I have a callback like this :
function mymodule_menu() {
$items = array();
$items['js/mymodule/add_action/%'] = array(
'title' => 'Add action',
'page callback' => 'mymodule_add_action_settings',
'page arguments' => array(3),
'access arguments' => array('administer mymodule'),
'delivery callback' => 'ajax_deliver',
);
return $items;
}
function mymodule_add_action_settings( $name ) {
$data = theme( 'mymodule_action_settings', array( "name" => $name ) );
$output = array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_append('#edit-instance-settings-actions #action-ctnr', $data ),
)
);
return $output;
}
Now in my javascript file associated to my form, I have the following code :
// add behavior when user try to add new action
$('#action-add:not(.ajax-processed)').addClass('ajax-processed').each( function() {
var element_settings = {};
element_settings.url = "/js/mymodule/add_action/";
element_settings.event = 'DelayedClick';
var base = $(this).attr('id');
var ajax = new Drupal.ajax( base, this, element_settings );
$(this).click(function() {
name = $('#edit-instance-settings-actions-name').val();
if( name.length ) {
ajax.element_settings.url += name;
$(ajax.element).trigger('DelayedClick');
return false;
//$.get( Drupal.settings.basePath + "js/mymodule/add_action/" + name, null, onAddAction );
}
else {
alert( "You must add a name before adding action" );
}
});
Drupal.ajax[base] = ajax;
});
Like you see, the user must enter an action name before add action. The name will be used to define new action and the returned html is a form fragment which allow the user to add action settings.
The name is used by the menu callback, but actually, the ajax call don't embed the name in the final url callback.
I don't understand how to modify the ajax object to pass the name. Second problem is even if I modify correctly the ajax.element_settings.url variable to add the name, next time user click on the button, I think that the initial url will have the old name and the process will add a new name after the old name instead of re-use the really initial url (/js/mymodule/add_action).
Maybe I must give the name in the POST array instead of trying put it in the url ?? Any suggestion is welcome.
Thanks in advance.
Comments
I finally found the solution.
I finally found the solution.
To pass some informations to url after attaching ajax object to Drupal.ajax[base] we must alter the beforeSubmit method of the ajax object like this :
Now I can add anything I want in the target url before the ajax call :-)
Hope this help.
http://www.sabugo.ch
http://www.titouille.ch