Extranl script with Ajax and UI Dialogs close immediately
Hi everyone,
I have a script who work perfectly as an inline script :
- <script>
- // jqn() is a custom utility finction that escape dots for allowing jquery to find items with dots
$(document).ready(function(){
$(jqn('#lnk.addnote')).click(function(){
$.get('/webapp/ui/parts/notes/add.html', function(data) {
var dlg = $("<div class=\"dialogcontainer\"></div>").append(data);
$(dlg).dialog({bgiframe: true, modal: true,
buttons : {
'Annuler' : function(){
$(dlg).dialog('close');
},
'Créer' : function(){
var datas = $(dlg).find('form').serialize();
alert('POST : \n/edu-webapp/ui/parts/notes/add.html?'+datas);
$.post('/webapp/ui/parts/notes/add.html?'+datas, function(fragment) {
alert('RECEIVED : \n'+fragment);
alert('EMPTY : \n'+$('#lstnotes'));
$('#lstnotes').empty().append(fragment);
alert('CLOSE DIALOG');
$(dlg).dialog('close');
});
}
},
title: 'Add note'
});
});
return false;
});
});
</script>
I have tried to externalize it :
- var Notes = {
add : function() {
alert("ADD FIRED");
$.get(CONTEXT+'/ui/parts/notes/add.html', function(fragment) {
alert("CREATING DIALOG");
var _dlg = $('<div class="dialogcontainer"></div>').append(fragment);
// Tip to enable expression evaluation for buttons label
var _buttons = {};
_buttons[Tsl.translate('label.Cancel')] = function() {
alert("CLOSE DIALOG");
$(_dlg).dialog("close");
};
_buttons[Tsl.translate('label.Create')] = function() {
var values = $(dlg).find('form').serialize();
alert("POST \n"+values)
$.post(CONTEXT+'/ui/parts/notes/add.html?'+values, function(response) {
alert("UPDATING "+$('#lstnotes'));
$('#lstnotes').empty().append(response);
alert("CLOSE DIALOG");
$(dlg).dialog('close');
});
};
$(_dlg).dialog({bgiframe: true, model: true, buttons: _buttons,
title: 'TO i18n'});
});
}
};
- <script>
$(document).ready(function(){
- $(jqn('#lnk.addnote')).click(Notes.add);
- });
- </script>
But when I click on the "lnk.addnote" link, the dialog is shown and immediately closed. In fact i can see the "ADD FIRED" alert, then the "CREATING DIALOG" alert but nothing appear. And finally I got this message into the Firebug console "
$ is not defined".
Can someone help me ?
Thanks