PDA

View Full Version : Ext.Promise, Scope ?



Sebastian.GESS
16 Jul 2015, 4:13 AM
Hi,

I was testing Ext.Promise but i could not figure out how to preserve scope for fulfilled function.

Tested code is very simple. The documentation states that the .then() statement can have three functions and a scope, so i expected the scope of the fulfilled function to be the same as the scope in the OnClick. Instead the scope turns out to be the window object.

I would love to use Promises and appreciate any help.

Thank in advance,
Sebastian




getProm: function () {
return new Ext.Promise(function (resolve, reject) {
return resolve(true);
});
},

OnBtnClick: function () {
var me = this; // Controller Scope
me.getProm().then(me.OnPromResolve, null, null, me);
},

OnPromResolve: function () {
var me = this; // Window Scope
console.log(me);
},

evant
16 Jul 2015, 4:15 AM
See: http://www.sencha.com/forum/showthread.php?302868

Sebastian.GESS
16 Jul 2015, 4:43 AM
That was quick thanks, and it works like exspected.

Psychokrameur
2 Nov 2015, 5:30 AM
Hello,
could you please share the answer here for non premium member please?


Thanks in advance,
Psycho

Sebastian.GESS
2 Nov 2015, 5:39 AM
Hi,

just use Ext.Defered insted.
link: http://docs.sencha.com/extjs/6.0/6.0.1-classic/#!/api/Ext.Deferred.

Psychokrameur
2 Nov 2015, 6:01 AM
Ok thanks.

Is there a way to pass the scope as a parameter to the Ext.Deferred?

Something like:


var deferred = new Ext.Deferred({
scope: myScope
});

[...]

deferred.resolve(true);

// so deferred.resolve is called in the scope of "myScope"


(I've tried, deferred.resolve.call(myScope, true) or deferred.resolve.apply(myScope, [true]) without success)

Sebastian.GESS
2 Nov 2015, 6:53 AM
Here is a little example i hope it helps.

https://fiddle.sencha.com/#fiddle/10di

greets,
Sebatian

Psychokrameur
2 Nov 2015, 12:19 PM
Not exactly what I wanted but thanks in any case!

Sebastian.GESS
3 Nov 2015, 3:29 AM
Well if You could explain it in more detail maybe i can help. If you just want to change the Scope of a fuction call to resolve your defered. You can do something like myfunction.apply(Scope, [deferedvar, arg1,arguments]);

But its more likely you wanted to do something like this.



doAsyncAction: function(){
...
defered.resolve(true); // you can also pass an object
defered.reject(false);
}

doasyncAction().then(success, failure, null, scope);

...

success: function(mybool){
// mybool is true
// this = scope i passed
}

failure: function(mybool){
// mybool is false
// this = scope i passed
}



you can of course pass the object in the deferec.resolve and then do an apply call on that object in the resolve function.

Psychokrameur
4 Nov 2015, 12:30 AM
thanks @Sebatian

I was hopping to be able to do something like:
deferred.resolve.apply(scope, args) and deferred.reject.apply(scope, args)

In order to avoid having a call like then(fct, null, null, scope)

But apparently is not possible.
However, it work with what you propose, that's fine :)

Psychokrameur
4 Nov 2015, 6:04 AM
Hello @Sebatian,


I check a little closer on your example and this great!

I'd like this :)

Sebastian.GESS
4 Nov 2015, 7:08 AM
thanks, i am happy to help.