PlugIn Development - Javascript Functions Scope

For developers using the Construct 2 Javascript SDK

Post » Thu Mar 05, 2015 6:29 pm

Hello all, I am new to construct and I am developing a plugin to solve a problem I have.

I am implementing a Push Notifications Plugin for android HTML5 games. In fact, I have it working using google push notifications service. But not the way I think a plugIn must be done.

I wrote the plugin using the JS SDK provided by scirra. But I hit a wall when I needed a javascript function that can be call from outside the application, via the Cordova Push Notifications Plugin (https://github.com/phonegap-build/PushPlugin.git).

That (and all push services) requires that the applications needs to be accessible (via a JS function) from the Device OS (Android).

In my implementation I had to include an external JS file into the index.html, where the "onNotification" function is defined. If I define that same function inside the plugin, it does not work.

In order to be a proper plugin, it must be autonomous and self contained. Or at least it must be Construct 2 itself who includes that file.


So my question is:

Plan A - How or where (inside the plugin) do I define a javascript function that has the same scope that the ones defined in the index.html file?

Plan B - How do I define a plugin that once the game is exported to Cordova or Phonegap, Construct 2 itself imports the needed JS files and writes the <script> tag including it in the index.html file.


Thank you very much for your help!
B
3
S
1
Posts: 9
Reputation: 343

Post » Thu Mar 05, 2015 6:44 pm

To add a global function that won't be renamed by closure compiler, just use:

window["myGlobalFunction"] = function () { ... };
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,516

Post » Thu Mar 05, 2015 7:21 pm

If you need to use "Plan B" you can add the JS file name in the "function GetPluginSettings()" in edittime.js
This is typically needed for JS-API based libraries when you want to keep it in a local file. My setting for ParseFE is:
Code: Select all
"dependency":   "parse-1.3.1_fe_1.js",

For the Push functions in ParseFE I listen for events from the Cordova plugin by using a construct like this:
Code: Select all
document.addEventListener('onParsePushReceived', function (pushBundle) {
}
B
18
S
7
Posts: 88
Reputation: 1,424

Post » Sat Mar 07, 2015 2:27 pm

Thank you very much for your quick answers.

I will test both options as soon as possible.

Thanks!!
B
3
S
1
Posts: 9
Reputation: 343

Post » Sat Mar 07, 2015 7:23 pm

pushnotifications.zip
Ashley wrote:To add a global function that won't be renamed by closure compiler, just use:

window["myGlobalFunction"] = function () { ... };



Hello Ashley, thanks for your reply.

I am testing your solution, and it seems to work. the problem I have now is to access the plugin properties from within the global function.

I mean, how can I do this:

window["onNotification"] = function (e) {

this.lastMessageReceived = e.payload.message;
this.runtime.trigger(cr.plugins_.pushNotifications.prototype.cnds.OnPushMessageReceived, this);

}

Of course I have defines lastMessageReceived and OnPushMessageReceived properly. But the error says "this is not defined".

I guess that this is related to the global status of the function. But how can I get the "self" object or access its properties from inside that function.

The process in general works fine. But I want to be able to catch the Push Messages from construct itself, and so stuff with it.

Thanks!!
You do not have the required permissions to view the files attached to this post.
B
3
S
1
Posts: 9
Reputation: 343

Post » Sun Mar 08, 2015 12:17 pm

This is really just a normal JS programming question, and this isn't really the place to ask that kind of thing. You just need to define the function in a scope where you can access the variables you want via a closure. Typically you have something like "var self = this" in the enclosing scope and then you refer to "self" instead of "this" in the function body. If you have other general JS programming questions best head to a site like StackOverflow.
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,516

Post » Sun Mar 08, 2015 1:45 pm

Ashley wrote:This is really just a normal JS programming question, and this isn't really the place to ask that kind of thing. You just need to define the function in a scope where you can access the variables you want via a closure. Typically you have something like "var self = this" in the enclosing scope and then you refer to "self" instead of "this" in the function body. If you have other general JS programming questions best head to a site like StackOverflow.



Thank you for your reply Ashley.

I asked that, because I already try what you mentioned. I am not familiarized on how Construct 2 compiles the JS, so I am finding hard to debug the code. But I am indeed familiarized with JS.

If you say that should work, then I will look for another error. Because I defined "self" as a global variable, and asigned self = this inside instanceProto.onCreate. And inside window["onNotification"] = function (e) {} "self" is null.

I apologize if my question was misplaced. I will try to find my answer elsewhere.


Kind regards
B
3
S
1
Posts: 9
Reputation: 343

Post » Mon Mar 09, 2015 10:56 am

No, you should not define self as a global variable.

Construct 2 uses Google Closure Compiler to compile JS code, which is separately documented by Google.
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,516

Post » Mon Mar 09, 2015 6:40 pm

Ashley wrote:No, you should not define self as a global variable.

Construct 2 uses Google Closure Compiler to compile JS code, which is separately documented by Google.



Thank you very much for your help Ashley. Your information indeed solved my issue.

Now I have a proper self contained and fully functional C2 plugin for push notifications.

Thank you very much!
B
3
S
1
Posts: 9
Reputation: 343

Post » Wed Mar 18, 2015 4:21 pm

@vairen
Hi, are you willing to share your latest version?
tnx!
B
26
S
11
G
2
Posts: 669
Reputation: 5,038

Next

Return to Javascript SDK

Who is online

Users browsing this forum: ST314 and 0 guests