Page 1 of 1

addEventListener scope parameter

Posted: 07 Sep 2017, 11:20
by Hamster
What exactly means the 3rd parameter in addEventListener in the documentation named scope, which is in all examples set to this? What is it usage and why is it in the documentation as type Number?

For example:
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);

In the documentation it says:

Parameters:
...
{Number} scope
The object that acts as a context for the event listener: it is the object that acts as a "parent scope" for the callback function, thus providing context (i.e. access to variables and other mehtods) to the function itself.


TIA

Re: addEventListener scope parameter

Posted: 07 Sep 2017, 16:28
by Bax
First of all, you are looking at the documentation of an older version of the JavaScript API.
If you are using SmartFoxServer 2X v2.13 (or later), you should use the API tagged as "w/ binary protocol", available here: http://www.smartfoxserver.com/download/sfs2x#p=client
The updated doc for the "binary" API is available here: http://docs2x.smartfoxserver.com/api-docs/jsdoc/client/

In any case that type "number" is wrong, it should be "object".
For more details on what the scope is, you will find a ton of resources online. For example this: https://www.w3schools.com/js/js_scope.asp

Re: addEventListener scope parameter

Posted: 08 Sep 2017, 09:34
by Hamster
Thanks for your answer, I haven't seen the new documentation before.

The reason why I was asking this question is because I use the Javascript API with Angular2 and with Smartfox Typescript type definitions from Definitelly Typed found in their repository. Someone made a great job with this type definitions and according to the old documentation the addEventListener has it's 3rd parameter as Number, which causes error when using this at compile time.

The new documentation should fill the missing Types (addEventListener method is one without it), so the Typescript definition files can be corrected. This is also part of another conversation on this forum.

I think it would be very helpful to have a client native in AngularJS 1 (with Promises) and Angular2 (with Observables; has a method to make them from callables) instead of Javascript API's with callbacks, or at least a wrapper around the Javascript API (like here for AngularJS). Unfortunately I don't have the knowledge for this yet.

Re: addEventListener scope parameter

Posted: 08 Sep 2017, 09:55
by Bax
I'm sorry but we don't support specific frameworks. Our API is supposed to be as generic as possible, so everybody can use it with a little or no effort.
As it regards the type definitions, see the other post.