Creating your own functions, libraries & classes

Categories:Extensibility

Overview

As mentioned in the previous article, there are several ways to add new JavaScript functionality to the (x)JSFL environment:

  • Functions
  • Libraries
  • Classes

The actual process you need to follow to make sure this functionality is available every time you use Flash is:

  1. Write your JSFL function, library or class
  2. Register the function, library or class
  3. Edit the user bootstrap so your JSFL loads when Flash starts

Write the JSFL code

It's up to you whether a function, library or class works best in your particular circumstances. There's no difference between them from the technical point of view of extending the framework, but there are some good-practices to follow. See the Writing better JavaScript section for more information.

For the sake of this demonstration, let's pretend you've written a simple library like so:

Tools =
{
    supertrace:function(str)
    {
        trace(str.toUpperCase());
    },
    superalert:function()
    {
        alert(str.toUpperCase());
    }
}

This is a simple example of a namespaced library; that is, a JavaScript Object object that contains Functions, under the namespace Tools.

In order to load libraries automatically at Flash startup, you should save your code in the user/jsfl/libraries folder. xJSFL's file-naming convention is to name files after the main class or library they contain, in lower-case, so in this case the file would be called tools.jsfl.

Register the the function, library or class

Because of the issue of variable persistence when running external scripts in Flash, you will need to register  functions, libraries and classes with xJSFL using the xjsfl.classes.register() method.

To do this, you simply add the following line after the Tools definition:

xjsfl.classes.register('Tools', Tools);

The Tools object (or your library) is then added to the xjsfl.classes Object as a new property Tools, and is permanently stored there so it can be extracted to global scope each time the framework is initialized. (If the framework has not yet been initialized, you will still be able to access your library via xjsfl.classes.Tools).

Note that you can have as many functions, libraries and classes in each file as you like, but you will need to register each one individually that you later require direct access to.

Save your library

The final step in the process is to have your library file load each time Flash starts. This is done simply by saving the file in your user/jsfl/libraries folder, as the default user bootstrap will load all libraries at startup.

One last thing to bear in mind: if there are any syntax or runtime errors in your library file, Flash will throw a misleading error on startup. Make sure your file works before saving it!

Use your library

From now on, your Tools library will be loaded with Flash and will be available any time after calling xjsfl.init():

// Script just started, but Tools is not yet available

// initialize framework
    xjsfl.init(this);

// Tools is now available!
    Tools.superalert('Hello world!');

Next steps

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>