PDA

View Full Version : Ext.MessageBox.confirm and scope problem



flyzb
28 Nov 2007, 4:38 PM
I met a problem about Ext.MessageBox.conirm, code is below:

Ext.MessageBox.confirm(
'confirm',
'Delete Role?',
function(btn, text){
if(btn=='yes')
{
Ext.Ajax.request({
url: '/Services/Login/RoleAction.ashx?RoleName='+Name+'&Action=Delete',
method : 'get',
success : function(response,options){
this.store.load();
},
failure: function(){
alert('delete erro!')
},
scope: this
});

}
});

In above , there is erro in the line 'this.store.load'. But if i change the code below,there is no erro ,why?

if(confirm('delete role?)){
Ext.Ajax.request({
url: '/Services/Login/RoleAction.ashx?RoleName='+Name+'&Action=Delete',
//method : 'get',
success : function(response,options){
this.store.load();
},
failure: function(){
alert('delete erro!') },
scope: this
});
}
}
else{
alert('need select role!')
}
}

hendricd
28 Nov 2007, 4:54 PM
You haven't really posted all the code necessary to explain this but,

in your first example, (scope: this) would be scoped to Ext.MessageBox because you declared the function within a member function of Ext.MessageBox.

In the second, 'this' just happens to be your grids' scope when you made the AJax request.

flyzb
28 Nov 2007, 5:01 PM
thanks. I konow, but in the first way how can I get the grid scope?

hendricd
28 Nov 2007, 5:04 PM
Give your GridPanel a component 'id'. Then reference it anywhere like:



Ext.getCmp('gridId').getStore()....

franck34
1 Jan 2008, 6:25 AM
I'm new with extJs and i'm making a desktop app. Got a grid and a delete button, and of course a confirm box.

To avoid using 'mymodule-win' everywhere in the code, i just defined a variable "module" in the top of my desktop module. I can use it everywhere in the code of this module, except in the confirm box context, because "this" is out of 'mymodule-win' (and it's logical).

So question is : is there a way to pass a parameter i can use in the function which handle the result of ok/cancel button in the confirm box ?

The goal should be to have something like (this example doesn't work because this.module not found because of the scope i suppose):



MyDesktop.myModule = Ext.extend(Ext.app.Module, {

module:'mymodule',
module_title:'This is my first module',
id:null,

init : function() {
this.id=this.module+'-win';
this.launcher = {
text: this.module_title,
iconCls:'icon-grid',
handler : this.createWindow,
scope: this
}
},

...............

onContext:function(grid, rowIndex, ev) {
ev.stopEvent();
var rowSelected = grid.getSelections();
var selected_id = grid.getView().ds.getAt(rowIndex).id;
var menu = new Ext.menu.Menu();
if (rowSelected.length==1) {
var item = menu.add({
scope: this,
text: 'Edit',
icon: 'shared/icons/fam/user_edit.png',
handler: function() { this.edit('Edit',selected_id); }
});
menu.addSeparator();
}
var item = menu.add({
scope: this,
text: 'Delete',
icon: 'shared/icons/fam/delete.gif',
module: this.module,
handler: function(){
//console.log(this);
Ext.MessageBox.confirm(
'Please confirm',
'Are you sure ?',
function(btn, text) {
if (btn=='yes') {
//console.log(this);
Ext.Ajax.request({
url: 'delete.php',
method: 'POST',
params: 'id='+selected_id,
success : function(response,options){
MyDesktop.getModule(this.module).grid.getView().ds.load();
},
failure: function(){
alert('delete error');
},
scope: this
});
}
}
);
}
});
menu.showAt(ev.getXY());
}
.....


Thanks for helping.

hendricd
1 Jan 2008, 10:39 AM
var item = menu.add({
scope: this,
text: 'Delete',
icon: 'shared/icons/fam/delete.gif',
module: this.module,
handler: function(){
//console.log(this);
Ext.MessageBox.confirm(
'Please confirm',
'Are you sure ?',
function(btn, text) {
if (btn=='yes') {
//console.log(this);
Ext.Ajax.request({
url: 'delete.php',
method: 'POST',
params: 'id='+selected_id,
success : function(response,options){
MyDesktop.getModule(this.module).grid.getView().ds.load();
},
failure: function(){
alert('delete error');
},
scope: this
});
}
},
this //just needed the last scope parameter
);
}
});
menu.showAt(ev.getXY());

franck34
3 Jan 2008, 9:21 AM
work like a charm. Thanks.