Development

dgDojoPlugin/DojoJavascript

You must first sign up to be able to contribute.

Version 2 (modified by Dean.Glazeski, 9 years ago)
--

PHP to Javascript Framework

The main idea of this framework is to create a simple interface to create Javascript while still using a PHP interface. This was accomplished by creating a few classes that help to manage Javascript functions, variables, and the script itself. This classes include the following:

Javascript Manager

The purpose of this class is to maintain the Javascript that a user wishes to have executed and also maintains a registration of variables that are currently in use. The class is non-static and allows you to have more than one manager to manage different sections of Javascript.

Creating a Javascript Manager

The Javascript Manager does maintain a single static method to retrieve or create Javascript Managers. For instance, to get the manager for the Dojo instance, one might do the following.

<?php
$manager = JSManager::getManager('dojo');
?>

If the dojo instance does not exist, the manager will be created and registered within the JSManager's list of managers. From here, all methods are non-static and only affect that instance of the class.

Adding Javascript to the Manager

The Javascript manager allows you to easily add Javascript to be executed. For instance, to have an alert be fired using Javascript, you could do the following.

<?php
// get the default Javascript manager
$manager = JSManager::getManager();
$manager->addScript('alert("Hello, user")');
?>

This will add the script to the manager. Semicolons are automatically added to lines, so you don't need to worry about those. With Javascript added, you now need to retrieve it.

Retrieving the Javascript from a Manager

Getting the Javascript currently set up in a manager is simple. All you need to do is the following.

<?php
// get the default Javascript manager
$manager = JSManager::getManager();
$manager->printScript();
?>

This will print the Javascript that has been registered with the default manager. This script will be printed in Javascript tags to make this even easier.

Registering or Unregistering Variable Names

The Javascript manager maintains an array of variable names that are currently in use in the Javascript. In order to maintain the list, you must use the register variable functions. For example, to register the variable 'foo', you would do the following.

<?php
// get the default Javascript manager
$manager = JSManager::getManager();
$varName = $manager->registerVar('foo');
?>

The registerVar will attempt to register the variable name and return the actual variable name. The reason this is done is so that if the variable is already registered, the function will return the new variable name, namely the desired name with a number appended to it. This helps to keep the variables unique. If you want to reuse a variable name and still want to register it, you can do the following.

<?php
// get the default Javascript manager
$manager = JSManager::getManager();
// open up the foo variable name
$manager->unregisterVar('foo');
// register it again
$var = $manager->registerVar('foo');
?>

By doing this, you can be sure that you won't overwrite any variable you want to use. The manager can be nice for managing Javascript, but there are two other classes that help out.

Javascript Variable

The Javascript variable automatically registers its name with the specified Javascript manager, default is the default manager. It also adds some capabilities to really help with writing Javascript. Once it has the manager, whenever you do anything with the JSVar, it will automatically add the Javascript equivalent to the manager.

Creating a Javascript Variable

The creation of a JSVar is simple. You do the following.

<?php
// register foo as name using the myapp manager
$var = new JSVar('foo', 'myapp');
// register bar as name using the default manager
$var2 = new JSVar('bar');
// create a unique variable in the default manager
$var3 = new JSVar();
?>

After that, you can use that variable to do tons of stuff.

Using a JSVar to Do Javascript

Writing Javscript is now just as easy as calling functions and setting properties of a variable. For example,

<?php
// create a unique variable in the default manager
$var3 = new JSVar();

// get the default Javascript manager
$manager = JSManager::getManager();
$manager->addScript("var $var3 = dojo.byId('foo')");

// JS: unique.srcNode = 'foobar';
$var3->srcNode = 'foobar';

// JS: var unique1 = unique.style;
// JS: unique1.display = 'none';
$var3->style->display = 'none';

// JS: var unique2 = unique.getChildren();
// This returns a JSVar, but we ignore it
$var3->getChildren();
?>

JSVar destruction

Whenever the JSVar is destroyed, it automatically unregisters its variable name so that it can be used again. So, when you call a function but don't capture its return variable, the variable name won't get used up.