0 Favourites

[Newbie] js lib import through require.js

  • 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.

    requirejs(["./myJsLib"], function (lib) {
            lib.doSomething();
        });
    [/code:4efybze6]
    
    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:4efybze6]
        // 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>
    [/code:4efybze6]
    
    Thanks for your reply!
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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?

  • [quote:1jk4flt0]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:

    scirra.com/manual/18/plugin-settings

    Example :

    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"
    	};
    };[/code:1jk4flt0]
  • Hi Guys!

    Thanks for the feedback, that was just what I was looking for. I will try this!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)