[Newbie] js lib import through require.js

For developers using the Construct 2 Javascript SDK

Post » Fri Jul 08, 2016 7:45 am

HI,
while I am a coder I am definitely not fluent in "modern javascript" especially when it comes to modules and loading them (AMD, CommonJS, UMD, etc...)

Now, I do have a lib that I load using require.js.
Code: Select all
requirejs(["./myJsLib"], function (lib) {
        lib.doSomething();
    });


My question is how would I use requirejs in a construct2 plugin? Is asynchronous loading possible? And where would I put this call to know that the lib has been loaded?

OR i have another idea:
in the html file generated by construct2 could I load my lib first using AMD and then have access to my lib in this context FROM the Browser object via a javascript call ?

Code: Select all
    // Start the project running on window load
    jQuery(document).ready(function ()
    {      
         requirejs(["./myJsLib"], function (lib) { // load the lib and THEN launch the app
                // Create new runtime using the c2canvas
          cr.createRuntime("c2canvas");
         }
    });
    </script>


Thanks for your reply!
Last edited by whitetigle on Fri Jul 08, 2016 10:29 am, edited 1 time in total.
B
10
S
3
Posts: 3
Reputation: 608

Post » Mon Aug 01, 2016 6:37 am

@whitetigle, why do you need requirejs? you can add your lib as a c2 plugin. or you can make your app with page that includes c2 project.
what exactly are you trying to do?
ImageImage
B
81
S
27
G
8
Posts: 191
Reputation: 11,045

Post » Thu Aug 04, 2016 4:24 pm

Message: X3M can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
dependency
This one isn't listed above, but if you need external files bundled with your plugin (e.g. a javascript library) you can specify one or more dependency files with:

1
"dependency": "file1.js;file2.js;file3.html"
You must provide these files in the plugin's folder. Construct 2 will then copy them out when exporting the project, and make them available on the preview server for testing. Construct 2 will also automatically insert a script tag in to the HTML page before the runtime for any files ending in .js, so you do not need to worry about loading them yourself.


Source:
https://www.scirra.com/manual/18/plugin-settings

Example :
Code: Select all
function GetPluginSettings()
{
   return {
      "name":         "Sprite",
      "id":         "Sprite",
      "version":      "1.0",
      "description":   "An animated object that is the building block of most projects.",
      "author":      "Scirra",
      "help url":      "http://www.scirra.com",
      "category":      "General",
      "type":         "world",
      "rotatable":   true,
      "flags":      pf_animations | pf_position_aces | pf_size_aces | pf_angle_aces | pf_appearance_aces,
                 "dependency": "babylon.js;myjsfile.js;file3.html"
   };
};
Banned User
B
17
S
7
G
24
Posts: 388
Reputation: 14,494

Post » Thu Aug 18, 2016 5:44 am

Hi Guys!
Thanks for the feedback, that was just what I was looking for. I will try this!
B
10
S
3
Posts: 3
Reputation: 608


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests